a719e903eed995d856526905f45f58c33517c1b2
[safe/jmp/linux-2.6] / 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_debug.h"
62 #include "et131x_defs.h"
63
64 #include <linux/pci.h>
65 #include <linux/init.h>
66 #include <linux/module.h>
67 #include <linux/types.h>
68 #include <linux/kernel.h>
69
70 #include <linux/sched.h>
71 #include <linux/ptrace.h>
72 #include <linux/slab.h>
73 #include <linux/ctype.h>
74 #include <linux/string.h>
75 #include <linux/timer.h>
76 #include <linux/interrupt.h>
77 #include <linux/in.h>
78 #include <linux/delay.h>
79 #include <linux/io.h>
80 #include <linux/bitops.h>
81 #include <asm/system.h>
82
83 #include <linux/netdevice.h>
84 #include <linux/etherdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/if_arp.h>
87 #include <linux/ioport.h>
88 #include <linux/random.h>
89
90 #include "et1310_phy.h"
91 #include "et1310_pm.h"
92 #include "et1310_jagcore.h"
93
94 #include "et131x_adapter.h"
95 #include "et131x_netdev.h"
96 #include "et131x_config.h"
97 #include "et131x_isr.h"
98
99 #include "et1310_address_map.h"
100 #include "et1310_tx.h"
101 #include "et1310_rx.h"
102 #include "et1310_mac.h"
103 #include "et1310_eeprom.h"
104
105
106 int __devinit et131x_pci_setup(struct pci_dev *pdev,
107                                const struct pci_device_id *ent);
108 void __devexit et131x_pci_remove(struct pci_dev *pdev);
109
110
111 /* Modinfo parameters (filled out using defines from et131x_version.h) */
112 MODULE_AUTHOR(DRIVER_AUTHOR);
113 MODULE_DESCRIPTION(DRIVER_INFO);
114 MODULE_LICENSE(DRIVER_LICENSE);
115
116 /* Module Parameters and related data for debugging facilities */
117 #ifdef CONFIG_ET131X_DEBUG
118 static u32 et131x_debug_level = DBG_LVL;
119 static u32 et131x_debug_flags = DBG_DEFAULTS;
120
121 /*
122 et131x_debug_level :
123  Level of debugging desired (0-7)
124    7 : DBG_RX_ON | DBG_TX_ON
125    6 : DBG_PARAM_ON
126    5 : DBG_VERBOSE_ON
127    4 : DBG_TRACE_ON
128    3 : DBG_NOTICE_ON
129    2 : no debug info
130    1 : no debug info
131    0 : no debug info
132 */
133
134 module_param(et131x_debug_level, uint, 0);
135 module_param(et131x_debug_flags, uint, 0);
136
137 MODULE_PARM_DESC(et131x_debug_level, "Level of debugging desired (0-7)");
138
139 static dbg_info_t et131x_info = { DRIVER_NAME_EXT, 0, 0 };
140 dbg_info_t *et131x_dbginfo = &et131x_info;
141 #endif /* CONFIG_ET131X_DEBUG */
142
143 /* Defines for Parameter Default/Min/Max vaules */
144 #define PARM_SPEED_DUPLEX_MIN   0
145 #define PARM_SPEED_DUPLEX_MAX   5
146
147 /* Module parameter for disabling NMI
148  * et131x_nmi_disable :
149  * Disable NMI (0-2) [0]
150  *  0 :
151  *  1 :
152  *  2 :
153  */
154 static u32 et131x_nmi_disable;  /* 0-2 */
155 module_param(et131x_nmi_disable, uint, 0);
156 MODULE_PARM_DESC(et131x_nmi_disable, "Disable NMI (0-2) [0]");
157
158 /* Module parameter for manual speed setting
159  * Set Link speed and dublex manually (0-5)  [0]
160  *  1 : 10Mb   Half-Duplex
161  *  2 : 10Mb   Full-Duplex
162  *  3 : 100Mb  Half-Duplex
163  *  4 : 100Mb  Full-Duplex
164  *  5 : 1000Mb Full-Duplex
165  *  0 : Auto Speed Auto Duplex // default
166  */
167 static u32 et131x_speed_set;
168 module_param(et131x_speed_set, uint, 0);
169 MODULE_PARM_DESC(et131x_speed_set,
170                 "Set Link speed and dublex manually (0-5)  [0] \n  1 : 10Mb   Half-Duplex \n  2 : 10Mb   Full-Duplex \n  3 : 100Mb  Half-Duplex \n  4 : 100Mb  Full-Duplex \n  5 : 1000Mb Full-Duplex \n 0 : Auto Speed Auto Dublex");
171
172
173
174 static struct pci_device_id et131x_pci_table[] __devinitdata = {
175         {ET131X_PCI_VENDOR_ID, ET131X_PCI_DEVICE_ID_GIG, PCI_ANY_ID,
176          PCI_ANY_ID, 0, 0, 0UL},
177         {ET131X_PCI_VENDOR_ID, ET131X_PCI_DEVICE_ID_FAST, PCI_ANY_ID,
178          PCI_ANY_ID, 0, 0, 0UL},
179         {0,}
180 };
181
182 MODULE_DEVICE_TABLE(pci, et131x_pci_table);
183
184 static struct pci_driver et131x_driver = {
185       .name     = DRIVER_NAME,
186       .id_table = et131x_pci_table,
187       .probe    = et131x_pci_setup,
188       .remove   = __devexit_p(et131x_pci_remove),
189       .suspend  = NULL,         /* et131x_pci_suspend */
190       .resume   = NULL,         /* et131x_pci_resume */
191 };
192
193
194 /**
195  * et131x_init_module - The "main" entry point called on driver initialization
196  *
197  * Returns 0 on success, errno on failure (as defined in errno.h)
198  */
199 int et131x_init_module(void)
200 {
201         int result;
202
203 #ifdef CONFIG_ET131X_DEBUG
204         /* Set the level of debug messages displayed using the module
205          * parameter
206          */
207         et131x_dbginfo->dbgFlags = et131x_debug_flags;
208
209         switch (et131x_debug_level) {
210         case 7:
211                 et131x_dbginfo->dbgFlags |= (DBG_RX_ON | DBG_TX_ON);
212
213         case 6:
214                 et131x_dbginfo->dbgFlags |= DBG_PARAM_ON;
215
216         case 5:
217                 et131x_dbginfo->dbgFlags |= DBG_VERBOSE_ON;
218
219         case 4:
220                 et131x_dbginfo->dbgFlags |= DBG_TRACE_ON;
221
222         case 3:
223                 et131x_dbginfo->dbgFlags |= DBG_NOTICE_ON;
224
225         case 2:
226         case 1:
227         case 0:
228         default:
229                 break;
230         }
231 #endif /* CONFIG_ET131X_DEBUG */
232
233         DBG_ENTER(et131x_dbginfo);
234         DBG_PRINT("%s\n", DRIVER_INFO);
235
236         if (et131x_speed_set < PARM_SPEED_DUPLEX_MIN ||
237             et131x_speed_set > PARM_SPEED_DUPLEX_MAX) {
238                 printk(KERN_WARNING "et131x: invalid speed setting ignored.\n");
239                 et131x_speed_set = 0;
240         }
241
242         result = pci_register_driver(&et131x_driver);
243
244         DBG_LEAVE(et131x_dbginfo);
245         return result;
246 }
247
248 /**
249  * et131x_cleanup_module - The entry point called on driver cleanup
250  */
251 void et131x_cleanup_module(void)
252 {
253         DBG_ENTER(et131x_dbginfo);
254
255         pci_unregister_driver(&et131x_driver);
256
257         DBG_LEAVE(et131x_dbginfo);
258 }
259
260 /*
261  * These macros map the driver-specific init_module() and cleanup_module()
262  * routines so they can be called by the kernel.
263  */
264 module_init(et131x_init_module);
265 module_exit(et131x_cleanup_module);
266
267
268 /**
269  * et131x_find_adapter - Find the adapter and get all the assigned resources
270  * @adapter: pointer to our private adapter structure
271  *
272  * Returns 0 on success, errno on failure (as defined in errno.h)
273  */
274 int et131x_find_adapter(struct et131x_adapter *adapter, struct pci_dev *pdev)
275 {
276         int result;
277         uint8_t eepromStat;
278         uint8_t maxPayload = 0;
279         uint8_t read_size_reg;
280         u8 rev;
281
282         DBG_ENTER(et131x_dbginfo);
283
284         /* Allow disabling of Non-Maskable Interrupts in I/O space, to
285          * support validation.
286          */
287         if (adapter->RegistryNMIDisable) {
288                 uint8_t RegisterVal;
289
290                 RegisterVal = inb(ET1310_NMI_DISABLE);
291                 RegisterVal &= 0xf3;
292
293                 if (adapter->RegistryNMIDisable == 2)
294                         RegisterVal |= 0xc;
295
296                 outb(ET1310_NMI_DISABLE, RegisterVal);
297         }
298
299         /* We first need to check the EEPROM Status code located at offset
300          * 0xB2 of config space
301          */
302         result = pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS,
303                                       &eepromStat);
304
305         /* THIS IS A WORKAROUND:
306          * I need to call this function twice to get my card in a
307          * LG M1 Express Dual running. I tried also a msleep before this
308          * function, because I thougth there could be some time condidions
309          * but it didn't work. Call the whole function twice also work.
310          */
311         result = pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS,
312                                       &eepromStat);
313         if (result != PCIBIOS_SUCCESSFUL) {
314                 DBG_ERROR(et131x_dbginfo, "Could not read PCI config space for "
315                           "EEPROM Status\n");
316                 DBG_LEAVE(et131x_dbginfo);
317                 return -EIO;
318         }
319
320         /* Determine if the error(s) we care about are present.  If they are
321          * present, we need to fail.
322          */
323         if (eepromStat & 0x4C) {
324                 result = pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
325                 if (result != PCIBIOS_SUCCESSFUL) {
326                         DBG_ERROR(et131x_dbginfo,
327                                   "Could not read PCI config space for "
328                                   "Revision ID\n");
329                         DBG_LEAVE(et131x_dbginfo);
330                         return -EIO;
331                 } else if (rev == 0x01) {
332                         int32_t nLoop;
333                         uint8_t temp[4] = { 0xFE, 0x13, 0x10, 0xFF };
334
335                         /* Re-write the first 4 bytes if we have an eeprom
336                          * present and the revision id is 1, this fixes the
337                          * corruption seen with 1310 B Silicon
338                          */
339                         for (nLoop = 0; nLoop < 3; nLoop++) {
340                                 EepromWriteByte(adapter, nLoop, temp[nLoop],
341                                                 0, SINGLE_BYTE);
342                         }
343                 }
344
345                 DBG_ERROR(et131x_dbginfo,
346                           "Fatal EEPROM Status Error - 0x%04x\n", eepromStat);
347
348                 /* This error could mean that there was an error reading the
349                  * eeprom or that the eeprom doesn't exist.  We will treat
350                  * each case the same and not try to gather additional
351                  * information that normally would come from the eeprom, like
352                  * MAC Address
353                  */
354                 adapter->has_eeprom = 0;
355
356                 DBG_LEAVE(et131x_dbginfo);
357                 return -EIO;
358         } else {
359                 DBG_TRACE(et131x_dbginfo, "EEPROM Status Code - 0x%04x\n",
360                           eepromStat);
361                 adapter->has_eeprom = 1;
362         }
363
364         /* Read the EEPROM for information regarding LED behavior. Refer to
365          * ET1310_phy.c, et131x_xcvr_init(), for its use.
366          */
367         EepromReadByte(adapter, 0x70, &adapter->eepromData[0], 0, SINGLE_BYTE);
368         EepromReadByte(adapter, 0x71, &adapter->eepromData[1], 0, SINGLE_BYTE);
369
370         if (adapter->eepromData[0] != 0xcd)
371                 /* Disable all optional features */
372                 adapter->eepromData[1] = 0x00;
373
374         /* Let's set up the PORT LOGIC Register.  First we need to know what
375          * the max_payload_size is
376          */
377         result = pci_read_config_byte(pdev, ET1310_PCI_MAX_PYLD, &maxPayload);
378         if (result != PCIBIOS_SUCCESSFUL) {
379                 DBG_ERROR(et131x_dbginfo, "Could not read PCI config space for "
380                           "Max Payload Size\n");
381                 DBG_LEAVE(et131x_dbginfo);
382                 return -EIO;
383         }
384
385         /* Program the Ack/Nak latency and replay timers */
386         maxPayload &= 0x07;     /* Only the lower 3 bits are valid */
387
388         if (maxPayload < 2) {
389                 const uint16_t AckNak[2] = { 0x76, 0xD0 };
390                 const uint16_t Replay[2] = { 0x1E0, 0x2ED };
391
392                 result = pci_write_config_word(pdev, ET1310_PCI_ACK_NACK,
393                                                AckNak[maxPayload]);
394                 if (result != PCIBIOS_SUCCESSFUL) {
395                         DBG_ERROR(et131x_dbginfo,
396                                   "Could not write PCI config space "
397                                   "for ACK/NAK\n");
398                         DBG_LEAVE(et131x_dbginfo);
399                         return -EIO;
400                 }
401
402                 result = pci_write_config_word(pdev, ET1310_PCI_REPLAY,
403                                                Replay[maxPayload]);
404                 if (result != PCIBIOS_SUCCESSFUL) {
405                         DBG_ERROR(et131x_dbginfo,
406                                   "Could not write PCI config space "
407                                   "for Replay Timer\n");
408                         DBG_LEAVE(et131x_dbginfo);
409                         return -EIO;
410                 }
411         }
412
413         /* l0s and l1 latency timers.  We are using default values.
414          * Representing 001 for L0s and 010 for L1
415          */
416         result = pci_write_config_byte(pdev, ET1310_PCI_L0L1LATENCY, 0x11);
417         if (result != PCIBIOS_SUCCESSFUL) {
418                 DBG_ERROR(et131x_dbginfo,
419                           "Could not write PCI config space for "
420                           "Latency Timers\n");
421                 DBG_LEAVE(et131x_dbginfo);
422                 return -EIO;
423         }
424
425         /* Change the max read size to 2k */
426         result = pci_read_config_byte(pdev, 0x51, &read_size_reg);
427         if (result != PCIBIOS_SUCCESSFUL) {
428                 DBG_ERROR(et131x_dbginfo,
429                         "Could not read PCI config space for Max read size\n");
430                 DBG_LEAVE(et131x_dbginfo);
431                 return -EIO;
432         }
433
434         read_size_reg &= 0x8f;
435         read_size_reg |= 0x40;
436
437         result = pci_write_config_byte(pdev, 0x51, read_size_reg);
438         if (result != PCIBIOS_SUCCESSFUL) {
439                 DBG_ERROR(et131x_dbginfo,
440                       "Could not write PCI config space for Max read size\n");
441                 DBG_LEAVE(et131x_dbginfo);
442                 return -EIO;
443         }
444
445         /* Get MAC address from config space if an eeprom exists, otherwise
446          * the MAC address there will not be valid
447          */
448         if (adapter->has_eeprom) {
449                 int i;
450
451                 for (i = 0; i < ETH_ALEN; i++) {
452                         result = pci_read_config_byte(
453                                         pdev, ET1310_PCI_MAC_ADDRESS + i,
454                                         adapter->PermanentAddress + i);
455                         if (result != PCIBIOS_SUCCESSFUL) {
456                                 DBG_ERROR(et131x_dbginfo,
457                                                 "Could not read PCI config space for MAC address\n");
458                                 DBG_LEAVE(et131x_dbginfo);
459                                 return -EIO;
460                         }
461                 }
462         }
463
464         DBG_LEAVE(et131x_dbginfo);
465         return 0;
466 }
467
468 /**
469  * et131x_error_timer_handler
470  * @data: timer-specific variable; here a pointer to our adapter structure
471  *
472  * The routine called when the error timer expires, to track the number of
473  * recurring errors.
474  */
475 void et131x_error_timer_handler(unsigned long data)
476 {
477         struct et131x_adapter *etdev = (struct et131x_adapter *) data;
478         PM_CSR_t pm_csr;
479
480         pm_csr.value = readl(&etdev->regs->global.pm_csr.value);
481
482         if (pm_csr.bits.pm_phy_sw_coma == 0)
483                 UpdateMacStatHostCounters(etdev);
484         else
485                 DBG_VERBOSE(et131x_dbginfo,
486                             "No interrupts, in PHY coma, pm_csr = 0x%x\n",
487                             pm_csr.value);
488
489         if (!etdev->Bmsr.bits.link_status &&
490             etdev->RegistryPhyComa &&
491             etdev->PoMgmt.TransPhyComaModeOnBoot < 11) {
492                 etdev->PoMgmt.TransPhyComaModeOnBoot++;
493         }
494
495         if (etdev->PoMgmt.TransPhyComaModeOnBoot == 10) {
496                 if (!etdev->Bmsr.bits.link_status
497                     && etdev->RegistryPhyComa) {
498                         if (pm_csr.bits.pm_phy_sw_coma == 0) {
499                                 /* NOTE - This was originally a 'sync with
500                                  *  interrupt'. How to do that under Linux?
501                                  */
502                                 et131x_enable_interrupts(etdev);
503                                 EnablePhyComa(etdev);
504                         }
505                 }
506         }
507
508         /* This is a periodic timer, so reschedule */
509         mod_timer(&etdev->ErrorTimer, jiffies +
510                                           TX_ERROR_PERIOD * HZ / 1000);
511 }
512
513 /**
514  * et131x_link_detection_handler
515  *
516  * Timer function for link up at driver load time
517  */
518 void et131x_link_detection_handler(unsigned long data)
519 {
520         struct et131x_adapter *etdev = (struct et131x_adapter *) data;
521         unsigned long flags;
522
523         if (etdev->MediaState == 0) {
524                 spin_lock_irqsave(&etdev->Lock, flags);
525
526                 etdev->MediaState = NETIF_STATUS_MEDIA_DISCONNECT;
527                 MP_CLEAR_FLAG(etdev, fMP_ADAPTER_LINK_DETECTION);
528
529                 spin_unlock_irqrestore(&etdev->Lock, flags);
530
531                 netif_carrier_off(etdev->netdev);
532         }
533 }
534
535 /**
536  * et131x_adapter_setup - Set the adapter up as per cassini+ documentation
537  * @adapter: pointer to our private adapter structure
538  *
539  * Returns 0 on success, errno on failure (as defined in errno.h)
540  */
541 int et131x_adapter_setup(struct et131x_adapter *etdev)
542 {
543         int status = 0;
544
545         DBG_ENTER(et131x_dbginfo);
546
547         /* Configure the JAGCore */
548         ConfigGlobalRegs(etdev);
549
550         ConfigMACRegs1(etdev);
551         ConfigMMCRegs(etdev);
552
553         ConfigRxMacRegs(etdev);
554         ConfigTxMacRegs(etdev);
555
556         ConfigRxDmaRegs(etdev);
557         ConfigTxDmaRegs(etdev);
558
559         ConfigMacStatRegs(etdev);
560
561         /* Move the following code to Timer function?? */
562         status = et131x_xcvr_find(etdev);
563
564         if (status != 0)
565                 DBG_WARNING(et131x_dbginfo, "Could not find the xcvr\n");
566
567         /* Prepare the TRUEPHY library. */
568         ET1310_PhyInit(etdev);
569
570         /* Reset the phy now so changes take place */
571         ET1310_PhyReset(etdev);
572
573         /* Power down PHY */
574         ET1310_PhyPowerDown(etdev, 1);
575
576         /*
577          * We need to turn off 1000 base half dulplex, the mac does not
578          * support it. For the 10/100 part, turn off all gig advertisement
579          */
580         if (etdev->pdev->device != ET131X_PCI_DEVICE_ID_FAST)
581                 ET1310_PhyAdvertise1000BaseT(etdev, TRUEPHY_ADV_DUPLEX_FULL);
582         else
583                 ET1310_PhyAdvertise1000BaseT(etdev, TRUEPHY_ADV_DUPLEX_NONE);
584
585         /* Power up PHY */
586         ET1310_PhyPowerDown(etdev, 0);
587
588         et131x_setphy_normal(etdev);
589
590         DBG_LEAVE(et131x_dbginfo);
591         return status;
592 }
593
594 /**
595  * et131x_setup_hardware_properties - set up the MAC Address on the ET1310
596  * @adapter: pointer to our private adapter structure
597  */
598 void et131x_setup_hardware_properties(struct et131x_adapter *adapter)
599 {
600         DBG_ENTER(et131x_dbginfo);
601
602         /* If have our default mac from registry and no mac address from
603          * EEPROM then we need to generate the last octet and set it on the
604          * device
605          */
606         if (adapter->PermanentAddress[0] == 0x00 &&
607             adapter->PermanentAddress[1] == 0x00 &&
608             adapter->PermanentAddress[2] == 0x00 &&
609             adapter->PermanentAddress[3] == 0x00 &&
610             adapter->PermanentAddress[4] == 0x00 &&
611             adapter->PermanentAddress[5] == 0x00) {
612                 /*
613                  * We need to randomly generate the last octet so we
614                  * decrease our chances of setting the mac address to
615                  * same as another one of our cards in the system
616                  */
617                 get_random_bytes(&adapter->CurrentAddress[5], 1);
618                 /*
619                  * We have the default value in the register we are
620                  * working with so we need to copy the current
621                  * address into the permanent address
622                  */
623                 memcpy(adapter->PermanentAddress,
624                         adapter->CurrentAddress, ETH_ALEN);
625         } else {
626                 /* We do not have an override address, so set the
627                  * current address to the permanent address and add
628                  * it to the device
629                  */
630                 memcpy(adapter->CurrentAddress,
631                        adapter->PermanentAddress, ETH_ALEN);
632         }
633
634         DBG_LEAVE(et131x_dbginfo);
635 }
636
637 /**
638  * et131x_soft_reset - Issue a soft reset to the hardware, complete for ET1310
639  * @adapter: pointer to our private adapter structure
640  */
641 void et131x_soft_reset(struct et131x_adapter *adapter)
642 {
643         DBG_ENTER(et131x_dbginfo);
644
645         /* Disable MAC Core */
646         writel(0xc00f0000, &adapter->regs->mac.cfg1.value);
647
648         /* Set everything to a reset value */
649         writel(0x7F, &adapter->regs->global.sw_reset.value);
650         writel(0x000f0000, &adapter->regs->mac.cfg1.value);
651         writel(0x00000000, &adapter->regs->mac.cfg1.value);
652
653         DBG_LEAVE(et131x_dbginfo);
654 }
655
656 /**
657  * et131x_align_allocated_memory - Align allocated memory on a given boundary
658  * @adapter: pointer to our adapter structure
659  * @phys_addr: pointer to Physical address
660  * @offset: pointer to the offset variable
661  * @mask: correct mask
662  */
663 void et131x_align_allocated_memory(struct et131x_adapter *adapter,
664                                    uint64_t *phys_addr,
665                                    uint64_t *offset, uint64_t mask)
666 {
667         uint64_t new_addr;
668
669         DBG_ENTER(et131x_dbginfo);
670
671         *offset = 0;
672
673         new_addr = *phys_addr & ~mask;
674
675         if (new_addr != *phys_addr) {
676                 /* Move to next aligned block */
677                 new_addr += mask + 1;
678                 /* Return offset for adjusting virt addr */
679                 *offset = new_addr - *phys_addr;
680                 /* Return new physical address */
681                 *phys_addr = new_addr;
682         }
683
684         DBG_LEAVE(et131x_dbginfo);
685 }
686
687 /**
688  * et131x_adapter_memory_alloc
689  * @adapter: pointer to our private adapter structure
690  *
691  * Returns 0 on success, errno on failure (as defined in errno.h).
692  *
693  * Allocate all the memory blocks for send, receive and others.
694  */
695 int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
696 {
697         int status = 0;
698
699         DBG_ENTER(et131x_dbginfo);
700
701         do {
702                 /* Allocate memory for the Tx Ring */
703                 status = et131x_tx_dma_memory_alloc(adapter);
704                 if (status != 0) {
705                         DBG_ERROR(et131x_dbginfo,
706                                   "et131x_tx_dma_memory_alloc FAILED\n");
707                         break;
708                 }
709
710                 /* Receive buffer memory allocation */
711                 status = et131x_rx_dma_memory_alloc(adapter);
712                 if (status != 0) {
713                         DBG_ERROR(et131x_dbginfo,
714                                   "et131x_rx_dma_memory_alloc FAILED\n");
715                         et131x_tx_dma_memory_free(adapter);
716                         break;
717                 }
718
719                 /* Init receive data structures */
720                 status = et131x_init_recv(adapter);
721                 if (status != 0) {
722                         DBG_ERROR(et131x_dbginfo, "et131x_init_recv FAILED\n");
723                         et131x_tx_dma_memory_free(adapter);
724                         et131x_rx_dma_memory_free(adapter);
725                         break;
726                 }
727         } while (0);
728
729         DBG_LEAVE(et131x_dbginfo);
730         return status;
731 }
732
733 /**
734  * et131x_adapter_memory_free - Free all memory allocated for use by Tx & Rx
735  * @adapter: pointer to our private adapter structure
736  */
737 void et131x_adapter_memory_free(struct et131x_adapter *adapter)
738 {
739         DBG_ENTER(et131x_dbginfo);
740
741         /* Free DMA memory */
742         et131x_tx_dma_memory_free(adapter);
743         et131x_rx_dma_memory_free(adapter);
744
745         DBG_LEAVE(et131x_dbginfo);
746 }
747
748 /**
749  * et131x_pci_remove
750  * @pdev: a pointer to the device's pci_dev structure
751  *
752  * Registered in the pci_driver structure, this function is called when the
753  * PCI subsystem detects that a PCI device which matches the information
754  * contained in the pci_device_id table has been removed.
755  */
756 void __devexit et131x_pci_remove(struct pci_dev *pdev)
757 {
758         struct net_device *netdev;
759         struct et131x_adapter *adapter;
760
761         DBG_ENTER(et131x_dbginfo);
762
763         /* Retrieve the net_device pointer from the pci_dev struct, as well
764          * as the private adapter struct
765          */
766         netdev = (struct net_device *) pci_get_drvdata(pdev);
767         adapter = netdev_priv(netdev);
768
769         /* Perform device cleanup */
770         unregister_netdev(netdev);
771         et131x_adapter_memory_free(adapter);
772         iounmap(adapter->regs);
773         pci_dev_put(adapter->pdev);
774         free_netdev(netdev);
775         pci_release_regions(pdev);
776         pci_disable_device(pdev);
777
778         DBG_LEAVE(et131x_dbginfo);
779 }
780
781 /**
782  * et131x_config_parse
783  * @etdev: pointer to the private adapter struct
784  *
785  * Parses a configuration from some location (module parameters, for example)
786  * into the private adapter struct. This really has no sensible analogy in
787  * Linux as sysfs parameters are dynamic. Several things that were hee could
788  * go into sysfs, but other stuff like speed handling is part of the mii
789  * interfaces/ethtool.
790  */
791 void et131x_config_parse(struct et131x_adapter *etdev)
792 {
793         static const u8 default_mac[] = { 0x00, 0x05, 0x3d, 0x00, 0x02, 0x00 };
794         static const u8 duplex[] = { 0, 1, 2, 1, 2, 2 };
795         static const u16 speed[] = { 0, 10, 10, 100, 100, 1000 };
796
797         DBG_ENTER(et131x_dbginfo);
798
799         if (et131x_speed_set)
800                 DBG_VERBOSE(et131x_dbginfo, "Speed set manually to : %d \n",
801                             et131x_speed_set);
802
803         etdev->SpeedDuplex = et131x_speed_set;
804         etdev->RegistryJumboPacket = 1514;      /* 1514-9216 */
805
806         etdev->RegistryNMIDisable = et131x_nmi_disable;
807
808         /* Set the MAC address to a default */
809         memcpy(etdev->CurrentAddress, default_mac, ETH_ALEN);
810
811         /* Decode SpeedDuplex
812          *
813          * Set up as if we are auto negotiating always and then change if we
814          * go into force mode
815          *
816          * If we are the 10/100 device, and gigabit is somehow requested then
817          * knock it down to 100 full.
818          */
819         if (etdev->pdev->device == ET131X_PCI_DEVICE_ID_FAST &&
820             etdev->SpeedDuplex == 5)
821                 etdev->SpeedDuplex = 4;
822
823         etdev->AiForceSpeed = speed[etdev->SpeedDuplex];
824         etdev->AiForceDpx = duplex[etdev->SpeedDuplex]; /* Auto FDX */
825
826         DBG_LEAVE(et131x_dbginfo);
827 }
828
829
830 /**
831  * et131x_pci_setup - Perform device initialization
832  * @pdev: a pointer to the device's pci_dev structure
833  * @ent: this device's entry in the pci_device_id table
834  *
835  * Returns 0 on success, errno on failure (as defined in errno.h)
836  *
837  * Registered in the pci_driver structure, this function is called when the
838  * PCI subsystem finds a new PCI device which matches the information
839  * contained in the pci_device_id table. This routine is the equivalent to
840  * a device insertion routine.
841  */
842 int __devinit et131x_pci_setup(struct pci_dev *pdev,
843                                const struct pci_device_id *ent)
844 {
845         int result = 0;
846         int pm_cap;
847         bool pci_using_dac;
848         struct net_device *netdev = NULL;
849         struct et131x_adapter *adapter = NULL;
850
851         DBG_ENTER(et131x_dbginfo);
852
853         /* Enable the device via the PCI subsystem */
854         result = pci_enable_device(pdev);
855         if (result != 0) {
856                 DBG_ERROR(et131x_dbginfo, "pci_enable_device() failed\n");
857                 goto out;
858         }
859
860         /* Perform some basic PCI checks */
861         if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
862                 DBG_ERROR(et131x_dbginfo,
863                           "Can't find PCI device's base address\n");
864                 result = -ENODEV;
865                 goto out;
866         }
867
868         result = pci_request_regions(pdev, DRIVER_NAME);
869         if (result != 0) {
870                 DBG_ERROR(et131x_dbginfo, "Can't get PCI resources\n");
871                 goto err_disable;
872         }
873
874         /* Enable PCI bus mastering */
875         DBG_TRACE(et131x_dbginfo, "Setting PCI Bus Mastering...\n");
876         pci_set_master(pdev);
877
878         /* Query PCI for Power Mgmt Capabilities
879          *
880          * NOTE: Now reading PowerMgmt in another location; is this still
881          * needed?
882          */
883         pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
884         if (pm_cap == 0) {
885                 DBG_ERROR(et131x_dbginfo,
886                           "Cannot find Power Management capabilities\n");
887                 result = -EIO;
888                 goto err_release_res;
889         }
890
891         /* Check the DMA addressing support of this device */
892         if (!pci_set_dma_mask(pdev, 0xffffffffffffffffULL)) {
893                 DBG_TRACE(et131x_dbginfo, "64-bit DMA addressing supported\n");
894                 pci_using_dac = true;
895
896                 result =
897                     pci_set_consistent_dma_mask(pdev, 0xffffffffffffffffULL);
898                 if (result != 0) {
899                         DBG_ERROR(et131x_dbginfo,
900                                   "Unable to obtain 64 bit DMA for consistent allocations\n");
901                         goto err_release_res;
902                 }
903         } else if (!pci_set_dma_mask(pdev, 0xffffffffULL)) {
904                 DBG_TRACE(et131x_dbginfo,
905                           "64-bit DMA addressing NOT supported\n");
906                 DBG_TRACE(et131x_dbginfo,
907                           "32-bit DMA addressing will be used\n");
908                 pci_using_dac = false;
909         } else {
910                 DBG_ERROR(et131x_dbginfo, "No usable DMA addressing method\n");
911                 result = -EIO;
912                 goto err_release_res;
913         }
914
915         /* Allocate netdev and private adapter structs */
916         DBG_TRACE(et131x_dbginfo,
917                   "Allocate netdev and private adapter structs...\n");
918         netdev = et131x_device_alloc();
919         if (netdev == NULL) {
920                 DBG_ERROR(et131x_dbginfo, "Couldn't alloc netdev struct\n");
921                 result = -ENOMEM;
922                 goto err_release_res;
923         }
924
925         /* Setup the fundamental net_device and private adapter structure elements  */
926         DBG_TRACE(et131x_dbginfo, "Setting fundamental net_device info...\n");
927         SET_NETDEV_DEV(netdev, &pdev->dev);
928         /*
929         if (pci_using_dac) {
930                 netdev->features |= NETIF_F_HIGHDMA;
931         }
932         */
933
934         /*
935          * NOTE - Turn this on when we're ready to deal with SG-DMA
936          *
937          * NOTE: According to "Linux Device Drivers", 3rd ed, Rubini et al,
938          * if checksumming is not performed in HW, then the kernel will not
939          * use SG.
940          * From pp 510-511:
941          *
942          * "Note that the kernel does not perform scatter/gather I/O to your
943          * device if it does not also provide some form of checksumming as
944          * well. The reason is that, if the kernel has to make a pass over a
945          * fragmented ("nonlinear") packet to calculate the checksum, it
946          * might as well copy the data and coalesce the packet at the same
947          * time."
948          *
949          * This has been verified by setting the flags below and still not
950          * receiving a scattered buffer from the network stack, so leave it
951          * off until checksums are calculated in HW.
952          */
953         /* netdev->features |= NETIF_F_SG; */
954         /* netdev->features |= NETIF_F_NO_CSUM; */
955         /* netdev->features |= NETIF_F_LLTX; */
956
957         /* Allocate private adapter struct and copy in relevant information */
958         adapter = netdev_priv(netdev);
959         adapter->pdev = pci_dev_get(pdev);
960         adapter->netdev = netdev;
961
962         /* Do the same for the netdev struct */
963         netdev->irq = pdev->irq;
964         netdev->base_addr = pdev->resource[0].start;
965
966         /* Initialize spinlocks here */
967         DBG_TRACE(et131x_dbginfo, "Initialize spinlocks...\n");
968
969         spin_lock_init(&adapter->Lock);
970         spin_lock_init(&adapter->TCBSendQLock);
971         spin_lock_init(&adapter->TCBReadyQLock);
972         spin_lock_init(&adapter->SendHWLock);
973         spin_lock_init(&adapter->SendWaitLock);
974         spin_lock_init(&adapter->RcvLock);
975         spin_lock_init(&adapter->RcvPendLock);
976         spin_lock_init(&adapter->FbrLock);
977         spin_lock_init(&adapter->PHYLock);
978
979         /* Parse configuration parameters into the private adapter struct */
980         et131x_config_parse(adapter);
981
982         /* Find the physical adapter
983          *
984          * NOTE: This is the equivalent of the MpFindAdapter() routine; can we
985          *       lump it's init with the device specific init below into a
986          *       single init function?
987          */
988         /* while (et131x_find_adapter(adapter, pdev) != 0); */
989         et131x_find_adapter(adapter, pdev);
990
991         /* Map the bus-relative registers to system virtual memory */
992         DBG_TRACE(et131x_dbginfo,
993                   "Mapping bus-relative registers to virtual memory...\n");
994
995         adapter->regs = ioremap_nocache(pci_resource_start(pdev, 0),
996                                               pci_resource_len(pdev, 0));
997         if (adapter->regs == NULL) {
998                 DBG_ERROR(et131x_dbginfo, "Cannot map device registers\n");
999                 result = -ENOMEM;
1000                 goto err_free_dev;
1001         }
1002
1003         /* Perform device-specific initialization here (See code below) */
1004
1005         /* If Phy COMA mode was enabled when we went down, disable it here. */
1006         {
1007                 PM_CSR_t GlobalPmCSR = { 0 };
1008
1009                 GlobalPmCSR.bits.pm_sysclk_gate = 1;
1010                 GlobalPmCSR.bits.pm_txclk_gate = 1;
1011                 GlobalPmCSR.bits.pm_rxclk_gate = 1;
1012                 writel(GlobalPmCSR.value,
1013                        &adapter->regs->global.pm_csr.value);
1014         }
1015
1016         /* Issue a global reset to the et1310 */
1017         DBG_TRACE(et131x_dbginfo, "Issuing soft reset...\n");
1018         et131x_soft_reset(adapter);
1019
1020         /* Disable all interrupts (paranoid) */
1021         DBG_TRACE(et131x_dbginfo, "Disable device interrupts...\n");
1022         et131x_disable_interrupts(adapter);
1023
1024         /* Allocate DMA memory */
1025         result = et131x_adapter_memory_alloc(adapter);
1026         if (result != 0) {
1027                 DBG_ERROR(et131x_dbginfo,
1028                           "Could not alloc adapater memory (DMA)\n");
1029                 goto err_iounmap;
1030         }
1031
1032         /* Init send data structures */
1033         DBG_TRACE(et131x_dbginfo, "Init send data structures...\n");
1034         et131x_init_send(adapter);
1035
1036         /* Register the interrupt
1037          *
1038          * NOTE - This is being done in the open routine, where most other
1039          *         Linux drivers setup IRQ handlers. Make sure device
1040          *         interrupts are not turned on before the IRQ is registered!!
1041          *
1042          *         What we will do here is setup the task structure for the
1043          *         ISR's deferred handler
1044          */
1045         INIT_WORK(&adapter->task, et131x_isr_handler);
1046
1047         /* Determine MAC Address, and copy into the net_device struct */
1048         DBG_TRACE(et131x_dbginfo, "Retrieve MAC address...\n");
1049         et131x_setup_hardware_properties(adapter);
1050
1051         memcpy(netdev->dev_addr, adapter->CurrentAddress, ETH_ALEN);
1052
1053         /* Setup et1310 as per the documentation */
1054         DBG_TRACE(et131x_dbginfo, "Setup the adapter...\n");
1055         et131x_adapter_setup(adapter);
1056
1057         /* Create a timer to count errors received by the NIC */
1058         init_timer(&adapter->ErrorTimer);
1059
1060         adapter->ErrorTimer.expires = jiffies + TX_ERROR_PERIOD * HZ / 1000;
1061         adapter->ErrorTimer.function = et131x_error_timer_handler;
1062         adapter->ErrorTimer.data = (unsigned long)adapter;
1063
1064         /* Initialize link state */
1065         et131x_link_detection_handler((unsigned long)adapter);
1066
1067         /* Intialize variable for counting how long we do not have
1068                                                         link status */
1069         adapter->PoMgmt.TransPhyComaModeOnBoot = 0;
1070
1071         /* We can enable interrupts now
1072          *
1073          *  NOTE - Because registration of interrupt handler is done in the
1074          *         device's open(), defer enabling device interrupts to that
1075          *         point
1076          */
1077
1078         /* Register the net_device struct with the Linux network layer */
1079         DBG_TRACE(et131x_dbginfo, "Registering net_device...\n");
1080         result = register_netdev(netdev);
1081         if (result != 0) {
1082                 DBG_ERROR(et131x_dbginfo, "register_netdev() failed\n");
1083                 goto err_mem_free;
1084         }
1085
1086         /* Register the net_device struct with the PCI subsystem. Save a copy
1087          * of the PCI config space for this device now that the device has
1088          * been initialized, just in case it needs to be quickly restored.
1089          */
1090         pci_set_drvdata(pdev, netdev);
1091
1092         pci_save_state(adapter->pdev);
1093
1094 out:
1095         DBG_LEAVE(et131x_dbginfo);
1096         return result;
1097
1098 err_mem_free:
1099         et131x_adapter_memory_free(adapter);
1100 err_iounmap:
1101         iounmap(adapter->regs);
1102 err_free_dev:
1103         pci_dev_put(pdev);
1104         free_netdev(netdev);
1105 err_release_res:
1106         pci_release_regions(pdev);
1107 err_disable:
1108         pci_disable_device(pdev);
1109         goto out;
1110 }