ba09063260d973bff94d87e336e9bce676111279
[pandora-kernel.git] / drivers / net / ixgbe / ixgbe_82598.c
1 /*******************************************************************************
2
3   Intel 10 Gigabit PCI Express Linux driver
4   Copyright(c) 1999 - 2007 Intel Corporation.
5
6   This program is free software; you can redistribute it and/or modify it
7   under the terms and conditions of the GNU General Public License,
8   version 2, as published by the Free Software Foundation.
9
10   This program is distributed in the hope it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13   more details.
14
15   You should have received a copy of the GNU General Public License along with
16   this program; if not, write to the Free Software Foundation, Inc.,
17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19   The full GNU General Public License is included in this distribution in
20   the file called "COPYING".
21
22   Contact Information:
23   Linux NICS <linux.nics@intel.com>
24   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27 *******************************************************************************/
28
29 #include <linux/pci.h>
30 #include <linux/delay.h>
31 #include <linux/sched.h>
32
33 #include "ixgbe.h"
34 #include "ixgbe_phy.h"
35
36 #define IXGBE_82598_MAX_TX_QUEUES 32
37 #define IXGBE_82598_MAX_RX_QUEUES 64
38 #define IXGBE_82598_RAR_ENTRIES   16
39 #define IXGBE_82598_MC_TBL_SIZE  128
40 #define IXGBE_82598_VFT_TBL_SIZE 128
41
42 static s32 ixgbe_get_invariants_82598(struct ixgbe_hw *hw);
43 static s32 ixgbe_get_link_settings_82598(struct ixgbe_hw *hw, u32 *speed,
44                                          bool *autoneg);
45 static s32 ixgbe_get_copper_link_settings_82598(struct ixgbe_hw *hw,
46                                                 u32 *speed, bool *autoneg);
47 static enum ixgbe_media_type ixgbe_get_media_type_82598(struct ixgbe_hw *hw);
48 static s32 ixgbe_setup_mac_link_82598(struct ixgbe_hw *hw);
49 static s32 ixgbe_check_mac_link_82598(struct ixgbe_hw *hw, u32 *speed,
50                                       bool *link_up);
51 static s32 ixgbe_setup_mac_link_speed_82598(struct ixgbe_hw *hw, u32 speed,
52                                             bool autoneg,
53                                             bool autoneg_wait_to_complete);
54 static s32 ixgbe_setup_copper_link_82598(struct ixgbe_hw *hw);
55 static s32 ixgbe_setup_copper_link_speed_82598(struct ixgbe_hw *hw, u32 speed,
56                                                bool autoneg,
57                                                bool autoneg_wait_to_complete);
58 static s32 ixgbe_reset_hw_82598(struct ixgbe_hw *hw);
59
60
61 static s32 ixgbe_get_invariants_82598(struct ixgbe_hw *hw)
62 {
63         hw->mac.num_rx_queues = IXGBE_82598_MAX_RX_QUEUES;
64         hw->mac.num_tx_queues = IXGBE_82598_MAX_TX_QUEUES;
65         hw->mac.mcft_size = IXGBE_82598_MC_TBL_SIZE;
66         hw->mac.vft_size = IXGBE_82598_VFT_TBL_SIZE;
67         hw->mac.num_rar_entries = IXGBE_82598_RAR_ENTRIES;
68
69         /* PHY ops are filled in by default properly for Fiber only */
70         if (hw->mac.ops.get_media_type(hw) == ixgbe_media_type_copper) {
71                 hw->mac.ops.setup_link = &ixgbe_setup_copper_link_82598;
72                 hw->mac.ops.setup_link_speed = &ixgbe_setup_copper_link_speed_82598;
73                 hw->mac.ops.get_link_settings =
74                                 &ixgbe_get_copper_link_settings_82598;
75
76                 /* Call PHY identify routine to get the phy type */
77                 ixgbe_identify_phy(hw);
78
79                 switch (hw->phy.type) {
80                 case ixgbe_phy_tn:
81                         hw->phy.ops.setup_link = &ixgbe_setup_tnx_phy_link;
82                         hw->phy.ops.check_link = &ixgbe_check_tnx_phy_link;
83                         hw->phy.ops.setup_link_speed =
84                                         &ixgbe_setup_tnx_phy_link_speed;
85                         break;
86                 default:
87                         break;
88                 }
89         }
90
91         return 0;
92 }
93
94 /**
95  *  ixgbe_get_link_settings_82598 - Determines default link settings
96  *  @hw: pointer to hardware structure
97  *  @speed: pointer to link speed
98  *  @autoneg: boolean auto-negotiation value
99  *
100  *  Determines the default link settings by reading the AUTOC register.
101  **/
102 static s32 ixgbe_get_link_settings_82598(struct ixgbe_hw *hw, u32 *speed,
103                                          bool *autoneg)
104 {
105         s32 status = 0;
106         s32 autoc_reg;
107
108         autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
109
110         if (hw->mac.link_settings_loaded) {
111                 autoc_reg &= ~IXGBE_AUTOC_LMS_ATTACH_TYPE;
112                 autoc_reg &= ~IXGBE_AUTOC_LMS_MASK;
113                 autoc_reg |= hw->mac.link_attach_type;
114                 autoc_reg |= hw->mac.link_mode_select;
115         }
116
117         switch (autoc_reg & IXGBE_AUTOC_LMS_MASK) {
118         case IXGBE_AUTOC_LMS_1G_LINK_NO_AN:
119                 *speed = IXGBE_LINK_SPEED_1GB_FULL;
120                 *autoneg = false;
121                 break;
122
123         case IXGBE_AUTOC_LMS_10G_LINK_NO_AN:
124                 *speed = IXGBE_LINK_SPEED_10GB_FULL;
125                 *autoneg = false;
126                 break;
127
128         case IXGBE_AUTOC_LMS_1G_AN:
129                 *speed = IXGBE_LINK_SPEED_1GB_FULL;
130                 *autoneg = true;
131                 break;
132
133         case IXGBE_AUTOC_LMS_KX4_AN:
134         case IXGBE_AUTOC_LMS_KX4_AN_1G_AN:
135                 *speed = IXGBE_LINK_SPEED_UNKNOWN;
136                 if (autoc_reg & IXGBE_AUTOC_KX4_SUPP)
137                         *speed |= IXGBE_LINK_SPEED_10GB_FULL;
138                 if (autoc_reg & IXGBE_AUTOC_KX_SUPP)
139                         *speed |= IXGBE_LINK_SPEED_1GB_FULL;
140                 *autoneg = true;
141                 break;
142
143         default:
144                 status = IXGBE_ERR_LINK_SETUP;
145                 break;
146         }
147
148         return status;
149 }
150
151 /**
152  *  ixgbe_get_copper_link_settings_82598 - Determines default link settings
153  *  @hw: pointer to hardware structure
154  *  @speed: pointer to link speed
155  *  @autoneg: boolean auto-negotiation value
156  *
157  *  Determines the default link settings by reading the AUTOC register.
158  **/
159 static s32 ixgbe_get_copper_link_settings_82598(struct ixgbe_hw *hw,
160                                                 u32 *speed, bool *autoneg)
161 {
162         s32 status = IXGBE_ERR_LINK_SETUP;
163         u16 speed_ability;
164
165         *speed = 0;
166         *autoneg = true;
167
168         status = ixgbe_read_phy_reg(hw, IXGBE_MDIO_PHY_SPEED_ABILITY,
169                                     IXGBE_MDIO_PMA_PMD_DEV_TYPE,
170                                     &speed_ability);
171
172         if (status == 0) {
173                 if (speed_ability & IXGBE_MDIO_PHY_SPEED_10G)
174                     *speed |= IXGBE_LINK_SPEED_10GB_FULL;
175                 if (speed_ability & IXGBE_MDIO_PHY_SPEED_1G)
176                     *speed |= IXGBE_LINK_SPEED_1GB_FULL;
177         }
178
179         return status;
180 }
181
182 /**
183  *  ixgbe_get_media_type_82598 - Determines media type
184  *  @hw: pointer to hardware structure
185  *
186  *  Returns the media type (fiber, copper, backplane)
187  **/
188 static enum ixgbe_media_type ixgbe_get_media_type_82598(struct ixgbe_hw *hw)
189 {
190         enum ixgbe_media_type media_type;
191
192         /* Media type for I82598 is based on device ID */
193         switch (hw->device_id) {
194         case IXGBE_DEV_ID_82598AF_DUAL_PORT:
195         case IXGBE_DEV_ID_82598AF_SINGLE_PORT:
196         case IXGBE_DEV_ID_82598EB_CX4:
197         case IXGBE_DEV_ID_82598_CX4_DUAL_PORT:
198                 media_type = ixgbe_media_type_fiber;
199                 break;
200         case IXGBE_DEV_ID_82598AT_DUAL_PORT:
201                 media_type = ixgbe_media_type_copper;
202                 break;
203         default:
204                 media_type = ixgbe_media_type_unknown;
205                 break;
206         }
207
208         return media_type;
209 }
210
211 /**
212  *  ixgbe_setup_mac_link_82598 - Configures MAC link settings
213  *  @hw: pointer to hardware structure
214  *
215  *  Configures link settings based on values in the ixgbe_hw struct.
216  *  Restarts the link.  Performs autonegotiation if needed.
217  **/
218 static s32 ixgbe_setup_mac_link_82598(struct ixgbe_hw *hw)
219 {
220         u32 autoc_reg;
221         u32 links_reg;
222         u32 i;
223         s32 status = 0;
224
225         autoc_reg = IXGBE_READ_REG(hw, IXGBE_AUTOC);
226
227         if (hw->mac.link_settings_loaded) {
228                 autoc_reg &= ~IXGBE_AUTOC_LMS_ATTACH_TYPE;
229                 autoc_reg &= ~IXGBE_AUTOC_LMS_MASK;
230                 autoc_reg |= hw->mac.link_attach_type;
231                 autoc_reg |= hw->mac.link_mode_select;
232
233                 IXGBE_WRITE_REG(hw, IXGBE_AUTOC, autoc_reg);
234                 IXGBE_WRITE_FLUSH(hw);
235                 msleep(50);
236         }
237
238         /* Restart link */
239         autoc_reg |= IXGBE_AUTOC_AN_RESTART;
240         IXGBE_WRITE_REG(hw, IXGBE_AUTOC, autoc_reg);
241
242         /* Only poll for autoneg to complete if specified to do so */
243         if (hw->phy.autoneg_wait_to_complete) {
244                 if (hw->mac.link_mode_select == IXGBE_AUTOC_LMS_KX4_AN ||
245                     hw->mac.link_mode_select == IXGBE_AUTOC_LMS_KX4_AN_1G_AN) {
246                         links_reg = 0; /* Just in case Autoneg time = 0 */
247                         for (i = 0; i < IXGBE_AUTO_NEG_TIME; i++) {
248                                 links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
249                                 if (links_reg & IXGBE_LINKS_KX_AN_COMP)
250                                         break;
251                                 msleep(100);
252                         }
253                         if (!(links_reg & IXGBE_LINKS_KX_AN_COMP)) {
254                                 status = IXGBE_ERR_AUTONEG_NOT_COMPLETE;
255                                 hw_dbg(hw,
256                                        "Autonegotiation did not complete.\n");
257                         }
258                 }
259         }
260
261         /*
262          * We want to save off the original Flow Control configuration just in
263          * case we get disconnected and then reconnected into a different hub
264          * or switch with different Flow Control capabilities.
265          */
266         hw->fc.type = hw->fc.original_type;
267         ixgbe_setup_fc(hw, 0);
268
269         /* Add delay to filter out noises during initial link setup */
270         msleep(50);
271
272         return status;
273 }
274
275 /**
276  *  ixgbe_check_mac_link_82598 - Get link/speed status
277  *  @hw: pointer to hardware structure
278  *  @speed: pointer to link speed
279  *  @link_up: true is link is up, false otherwise
280  *
281  *  Reads the links register to determine if link is up and the current speed
282  **/
283 static s32 ixgbe_check_mac_link_82598(struct ixgbe_hw *hw, u32 *speed,
284                                       bool *link_up)
285 {
286         u32 links_reg;
287
288         links_reg = IXGBE_READ_REG(hw, IXGBE_LINKS);
289
290         if (links_reg & IXGBE_LINKS_UP)
291                 *link_up = true;
292         else
293                 *link_up = false;
294
295         if (links_reg & IXGBE_LINKS_SPEED)
296                 *speed = IXGBE_LINK_SPEED_10GB_FULL;
297         else
298                 *speed = IXGBE_LINK_SPEED_1GB_FULL;
299
300         return 0;
301 }
302
303 /**
304  *  ixgbe_setup_mac_link_speed_82598 - Set MAC link speed
305  *  @hw: pointer to hardware structure
306  *  @speed: new link speed
307  *  @autoneg: true if auto-negotiation enabled
308  *  @autoneg_wait_to_complete: true if waiting is needed to complete
309  *
310  *  Set the link speed in the AUTOC register and restarts link.
311  **/
312 static s32 ixgbe_setup_mac_link_speed_82598(struct ixgbe_hw *hw,
313                                             u32 speed, bool autoneg,
314                                             bool autoneg_wait_to_complete)
315 {
316         s32 status = 0;
317
318         /* If speed is 10G, then check for CX4 or XAUI. */
319         if ((speed == IXGBE_LINK_SPEED_10GB_FULL) &&
320             (!(hw->mac.link_attach_type & IXGBE_AUTOC_10G_KX4)))
321                 hw->mac.link_mode_select = IXGBE_AUTOC_LMS_10G_LINK_NO_AN;
322         else if ((speed == IXGBE_LINK_SPEED_1GB_FULL) && (!autoneg))
323                 hw->mac.link_mode_select = IXGBE_AUTOC_LMS_1G_LINK_NO_AN;
324         else if (autoneg) {
325                 /* BX mode - Autonegotiate 1G */
326                 if (!(hw->mac.link_attach_type & IXGBE_AUTOC_1G_PMA_PMD))
327                         hw->mac.link_mode_select = IXGBE_AUTOC_LMS_1G_AN;
328                 else /* KX/KX4 mode */
329                         hw->mac.link_mode_select = IXGBE_AUTOC_LMS_KX4_AN_1G_AN;
330         } else {
331                 status = IXGBE_ERR_LINK_SETUP;
332         }
333
334         if (status == 0) {
335                 hw->phy.autoneg_wait_to_complete = autoneg_wait_to_complete;
336
337                 hw->mac.link_settings_loaded = true;
338                 /*
339                  * Setup and restart the link based on the new values in
340                  * ixgbe_hw This will write the AUTOC register based on the new
341                  * stored values
342                  */
343                 hw->mac.ops.setup_link(hw);
344         }
345
346         return status;
347 }
348
349
350 /**
351  *  ixgbe_setup_copper_link_82598 - Setup copper link settings
352  *  @hw: pointer to hardware structure
353  *
354  *  Configures link settings based on values in the ixgbe_hw struct.
355  *  Restarts the link.  Performs autonegotiation if needed.  Restart
356  *  phy and wait for autonegotiate to finish.  Then synchronize the
357  *  MAC and PHY.
358  **/
359 static s32 ixgbe_setup_copper_link_82598(struct ixgbe_hw *hw)
360 {
361         s32 status = 0;
362
363         /* Restart autonegotiation on PHY */
364         if (hw->phy.ops.setup_link)
365                 status = hw->phy.ops.setup_link(hw);
366
367         /* Set MAC to KX/KX4 autoneg, which defaultis to Parallel detection */
368         hw->mac.link_attach_type = (IXGBE_AUTOC_10G_KX4 | IXGBE_AUTOC_1G_KX);
369         hw->mac.link_mode_select = IXGBE_AUTOC_LMS_KX4_AN;
370
371         /* Set up MAC */
372         hw->mac.ops.setup_link(hw);
373
374         return status;
375 }
376
377 /**
378  *  ixgbe_setup_copper_link_speed_82598 - Set the PHY autoneg advertised field
379  *  @hw: pointer to hardware structure
380  *  @speed: new link speed
381  *  @autoneg: true if autonegotiation enabled
382  *  @autoneg_wait_to_complete: true if waiting is needed to complete
383  *
384  *  Sets the link speed in the AUTOC register in the MAC and restarts link.
385  **/
386 static s32 ixgbe_setup_copper_link_speed_82598(struct ixgbe_hw *hw, u32 speed,
387                                                bool autoneg,
388                                                bool autoneg_wait_to_complete)
389 {
390         s32 status = 0;
391
392         /* Setup the PHY according to input speed */
393         if (hw->phy.ops.setup_link_speed)
394                 status = hw->phy.ops.setup_link_speed(hw, speed, autoneg,
395                                                 autoneg_wait_to_complete);
396
397         /* Set MAC to KX/KX4 autoneg, which defaults to Parallel detection */
398         hw->mac.link_attach_type = (IXGBE_AUTOC_10G_KX4 | IXGBE_AUTOC_1G_KX);
399         hw->mac.link_mode_select = IXGBE_AUTOC_LMS_KX4_AN;
400
401         /* Set up MAC */
402         hw->mac.ops.setup_link(hw);
403
404         return status;
405 }
406
407 /**
408  *  ixgbe_reset_hw_82598 - Performs hardware reset
409  *  @hw: pointer to hardware structure
410  *
411  *  Resets the hardware by reseting the transmit and receive units, masks and
412  *  clears all interrupts, performing a PHY reset, and performing a link (MAC)
413  *  reset.
414  **/
415 static s32 ixgbe_reset_hw_82598(struct ixgbe_hw *hw)
416 {
417         s32 status = 0;
418         u32 ctrl;
419         u32 gheccr;
420         u32 i;
421         u32 autoc;
422         u8  analog_val;
423
424         /* Call adapter stop to disable tx/rx and clear interrupts */
425         ixgbe_stop_adapter(hw);
426
427         /*
428          * Power up the Atlas TX lanes if they are currently powered down.
429          * Atlas TX lanes are powered down for MAC loopback tests, but
430          * they are not automatically restored on reset.
431          */
432         ixgbe_read_analog_reg8(hw, IXGBE_ATLAS_PDN_LPBK, &analog_val);
433         if (analog_val & IXGBE_ATLAS_PDN_TX_REG_EN) {
434                 /* Enable TX Atlas so packets can be transmitted again */
435                 ixgbe_read_analog_reg8(hw, IXGBE_ATLAS_PDN_LPBK, &analog_val);
436                 analog_val &= ~IXGBE_ATLAS_PDN_TX_REG_EN;
437                 ixgbe_write_analog_reg8(hw, IXGBE_ATLAS_PDN_LPBK, analog_val);
438
439                 ixgbe_read_analog_reg8(hw, IXGBE_ATLAS_PDN_10G, &analog_val);
440                 analog_val &= ~IXGBE_ATLAS_PDN_TX_10G_QL_ALL;
441                 ixgbe_write_analog_reg8(hw, IXGBE_ATLAS_PDN_10G, analog_val);
442
443                 ixgbe_read_analog_reg8(hw, IXGBE_ATLAS_PDN_1G, &analog_val);
444                 analog_val &= ~IXGBE_ATLAS_PDN_TX_1G_QL_ALL;
445                 ixgbe_write_analog_reg8(hw, IXGBE_ATLAS_PDN_1G, analog_val);
446
447                 ixgbe_read_analog_reg8(hw, IXGBE_ATLAS_PDN_AN, &analog_val);
448                 analog_val &= ~IXGBE_ATLAS_PDN_TX_AN_QL_ALL;
449                 ixgbe_write_analog_reg8(hw, IXGBE_ATLAS_PDN_AN, analog_val);
450         }
451
452         /* Reset PHY */
453         ixgbe_reset_phy(hw);
454
455         /*
456          * Prevent the PCI-E bus from from hanging by disabling PCI-E master
457          * access and verify no pending requests before reset
458          */
459         if (ixgbe_disable_pcie_master(hw) != 0) {
460                 status = IXGBE_ERR_MASTER_REQUESTS_PENDING;
461                 hw_dbg(hw, "PCI-E Master disable polling has failed.\n");
462         }
463
464         /*
465          * Issue global reset to the MAC.  This needs to be a SW reset.
466          * If link reset is used, it might reset the MAC when mng is using it
467          */
468         ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL);
469         IXGBE_WRITE_REG(hw, IXGBE_CTRL, (ctrl | IXGBE_CTRL_RST));
470         IXGBE_WRITE_FLUSH(hw);
471
472         /* Poll for reset bit to self-clear indicating reset is complete */
473         for (i = 0; i < 10; i++) {
474                 udelay(1);
475                 ctrl = IXGBE_READ_REG(hw, IXGBE_CTRL);
476                 if (!(ctrl & IXGBE_CTRL_RST))
477                         break;
478         }
479         if (ctrl & IXGBE_CTRL_RST) {
480                 status = IXGBE_ERR_RESET_FAILED;
481                 hw_dbg(hw, "Reset polling failed to complete.\n");
482         }
483
484         msleep(50);
485
486         gheccr = IXGBE_READ_REG(hw, IXGBE_GHECCR);
487         gheccr &= ~((1 << 21) | (1 << 18) | (1 << 9) | (1 << 6));
488         IXGBE_WRITE_REG(hw, IXGBE_GHECCR, gheccr);
489
490         /*
491          * AUTOC register which stores link settings gets cleared
492          * and reloaded from EEPROM after reset. We need to restore
493          * our stored value from init in case SW changed the attach
494          * type or speed.  If this is the first time and link settings
495          * have not been stored, store default settings from AUTOC.
496          */
497         autoc = IXGBE_READ_REG(hw, IXGBE_AUTOC);
498         if (hw->mac.link_settings_loaded) {
499                 autoc &= ~(IXGBE_AUTOC_LMS_ATTACH_TYPE);
500                 autoc &= ~(IXGBE_AUTOC_LMS_MASK);
501                 autoc |= hw->mac.link_attach_type;
502                 autoc |= hw->mac.link_mode_select;
503                 IXGBE_WRITE_REG(hw, IXGBE_AUTOC, autoc);
504         } else {
505                 hw->mac.link_attach_type =
506                                          (autoc & IXGBE_AUTOC_LMS_ATTACH_TYPE);
507                 hw->mac.link_mode_select = (autoc & IXGBE_AUTOC_LMS_MASK);
508                 hw->mac.link_settings_loaded = true;
509         }
510
511         /* Store the permanent mac address */
512         ixgbe_get_mac_addr(hw, hw->mac.perm_addr);
513
514         return status;
515 }
516
517 static struct ixgbe_mac_operations mac_ops_82598 = {
518         .reset                  = &ixgbe_reset_hw_82598,
519         .get_media_type         = &ixgbe_get_media_type_82598,
520         .setup_link             = &ixgbe_setup_mac_link_82598,
521         .check_link             = &ixgbe_check_mac_link_82598,
522         .setup_link_speed       = &ixgbe_setup_mac_link_speed_82598,
523         .get_link_settings      = &ixgbe_get_link_settings_82598,
524 };
525
526 struct ixgbe_info ixgbe_82598_info = {
527         .mac                    = ixgbe_mac_82598EB,
528         .get_invariants         = &ixgbe_get_invariants_82598,
529         .mac_ops                = &mac_ops_82598,
530 };
531