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