Merge branch 'devel' into next
[sfrench/cifs-2.6.git] / drivers / net / sfc / tenxpress.c
1 /****************************************************************************
2  * Driver for Solarflare Solarstorm network controllers and boards
3  * Copyright 2007-2008 Solarflare Communications Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published
7  * by the Free Software Foundation, incorporated herein by reference.
8  */
9
10 #include <linux/delay.h>
11 #include <linux/seq_file.h>
12 #include "efx.h"
13 #include "mdio_10g.h"
14 #include "falcon.h"
15 #include "phy.h"
16 #include "falcon_hwdefs.h"
17 #include "boards.h"
18 #include "workarounds.h"
19 #include "selftest.h"
20
21 /* We expect these MMDs to be in the package.  SFT9001 also has a
22  * clause 22 extension MMD, but since it doesn't have all the generic
23  * MMD registers it is pointless to include it here.
24  */
25 #define TENXPRESS_REQUIRED_DEVS (MDIO_MMDREG_DEVS_PMAPMD        | \
26                                  MDIO_MMDREG_DEVS_PCS           | \
27                                  MDIO_MMDREG_DEVS_PHYXS         | \
28                                  MDIO_MMDREG_DEVS_AN)
29
30 #define SFX7101_LOOPBACKS ((1 << LOOPBACK_PHYXS) |      \
31                            (1 << LOOPBACK_PCS) |        \
32                            (1 << LOOPBACK_PMAPMD) |     \
33                            (1 << LOOPBACK_NETWORK))
34
35 #define SFT9001_LOOPBACKS ((1 << LOOPBACK_GPHY) |       \
36                            (1 << LOOPBACK_PHYXS) |      \
37                            (1 << LOOPBACK_PCS) |        \
38                            (1 << LOOPBACK_PMAPMD) |     \
39                            (1 << LOOPBACK_NETWORK))
40
41 /* We complain if we fail to see the link partner as 10G capable this many
42  * times in a row (must be > 1 as sampling the autoneg. registers is racy)
43  */
44 #define MAX_BAD_LP_TRIES        (5)
45
46 /* LASI Control */
47 #define PMA_PMD_LASI_CTRL       36866
48 #define PMA_PMD_LASI_STATUS     36869
49 #define PMA_PMD_LS_ALARM_LBN    0
50 #define PMA_PMD_LS_ALARM_WIDTH  1
51 #define PMA_PMD_TX_ALARM_LBN    1
52 #define PMA_PMD_TX_ALARM_WIDTH  1
53 #define PMA_PMD_RX_ALARM_LBN    2
54 #define PMA_PMD_RX_ALARM_WIDTH  1
55 #define PMA_PMD_AN_ALARM_LBN    3
56 #define PMA_PMD_AN_ALARM_WIDTH  1
57
58 /* Extended control register */
59 #define PMA_PMD_XCONTROL_REG    49152
60 #define PMA_PMD_EXT_GMII_EN_LBN 1
61 #define PMA_PMD_EXT_GMII_EN_WIDTH 1
62 #define PMA_PMD_EXT_CLK_OUT_LBN 2
63 #define PMA_PMD_EXT_CLK_OUT_WIDTH 1
64 #define PMA_PMD_LNPGA_POWERDOWN_LBN 8   /* SFX7101 only */
65 #define PMA_PMD_LNPGA_POWERDOWN_WIDTH 1
66 #define PMA_PMD_EXT_CLK312_LBN  8       /* SFT9001 only */
67 #define PMA_PMD_EXT_CLK312_WIDTH 1
68 #define PMA_PMD_EXT_LPOWER_LBN  12
69 #define PMA_PMD_EXT_LPOWER_WIDTH 1
70 #define PMA_PMD_EXT_SSR_LBN     15
71 #define PMA_PMD_EXT_SSR_WIDTH   1
72
73 /* extended status register */
74 #define PMA_PMD_XSTATUS_REG     49153
75 #define PMA_PMD_XSTAT_FLP_LBN   (12)
76
77 /* LED control register */
78 #define PMA_PMD_LED_CTRL_REG    49159
79 #define PMA_PMA_LED_ACTIVITY_LBN        (3)
80
81 /* LED function override register */
82 #define PMA_PMD_LED_OVERR_REG   49161
83 /* Bit positions for different LEDs (there are more but not wired on SFE4001)*/
84 #define PMA_PMD_LED_LINK_LBN    (0)
85 #define PMA_PMD_LED_SPEED_LBN   (2)
86 #define PMA_PMD_LED_TX_LBN      (4)
87 #define PMA_PMD_LED_RX_LBN      (6)
88 /* Override settings */
89 #define PMA_PMD_LED_AUTO        (0)     /* H/W control */
90 #define PMA_PMD_LED_ON          (1)
91 #define PMA_PMD_LED_OFF         (2)
92 #define PMA_PMD_LED_FLASH       (3)
93 #define PMA_PMD_LED_MASK        3
94 /* All LEDs under hardware control */
95 #define PMA_PMD_LED_FULL_AUTO   (0)
96 /* Green and Amber under hardware control, Red off */
97 #define PMA_PMD_LED_DEFAULT     (PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN)
98
99 #define PMA_PMD_SPEED_ENABLE_REG 49192
100 #define PMA_PMD_100TX_ADV_LBN    1
101 #define PMA_PMD_100TX_ADV_WIDTH  1
102 #define PMA_PMD_1000T_ADV_LBN    2
103 #define PMA_PMD_1000T_ADV_WIDTH  1
104 #define PMA_PMD_10000T_ADV_LBN   3
105 #define PMA_PMD_10000T_ADV_WIDTH 1
106 #define PMA_PMD_SPEED_LBN        4
107 #define PMA_PMD_SPEED_WIDTH      4
108
109 /* Cable diagnostics - SFT9001 only */
110 #define PMA_PMD_CDIAG_CTRL_REG  49213
111 #define CDIAG_CTRL_IMMED_LBN    15
112 #define CDIAG_CTRL_BRK_LINK_LBN 12
113 #define CDIAG_CTRL_IN_PROG_LBN  11
114 #define CDIAG_CTRL_LEN_UNIT_LBN 10
115 #define CDIAG_CTRL_LEN_METRES   1
116 #define PMA_PMD_CDIAG_RES_REG   49174
117 #define CDIAG_RES_A_LBN         12
118 #define CDIAG_RES_B_LBN         8
119 #define CDIAG_RES_C_LBN         4
120 #define CDIAG_RES_D_LBN         0
121 #define CDIAG_RES_WIDTH         4
122 #define CDIAG_RES_OPEN          2
123 #define CDIAG_RES_OK            1
124 #define CDIAG_RES_INVALID       0
125 /* Set of 4 registers for pairs A-D */
126 #define PMA_PMD_CDIAG_LEN_REG   49175
127
128 /* Serdes control registers - SFT9001 only */
129 #define PMA_PMD_CSERDES_CTRL_REG 64258
130 /* Set the 156.25 MHz output to 312.5 MHz to drive Falcon's XMAC */
131 #define PMA_PMD_CSERDES_DEFAULT 0x000f
132
133 /* Misc register defines - SFX7101 only */
134 #define PCS_CLOCK_CTRL_REG      55297
135 #define PLL312_RST_N_LBN 2
136
137 #define PCS_SOFT_RST2_REG       55302
138 #define SERDES_RST_N_LBN 13
139 #define XGXS_RST_N_LBN 12
140
141 #define PCS_TEST_SELECT_REG     55303   /* PRM 10.5.8 */
142 #define CLK312_EN_LBN 3
143
144 /* PHYXS registers */
145 #define PHYXS_XCONTROL_REG      49152
146 #define PHYXS_RESET_LBN         15
147 #define PHYXS_RESET_WIDTH       1
148
149 #define PHYXS_TEST1         (49162)
150 #define LOOPBACK_NEAR_LBN   (8)
151 #define LOOPBACK_NEAR_WIDTH (1)
152
153 #define PCS_10GBASET_STAT1       32
154 #define PCS_10GBASET_BLKLK_LBN   0
155 #define PCS_10GBASET_BLKLK_WIDTH 1
156
157 /* Boot status register */
158 #define PCS_BOOT_STATUS_REG     53248
159 #define PCS_BOOT_FATAL_ERR_LBN  (0)
160 #define PCS_BOOT_PROGRESS_LBN   (1)
161 #define PCS_BOOT_PROGRESS_WIDTH (2)
162 #define PCS_BOOT_COMPLETE_LBN   (3)
163
164 #define PCS_BOOT_MAX_DELAY      (100)
165 #define PCS_BOOT_POLL_DELAY     (10)
166
167 /* 100M/1G PHY registers */
168 #define GPHY_XCONTROL_REG       49152
169 #define GPHY_ISOLATE_LBN        10
170 #define GPHY_ISOLATE_WIDTH      1
171 #define GPHY_DUPLEX_LBN         8
172 #define GPHY_DUPLEX_WIDTH       1
173 #define GPHY_LOOPBACK_NEAR_LBN  14
174 #define GPHY_LOOPBACK_NEAR_WIDTH 1
175
176 #define C22EXT_STATUS_REG       49153
177 #define C22EXT_STATUS_LINK_LBN  2
178 #define C22EXT_STATUS_LINK_WIDTH 1
179
180 #define C22EXT_MSTSLV_REG       49162
181 #define C22EXT_MSTSLV_1000_HD_LBN 10
182 #define C22EXT_MSTSLV_1000_HD_WIDTH 1
183 #define C22EXT_MSTSLV_1000_FD_LBN 11
184 #define C22EXT_MSTSLV_1000_FD_WIDTH 1
185
186 /* Time to wait between powering down the LNPGA and turning off the power
187  * rails */
188 #define LNPGA_PDOWN_WAIT        (HZ / 5)
189
190 static int crc_error_reset_threshold = 100;
191 module_param(crc_error_reset_threshold, int, 0644);
192 MODULE_PARM_DESC(crc_error_reset_threshold,
193                  "Max number of CRC errors before XAUI reset");
194
195 struct tenxpress_phy_data {
196         enum efx_loopback_mode loopback_mode;
197         atomic_t bad_crc_count;
198         enum efx_phy_mode phy_mode;
199         int bad_lp_tries;
200 };
201
202 void tenxpress_crc_err(struct efx_nic *efx)
203 {
204         struct tenxpress_phy_data *phy_data = efx->phy_data;
205         if (phy_data != NULL)
206                 atomic_inc(&phy_data->bad_crc_count);
207 }
208
209 static ssize_t show_phy_short_reach(struct device *dev,
210                                     struct device_attribute *attr, char *buf)
211 {
212         struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
213         int reg;
214
215         reg = mdio_clause45_read(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
216                                  MDIO_PMAPMD_10GBT_TXPWR);
217         return sprintf(buf, "%d\n",
218                        !!(reg & (1 << MDIO_PMAPMD_10GBT_TXPWR_SHORT_LBN)));
219 }
220
221 static ssize_t set_phy_short_reach(struct device *dev,
222                                    struct device_attribute *attr,
223                                    const char *buf, size_t count)
224 {
225         struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
226
227         rtnl_lock();
228         mdio_clause45_set_flag(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
229                                MDIO_PMAPMD_10GBT_TXPWR,
230                                MDIO_PMAPMD_10GBT_TXPWR_SHORT_LBN,
231                                count != 0 && *buf != '0');
232         efx_reconfigure_port(efx);
233         rtnl_unlock();
234
235         return count;
236 }
237
238 static DEVICE_ATTR(phy_short_reach, 0644, show_phy_short_reach,
239                    set_phy_short_reach);
240
241 /* Check that the C166 has booted successfully */
242 static int tenxpress_phy_check(struct efx_nic *efx)
243 {
244         int phy_id = efx->mii.phy_id;
245         int count = PCS_BOOT_MAX_DELAY / PCS_BOOT_POLL_DELAY;
246         int boot_stat;
247
248         /* Wait for the boot to complete (or not) */
249         while (count) {
250                 boot_stat = mdio_clause45_read(efx, phy_id,
251                                                MDIO_MMD_PCS,
252                                                PCS_BOOT_STATUS_REG);
253                 if (boot_stat & (1 << PCS_BOOT_COMPLETE_LBN))
254                         break;
255                 count--;
256                 udelay(PCS_BOOT_POLL_DELAY);
257         }
258
259         if (!count) {
260                 EFX_ERR(efx, "%s: PHY boot timed out. Last status "
261                         "%x\n", __func__,
262                         (boot_stat >> PCS_BOOT_PROGRESS_LBN) &
263                         ((1 << PCS_BOOT_PROGRESS_WIDTH) - 1));
264                 return -ETIMEDOUT;
265         }
266
267         return 0;
268 }
269
270 static int tenxpress_init(struct efx_nic *efx)
271 {
272         int phy_id = efx->mii.phy_id;
273         int reg;
274         int rc;
275
276         if (efx->phy_type == PHY_TYPE_SFX7101) {
277                 /* Enable 312.5 MHz clock */
278                 mdio_clause45_write(efx, phy_id,
279                                     MDIO_MMD_PCS, PCS_TEST_SELECT_REG,
280                                     1 << CLK312_EN_LBN);
281         } else {
282                 /* Enable 312.5 MHz clock and GMII */
283                 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
284                                          PMA_PMD_XCONTROL_REG);
285                 reg |= ((1 << PMA_PMD_EXT_GMII_EN_LBN) |
286                         (1 << PMA_PMD_EXT_CLK_OUT_LBN) |
287                         (1 << PMA_PMD_EXT_CLK312_LBN));
288                 mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
289                                     PMA_PMD_XCONTROL_REG, reg);
290                 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_C22EXT,
291                                        GPHY_XCONTROL_REG, GPHY_ISOLATE_LBN,
292                                        false);
293         }
294
295         rc = tenxpress_phy_check(efx);
296         if (rc < 0)
297                 return rc;
298
299         /* Set the LEDs up as: Green = Link, Amber = Link/Act, Red = Off */
300         if (efx->phy_type == PHY_TYPE_SFX7101) {
301                 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PMAPMD,
302                                        PMA_PMD_LED_CTRL_REG,
303                                        PMA_PMA_LED_ACTIVITY_LBN,
304                                        true);
305                 mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
306                                     PMA_PMD_LED_OVERR_REG, PMA_PMD_LED_DEFAULT);
307         }
308
309         return rc;
310 }
311
312 static int tenxpress_phy_init(struct efx_nic *efx)
313 {
314         struct tenxpress_phy_data *phy_data;
315         int rc = 0;
316
317         phy_data = kzalloc(sizeof(*phy_data), GFP_KERNEL);
318         if (!phy_data)
319                 return -ENOMEM;
320         efx->phy_data = phy_data;
321         phy_data->phy_mode = efx->phy_mode;
322
323         if (!(efx->phy_mode & PHY_MODE_SPECIAL)) {
324                 if (efx->phy_type == PHY_TYPE_SFT9001A) {
325                         int reg;
326                         reg = mdio_clause45_read(efx, efx->mii.phy_id,
327                                                  MDIO_MMD_PMAPMD,
328                                                  PMA_PMD_XCONTROL_REG);
329                         reg |= (1 << PMA_PMD_EXT_SSR_LBN);
330                         mdio_clause45_write(efx, efx->mii.phy_id,
331                                             MDIO_MMD_PMAPMD,
332                                             PMA_PMD_XCONTROL_REG, reg);
333                         mdelay(200);
334                 }
335
336                 rc = mdio_clause45_wait_reset_mmds(efx,
337                                                    TENXPRESS_REQUIRED_DEVS);
338                 if (rc < 0)
339                         goto fail;
340
341                 rc = mdio_clause45_check_mmds(efx, TENXPRESS_REQUIRED_DEVS, 0);
342                 if (rc < 0)
343                         goto fail;
344         }
345
346         rc = tenxpress_init(efx);
347         if (rc < 0)
348                 goto fail;
349
350         if (efx->phy_type == PHY_TYPE_SFT9001B) {
351                 rc = device_create_file(&efx->pci_dev->dev,
352                                         &dev_attr_phy_short_reach);
353                 if (rc)
354                         goto fail;
355         }
356
357         schedule_timeout_uninterruptible(HZ / 5); /* 200ms */
358
359         /* Let XGXS and SerDes out of reset */
360         falcon_reset_xaui(efx);
361
362         return 0;
363
364  fail:
365         kfree(efx->phy_data);
366         efx->phy_data = NULL;
367         return rc;
368 }
369
370 /* Perform a "special software reset" on the PHY. The caller is
371  * responsible for saving and restoring the PHY hardware registers
372  * properly, and masking/unmasking LASI */
373 static int tenxpress_special_reset(struct efx_nic *efx)
374 {
375         int rc, reg;
376
377         /* The XGMAC clock is driven from the SFC7101/SFT9001 312MHz clock, so
378          * a special software reset can glitch the XGMAC sufficiently for stats
379          * requests to fail. Since we don't often special_reset, just lock. */
380         spin_lock(&efx->stats_lock);
381
382         /* Initiate reset */
383         reg = mdio_clause45_read(efx, efx->mii.phy_id,
384                                  MDIO_MMD_PMAPMD, PMA_PMD_XCONTROL_REG);
385         reg |= (1 << PMA_PMD_EXT_SSR_LBN);
386         mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
387                             PMA_PMD_XCONTROL_REG, reg);
388
389         mdelay(200);
390
391         /* Wait for the blocks to come out of reset */
392         rc = mdio_clause45_wait_reset_mmds(efx,
393                                            TENXPRESS_REQUIRED_DEVS);
394         if (rc < 0)
395                 goto unlock;
396
397         /* Try and reconfigure the device */
398         rc = tenxpress_init(efx);
399         if (rc < 0)
400                 goto unlock;
401
402         /* Wait for the XGXS state machine to churn */
403         mdelay(10);
404 unlock:
405         spin_unlock(&efx->stats_lock);
406         return rc;
407 }
408
409 static void sfx7101_check_bad_lp(struct efx_nic *efx, bool link_ok)
410 {
411         struct tenxpress_phy_data *pd = efx->phy_data;
412         int phy_id = efx->mii.phy_id;
413         bool bad_lp;
414         int reg;
415
416         if (link_ok) {
417                 bad_lp = false;
418         } else {
419                 /* Check that AN has started but not completed. */
420                 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_AN,
421                                          MDIO_AN_STATUS);
422                 if (!(reg & (1 << MDIO_AN_STATUS_LP_AN_CAP_LBN)))
423                         return; /* LP status is unknown */
424                 bad_lp = !(reg & (1 << MDIO_AN_STATUS_AN_DONE_LBN));
425                 if (bad_lp)
426                         pd->bad_lp_tries++;
427         }
428
429         /* Nothing to do if all is well and was previously so. */
430         if (!pd->bad_lp_tries)
431                 return;
432
433         /* Use the RX (red) LED as an error indicator once we've seen AN
434          * failure several times in a row, and also log a message. */
435         if (!bad_lp || pd->bad_lp_tries == MAX_BAD_LP_TRIES) {
436                 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
437                                          PMA_PMD_LED_OVERR_REG);
438                 reg &= ~(PMA_PMD_LED_MASK << PMA_PMD_LED_RX_LBN);
439                 if (!bad_lp) {
440                         reg |= PMA_PMD_LED_OFF << PMA_PMD_LED_RX_LBN;
441                 } else {
442                         reg |= PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN;
443                         EFX_ERR(efx, "appears to be plugged into a port"
444                                 " that is not 10GBASE-T capable. The PHY"
445                                 " supports 10GBASE-T ONLY, so no link can"
446                                 " be established\n");
447                 }
448                 mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
449                                     PMA_PMD_LED_OVERR_REG, reg);
450                 pd->bad_lp_tries = bad_lp;
451         }
452 }
453
454 static bool sfx7101_link_ok(struct efx_nic *efx)
455 {
456         return mdio_clause45_links_ok(efx,
457                                       MDIO_MMDREG_DEVS_PMAPMD |
458                                       MDIO_MMDREG_DEVS_PCS |
459                                       MDIO_MMDREG_DEVS_PHYXS);
460 }
461
462 static bool sft9001_link_ok(struct efx_nic *efx, struct ethtool_cmd *ecmd)
463 {
464         int phy_id = efx->mii.phy_id;
465         u32 reg;
466
467         if (efx_phy_mode_disabled(efx->phy_mode))
468                 return false;
469         else if (efx->loopback_mode == LOOPBACK_GPHY)
470                 return true;
471         else if (efx->loopback_mode)
472                 return mdio_clause45_links_ok(efx,
473                                               MDIO_MMDREG_DEVS_PMAPMD |
474                                               MDIO_MMDREG_DEVS_PHYXS);
475
476         /* We must use the same definition of link state as LASI,
477          * otherwise we can miss a link state transition
478          */
479         if (ecmd->speed == 10000) {
480                 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PCS,
481                                          PCS_10GBASET_STAT1);
482                 return reg & (1 << PCS_10GBASET_BLKLK_LBN);
483         } else {
484                 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_C22EXT,
485                                          C22EXT_STATUS_REG);
486                 return reg & (1 << C22EXT_STATUS_LINK_LBN);
487         }
488 }
489
490 static void tenxpress_ext_loopback(struct efx_nic *efx)
491 {
492         int phy_id = efx->mii.phy_id;
493
494         mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_PHYXS,
495                                PHYXS_TEST1, LOOPBACK_NEAR_LBN,
496                                efx->loopback_mode == LOOPBACK_PHYXS);
497         if (efx->phy_type != PHY_TYPE_SFX7101)
498                 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_C22EXT,
499                                        GPHY_XCONTROL_REG,
500                                        GPHY_LOOPBACK_NEAR_LBN,
501                                        efx->loopback_mode == LOOPBACK_GPHY);
502 }
503
504 static void tenxpress_low_power(struct efx_nic *efx)
505 {
506         int phy_id = efx->mii.phy_id;
507
508         if (efx->phy_type == PHY_TYPE_SFX7101)
509                 mdio_clause45_set_mmds_lpower(
510                         efx, !!(efx->phy_mode & PHY_MODE_LOW_POWER),
511                         TENXPRESS_REQUIRED_DEVS);
512         else
513                 mdio_clause45_set_flag(
514                         efx, phy_id, MDIO_MMD_PMAPMD,
515                         PMA_PMD_XCONTROL_REG, PMA_PMD_EXT_LPOWER_LBN,
516                         !!(efx->phy_mode & PHY_MODE_LOW_POWER));
517 }
518
519 static void tenxpress_phy_reconfigure(struct efx_nic *efx)
520 {
521         struct tenxpress_phy_data *phy_data = efx->phy_data;
522         struct ethtool_cmd ecmd;
523         bool phy_mode_change, loop_reset, loop_toggle, loopback;
524
525         if (efx->phy_mode & (PHY_MODE_OFF | PHY_MODE_SPECIAL)) {
526                 phy_data->phy_mode = efx->phy_mode;
527                 return;
528         }
529
530         tenxpress_low_power(efx);
531
532         phy_mode_change = (efx->phy_mode == PHY_MODE_NORMAL &&
533                            phy_data->phy_mode != PHY_MODE_NORMAL);
534         loopback = LOOPBACK_MASK(efx) & efx->phy_op->loopbacks;
535         loop_toggle = LOOPBACK_CHANGED(phy_data, efx, efx->phy_op->loopbacks);
536         loop_reset = (LOOPBACK_OUT_OF(phy_data, efx, efx->phy_op->loopbacks) ||
537                       LOOPBACK_CHANGED(phy_data, efx, 1 << LOOPBACK_GPHY));
538
539         if (loop_reset || loop_toggle || loopback || phy_mode_change) {
540                 int rc;
541
542                 efx->phy_op->get_settings(efx, &ecmd);
543
544                 if (loop_reset || phy_mode_change) {
545                         tenxpress_special_reset(efx);
546
547                         /* Reset XAUI if we were in 10G, and are staying
548                          * in 10G. If we're moving into and out of 10G
549                          * then xaui will be reset anyway */
550                         if (EFX_IS10G(efx))
551                                 falcon_reset_xaui(efx);
552                 }
553
554                 if (efx->phy_type != PHY_TYPE_SFX7101) {
555                         /* Only change autoneg once, on coming out or
556                          * going into loopback */
557                         if (loop_toggle)
558                                 ecmd.autoneg = !loopback;
559                         if (loopback) {
560                                 ecmd.duplex = DUPLEX_FULL;
561                                 if (efx->loopback_mode == LOOPBACK_GPHY)
562                                         ecmd.speed = SPEED_1000;
563                                 else
564                                         ecmd.speed = SPEED_10000;
565                         }
566                 }
567
568                 rc = efx->phy_op->set_settings(efx, &ecmd);
569                 WARN_ON(rc);
570         }
571
572         mdio_clause45_transmit_disable(efx);
573         mdio_clause45_phy_reconfigure(efx);
574         tenxpress_ext_loopback(efx);
575
576         phy_data->loopback_mode = efx->loopback_mode;
577         phy_data->phy_mode = efx->phy_mode;
578
579         if (efx->phy_type == PHY_TYPE_SFX7101) {
580                 efx->link_speed = 10000;
581                 efx->link_fd = true;
582                 efx->link_up = sfx7101_link_ok(efx);
583         } else {
584                 efx->phy_op->get_settings(efx, &ecmd);
585                 efx->link_speed = ecmd.speed;
586                 efx->link_fd = ecmd.duplex == DUPLEX_FULL;
587                 efx->link_up = sft9001_link_ok(efx, &ecmd);
588         }
589         efx->link_fc = mdio_clause45_get_pause(efx);
590 }
591
592 /* Poll PHY for interrupt */
593 static void tenxpress_phy_poll(struct efx_nic *efx)
594 {
595         struct tenxpress_phy_data *phy_data = efx->phy_data;
596         bool change = false, link_ok;
597         unsigned link_fc;
598
599         if (efx->phy_type == PHY_TYPE_SFX7101) {
600                 link_ok = sfx7101_link_ok(efx);
601                 if (link_ok != efx->link_up) {
602                         change = true;
603                 } else {
604                         link_fc = mdio_clause45_get_pause(efx);
605                         if (link_fc != efx->link_fc)
606                                 change = true;
607                 }
608                 sfx7101_check_bad_lp(efx, link_ok);
609         } else if (efx->loopback_mode) {
610                 bool link_ok = sft9001_link_ok(efx, NULL);
611                 if (link_ok != efx->link_up)
612                         change = true;
613         } else {
614                 u32 status = mdio_clause45_read(efx, efx->mii.phy_id,
615                                                 MDIO_MMD_PMAPMD,
616                                                 PMA_PMD_LASI_STATUS);
617                 if (status & (1 << PMA_PMD_LS_ALARM_LBN))
618                         change = true;
619         }
620
621         if (change)
622                 falcon_sim_phy_event(efx);
623
624         if (phy_data->phy_mode != PHY_MODE_NORMAL)
625                 return;
626
627         if (EFX_WORKAROUND_10750(efx) &&
628             atomic_read(&phy_data->bad_crc_count) > crc_error_reset_threshold) {
629                 EFX_ERR(efx, "Resetting XAUI due to too many CRC errors\n");
630                 falcon_reset_xaui(efx);
631                 atomic_set(&phy_data->bad_crc_count, 0);
632         }
633 }
634
635 static void tenxpress_phy_fini(struct efx_nic *efx)
636 {
637         int reg;
638
639         if (efx->phy_type == PHY_TYPE_SFT9001B) {
640                 device_remove_file(&efx->pci_dev->dev,
641                                    &dev_attr_phy_short_reach);
642         } else {
643                 /* Power down the LNPGA */
644                 reg = (1 << PMA_PMD_LNPGA_POWERDOWN_LBN);
645                 mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
646                                     PMA_PMD_XCONTROL_REG, reg);
647
648                 /* Waiting here ensures that the board fini, which can turn
649                  * off the power to the PHY, won't get run until the LNPGA
650                  * powerdown has been given long enough to complete. */
651                 schedule_timeout_uninterruptible(LNPGA_PDOWN_WAIT); /* 200 ms */
652         }
653
654         kfree(efx->phy_data);
655         efx->phy_data = NULL;
656 }
657
658
659 /* Set the RX and TX LEDs and Link LED flashing. The other LEDs
660  * (which probably aren't wired anyway) are left in AUTO mode */
661 void tenxpress_phy_blink(struct efx_nic *efx, bool blink)
662 {
663         int reg;
664
665         if (blink)
666                 reg = (PMA_PMD_LED_FLASH << PMA_PMD_LED_TX_LBN) |
667                         (PMA_PMD_LED_FLASH << PMA_PMD_LED_RX_LBN) |
668                         (PMA_PMD_LED_FLASH << PMA_PMD_LED_LINK_LBN);
669         else
670                 reg = PMA_PMD_LED_DEFAULT;
671
672         mdio_clause45_write(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
673                             PMA_PMD_LED_OVERR_REG, reg);
674 }
675
676 static const char *const sfx7101_test_names[] = {
677         "bist"
678 };
679
680 static int
681 sfx7101_run_tests(struct efx_nic *efx, int *results, unsigned flags)
682 {
683         int rc;
684
685         if (!(flags & ETH_TEST_FL_OFFLINE))
686                 return 0;
687
688         /* BIST is automatically run after a special software reset */
689         rc = tenxpress_special_reset(efx);
690         results[0] = rc ? -1 : 1;
691         return rc;
692 }
693
694 static const char *const sft9001_test_names[] = {
695         "bist",
696         "cable.pairA.status",
697         "cable.pairB.status",
698         "cable.pairC.status",
699         "cable.pairD.status",
700         "cable.pairA.length",
701         "cable.pairB.length",
702         "cable.pairC.length",
703         "cable.pairD.length",
704 };
705
706 static int sft9001_run_tests(struct efx_nic *efx, int *results, unsigned flags)
707 {
708         struct ethtool_cmd ecmd;
709         int phy_id = efx->mii.phy_id;
710         int rc = 0, rc2, i, res_reg;
711
712         if (!(flags & ETH_TEST_FL_OFFLINE))
713                 return 0;
714
715         efx->phy_op->get_settings(efx, &ecmd);
716
717         /* Initialise cable diagnostic results to unknown failure */
718         for (i = 1; i < 9; ++i)
719                 results[i] = -1;
720
721         /* Run cable diagnostics; wait up to 5 seconds for them to complete.
722          * A cable fault is not a self-test failure, but a timeout is. */
723         mdio_clause45_write(efx, phy_id, MDIO_MMD_PMAPMD,
724                             PMA_PMD_CDIAG_CTRL_REG,
725                             (1 << CDIAG_CTRL_IMMED_LBN) |
726                             (1 << CDIAG_CTRL_BRK_LINK_LBN) |
727                             (CDIAG_CTRL_LEN_METRES << CDIAG_CTRL_LEN_UNIT_LBN));
728         i = 0;
729         while (mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
730                                   PMA_PMD_CDIAG_CTRL_REG) &
731                (1 << CDIAG_CTRL_IN_PROG_LBN)) {
732                 if (++i == 50) {
733                         rc = -ETIMEDOUT;
734                         goto reset;
735                 }
736                 msleep(100);
737         }
738         res_reg = mdio_clause45_read(efx, efx->mii.phy_id, MDIO_MMD_PMAPMD,
739                                      PMA_PMD_CDIAG_RES_REG);
740         for (i = 0; i < 4; i++) {
741                 int pair_res =
742                         (res_reg >> (CDIAG_RES_A_LBN - i * CDIAG_RES_WIDTH))
743                         & ((1 << CDIAG_RES_WIDTH) - 1);
744                 int len_reg = mdio_clause45_read(efx, efx->mii.phy_id,
745                                                  MDIO_MMD_PMAPMD,
746                                                  PMA_PMD_CDIAG_LEN_REG + i);
747                 if (pair_res == CDIAG_RES_OK)
748                         results[1 + i] = 1;
749                 else if (pair_res == CDIAG_RES_INVALID)
750                         results[1 + i] = -1;
751                 else
752                         results[1 + i] = -pair_res;
753                 if (pair_res != CDIAG_RES_INVALID &&
754                     pair_res != CDIAG_RES_OPEN &&
755                     len_reg != 0xffff)
756                         results[5 + i] = len_reg;
757         }
758
759         /* We must reset to exit cable diagnostic mode.  The BIST will
760          * also run when we do this. */
761 reset:
762         rc2 = tenxpress_special_reset(efx);
763         results[0] = rc2 ? -1 : 1;
764         if (!rc)
765                 rc = rc2;
766
767         rc2 = efx->phy_op->set_settings(efx, &ecmd);
768         if (!rc)
769                 rc = rc2;
770
771         return rc;
772 }
773
774 static u32 tenxpress_get_xnp_lpa(struct efx_nic *efx)
775 {
776         int phy = efx->mii.phy_id;
777         u32 lpa = 0;
778         int reg;
779
780         if (efx->phy_type != PHY_TYPE_SFX7101) {
781                 reg = mdio_clause45_read(efx, phy, MDIO_MMD_C22EXT,
782                                          C22EXT_MSTSLV_REG);
783                 if (reg & (1 << C22EXT_MSTSLV_1000_HD_LBN))
784                         lpa |= ADVERTISED_1000baseT_Half;
785                 if (reg & (1 << C22EXT_MSTSLV_1000_FD_LBN))
786                         lpa |= ADVERTISED_1000baseT_Full;
787         }
788         reg = mdio_clause45_read(efx, phy, MDIO_MMD_AN, MDIO_AN_10GBT_STATUS);
789         if (reg & (1 << MDIO_AN_10GBT_STATUS_LP_10G_LBN))
790                 lpa |= ADVERTISED_10000baseT_Full;
791         return lpa;
792 }
793
794 static void sfx7101_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
795 {
796         mdio_clause45_get_settings_ext(efx, ecmd, ADVERTISED_10000baseT_Full,
797                                        tenxpress_get_xnp_lpa(efx));
798         ecmd->supported |= SUPPORTED_10000baseT_Full;
799         ecmd->advertising |= ADVERTISED_10000baseT_Full;
800 }
801
802 static void sft9001_get_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
803 {
804         int phy_id = efx->mii.phy_id;
805         u32 xnp_adv = 0;
806         int reg;
807
808         reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_PMAPMD,
809                                  PMA_PMD_SPEED_ENABLE_REG);
810         if (EFX_WORKAROUND_13204(efx) && (reg & (1 << PMA_PMD_100TX_ADV_LBN)))
811                 xnp_adv |= ADVERTISED_100baseT_Full;
812         if (reg & (1 << PMA_PMD_1000T_ADV_LBN))
813                 xnp_adv |= ADVERTISED_1000baseT_Full;
814         if (reg & (1 << PMA_PMD_10000T_ADV_LBN))
815                 xnp_adv |= ADVERTISED_10000baseT_Full;
816
817         mdio_clause45_get_settings_ext(efx, ecmd, xnp_adv,
818                                        tenxpress_get_xnp_lpa(efx));
819
820         ecmd->supported |= (SUPPORTED_100baseT_Half |
821                             SUPPORTED_100baseT_Full |
822                             SUPPORTED_1000baseT_Full);
823
824         /* Use the vendor defined C22ext register for duplex settings */
825         if (ecmd->speed != SPEED_10000 && !ecmd->autoneg) {
826                 reg = mdio_clause45_read(efx, phy_id, MDIO_MMD_C22EXT,
827                                          GPHY_XCONTROL_REG);
828                 ecmd->duplex = (reg & (1 << GPHY_DUPLEX_LBN) ?
829                                 DUPLEX_FULL : DUPLEX_HALF);
830         }
831 }
832
833 static int sft9001_set_settings(struct efx_nic *efx, struct ethtool_cmd *ecmd)
834 {
835         int phy_id = efx->mii.phy_id;
836         int rc;
837
838         rc = mdio_clause45_set_settings(efx, ecmd);
839         if (rc)
840                 return rc;
841
842         if (ecmd->speed != SPEED_10000 && !ecmd->autoneg)
843                 mdio_clause45_set_flag(efx, phy_id, MDIO_MMD_C22EXT,
844                                        GPHY_XCONTROL_REG, GPHY_DUPLEX_LBN,
845                                        ecmd->duplex == DUPLEX_FULL);
846
847         return rc;
848 }
849
850 static bool sft9001_set_xnp_advertise(struct efx_nic *efx, u32 advertising)
851 {
852         int phy = efx->mii.phy_id;
853         int reg = mdio_clause45_read(efx, phy, MDIO_MMD_PMAPMD,
854                                      PMA_PMD_SPEED_ENABLE_REG);
855         bool enabled;
856
857         reg &= ~((1 << 2) | (1 << 3));
858         if (EFX_WORKAROUND_13204(efx) &&
859             (advertising & ADVERTISED_100baseT_Full))
860                 reg |= 1 << PMA_PMD_100TX_ADV_LBN;
861         if (advertising & ADVERTISED_1000baseT_Full)
862                 reg |= 1 << PMA_PMD_1000T_ADV_LBN;
863         if (advertising & ADVERTISED_10000baseT_Full)
864                 reg |= 1 << PMA_PMD_10000T_ADV_LBN;
865         mdio_clause45_write(efx, phy, MDIO_MMD_PMAPMD,
866                             PMA_PMD_SPEED_ENABLE_REG, reg);
867
868         enabled = (advertising &
869                    (ADVERTISED_1000baseT_Half |
870                     ADVERTISED_1000baseT_Full |
871                     ADVERTISED_10000baseT_Full));
872         if (EFX_WORKAROUND_13204(efx))
873                 enabled |= (advertising & ADVERTISED_100baseT_Full);
874         return enabled;
875 }
876
877 struct efx_phy_operations falcon_sfx7101_phy_ops = {
878         .macs             = EFX_XMAC,
879         .init             = tenxpress_phy_init,
880         .reconfigure      = tenxpress_phy_reconfigure,
881         .poll             = tenxpress_phy_poll,
882         .fini             = tenxpress_phy_fini,
883         .clear_interrupt  = efx_port_dummy_op_void,
884         .get_settings     = sfx7101_get_settings,
885         .set_settings     = mdio_clause45_set_settings,
886         .num_tests        = ARRAY_SIZE(sfx7101_test_names),
887         .test_names       = sfx7101_test_names,
888         .run_tests        = sfx7101_run_tests,
889         .mmds             = TENXPRESS_REQUIRED_DEVS,
890         .loopbacks        = SFX7101_LOOPBACKS,
891 };
892
893 struct efx_phy_operations falcon_sft9001_phy_ops = {
894         .macs             = EFX_GMAC | EFX_XMAC,
895         .init             = tenxpress_phy_init,
896         .reconfigure      = tenxpress_phy_reconfigure,
897         .poll             = tenxpress_phy_poll,
898         .fini             = tenxpress_phy_fini,
899         .clear_interrupt  = efx_port_dummy_op_void,
900         .get_settings     = sft9001_get_settings,
901         .set_settings     = sft9001_set_settings,
902         .set_xnp_advertise = sft9001_set_xnp_advertise,
903         .num_tests        = ARRAY_SIZE(sft9001_test_names),
904         .test_names       = sft9001_test_names,
905         .run_tests        = sft9001_run_tests,
906         .mmds             = TENXPRESS_REQUIRED_DEVS,
907         .loopbacks        = SFT9001_LOOPBACKS,
908 };