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