2 * Copyright (C) 2003 - 2009 NetXen, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * The full GNU General Public License is included in this distribution
21 * in the file called LICENSE.
23 * Contact Information:
27 * Cupertino, CA 95014-0701
31 #include "netxen_nic.h"
33 #define NETXEN_GB_MAC_SOFT_RESET 0x80000000
34 #define NETXEN_GB_MAC_RESET_PROT_BLK 0x000F0000
35 #define NETXEN_GB_MAC_ENABLE_TX_RX 0x00000005
36 #define NETXEN_GB_MAC_PAUSED_FRMS 0x00000020
38 static long phy_lock_timeout
= 100000000;
40 static int phy_lock(struct netxen_adapter
*adapter
)
43 int done
= 0, timeout
= 0;
46 done
= netxen_nic_reg_read(adapter
,
47 NETXEN_PCIE_REG(PCIE_SEM3_LOCK
));
50 if (timeout
>= phy_lock_timeout
) {
57 for (i
= 0; i
< 20; i
++)
62 netxen_crb_writelit_adapter(adapter
,
63 NETXEN_PHY_LOCK_ID
, PHY_LOCK_DRIVER
);
67 static int phy_unlock(struct netxen_adapter
*adapter
)
69 adapter
->pci_read_immediate(adapter
, NETXEN_PCIE_REG(PCIE_SEM3_UNLOCK
));
75 * netxen_niu_gbe_phy_read - read a register from the GbE PHY via
76 * mii management interface.
78 * Note: The MII management interface goes through port 0.
79 * Individual phys are addressed as follows:
80 * @param phy [15:8] phy id
81 * @param reg [7:0] register number
83 * @returns 0 on success
87 int netxen_niu_gbe_phy_read(struct netxen_adapter
*adapter
, long reg
,
93 long phy
= adapter
->physical_port
;
99 if (phy_lock(adapter
) != 0) {
104 * MII mgmt all goes through port 0 MAC interface,
105 * so it cannot be in reset
108 if (adapter
->hw_read_wx(adapter
, NETXEN_NIU_GB_MAC_CONFIG_0(0),
111 if (netxen_gb_get_soft_reset(mac_cfg0
)) {
114 netxen_gb_tx_reset_pb(temp
);
115 netxen_gb_rx_reset_pb(temp
);
116 netxen_gb_tx_reset_mac(temp
);
117 netxen_gb_rx_reset_mac(temp
);
118 if (adapter
->hw_write_wx(adapter
,
119 NETXEN_NIU_GB_MAC_CONFIG_0(0),
126 netxen_gb_mii_mgmt_reg_addr(address
, reg
);
127 netxen_gb_mii_mgmt_phy_addr(address
, phy
);
128 if (adapter
->hw_write_wx(adapter
, NETXEN_NIU_GB_MII_MGMT_ADDR(0),
131 command
= 0; /* turn off any prior activity */
132 if (adapter
->hw_write_wx(adapter
, NETXEN_NIU_GB_MII_MGMT_COMMAND(0),
135 /* send read command */
136 netxen_gb_mii_mgmt_set_read_cycle(command
);
137 if (adapter
->hw_write_wx(adapter
, NETXEN_NIU_GB_MII_MGMT_COMMAND(0),
143 if (adapter
->hw_read_wx(adapter
,
144 NETXEN_NIU_GB_MII_MGMT_INDICATE(0),
148 } while ((netxen_get_gb_mii_mgmt_busy(status
)
149 || netxen_get_gb_mii_mgmt_notvalid(status
))
150 && (timeout
++ < NETXEN_NIU_PHY_WAITMAX
));
152 if (timeout
< NETXEN_NIU_PHY_WAITMAX
) {
153 if (adapter
->hw_read_wx(adapter
,
154 NETXEN_NIU_GB_MII_MGMT_STATUS(0),
162 if (adapter
->hw_write_wx(adapter
,
163 NETXEN_NIU_GB_MAC_CONFIG_0(0),
171 * netxen_niu_gbe_phy_write - write a register to the GbE PHY via
172 * mii management interface.
174 * Note: The MII management interface goes through port 0.
175 * Individual phys are addressed as follows:
176 * @param phy [15:8] phy id
177 * @param reg [7:0] register number
179 * @returns 0 on success
183 int netxen_niu_gbe_phy_write(struct netxen_adapter
*adapter
, long reg
,
189 long phy
= adapter
->physical_port
;
196 * MII mgmt all goes through port 0 MAC interface, so it
200 if (adapter
->hw_read_wx(adapter
, NETXEN_NIU_GB_MAC_CONFIG_0(0),
203 if (netxen_gb_get_soft_reset(mac_cfg0
)) {
206 netxen_gb_tx_reset_pb(temp
);
207 netxen_gb_rx_reset_pb(temp
);
208 netxen_gb_tx_reset_mac(temp
);
209 netxen_gb_rx_reset_mac(temp
);
211 if (adapter
->hw_write_wx(adapter
,
212 NETXEN_NIU_GB_MAC_CONFIG_0(0),
218 command
= 0; /* turn off any prior activity */
219 if (adapter
->hw_write_wx(adapter
, NETXEN_NIU_GB_MII_MGMT_COMMAND(0),
224 netxen_gb_mii_mgmt_reg_addr(address
, reg
);
225 netxen_gb_mii_mgmt_phy_addr(address
, phy
);
226 if (adapter
->hw_write_wx(adapter
, NETXEN_NIU_GB_MII_MGMT_ADDR(0),
230 if (adapter
->hw_write_wx(adapter
, NETXEN_NIU_GB_MII_MGMT_CTRL(0),
236 if (adapter
->hw_read_wx(adapter
,
237 NETXEN_NIU_GB_MII_MGMT_INDICATE(0),
241 } while ((netxen_get_gb_mii_mgmt_busy(status
))
242 && (timeout
++ < NETXEN_NIU_PHY_WAITMAX
));
244 if (timeout
< NETXEN_NIU_PHY_WAITMAX
)
249 /* restore the state of port 0 MAC in case we tampered with it */
251 if (adapter
->hw_write_wx(adapter
,
252 NETXEN_NIU_GB_MAC_CONFIG_0(0),
259 int netxen_niu_xgbe_enable_phy_interrupts(struct netxen_adapter
*adapter
)
261 netxen_crb_writelit_adapter(adapter
, NETXEN_NIU_INT_MASK
, 0x3f);
265 int netxen_niu_gbe_enable_phy_interrupts(struct netxen_adapter
*adapter
)
269 netxen_set_phy_int_link_status_changed(enable
);
270 netxen_set_phy_int_autoneg_completed(enable
);
271 netxen_set_phy_int_speed_changed(enable
);
274 netxen_niu_gbe_phy_write(adapter
,
275 NETXEN_NIU_GB_MII_MGMT_ADDR_INT_ENABLE
,
282 int netxen_niu_xgbe_disable_phy_interrupts(struct netxen_adapter
*adapter
)
284 netxen_crb_writelit_adapter(adapter
, NETXEN_NIU_INT_MASK
, 0x7f);
288 int netxen_niu_gbe_disable_phy_interrupts(struct netxen_adapter
*adapter
)
292 netxen_niu_gbe_phy_write(adapter
,
293 NETXEN_NIU_GB_MII_MGMT_ADDR_INT_ENABLE
, 0))
299 static int netxen_niu_gbe_clear_phy_interrupts(struct netxen_adapter
*adapter
)
303 netxen_niu_gbe_phy_write(adapter
,
304 NETXEN_NIU_GB_MII_MGMT_ADDR_INT_STATUS
,
312 * netxen_niu_gbe_set_mii_mode- Set 10/100 Mbit Mode for GbE MAC
315 static void netxen_niu_gbe_set_mii_mode(struct netxen_adapter
*adapter
,
316 int port
, long enable
)
318 netxen_crb_writelit_adapter(adapter
, NETXEN_NIU_MODE
, 0x2);
319 netxen_crb_writelit_adapter(adapter
, NETXEN_NIU_GB_MAC_CONFIG_0(port
),
321 netxen_crb_writelit_adapter(adapter
, NETXEN_NIU_GB_MAC_CONFIG_0(port
),
323 netxen_crb_writelit_adapter(adapter
, NETXEN_NIU_GB_MAC_CONFIG_1(port
),
325 netxen_crb_writelit_adapter(adapter
,
326 NETXEN_NIU_GB0_GMII_MODE
+ (port
<< 3), 0);
327 netxen_crb_writelit_adapter(adapter
,
328 NETXEN_NIU_GB0_MII_MODE
+ (port
<< 3), 1);
329 netxen_crb_writelit_adapter(adapter
,
330 (NETXEN_NIU_GB0_HALF_DUPLEX
+ port
* 4), 0);
331 netxen_crb_writelit_adapter(adapter
,
332 NETXEN_NIU_GB_MII_MGMT_CONFIG(port
), 0x7);
336 * Do NOT enable flow control until a suitable solution for
337 * shutting down pause frames is found.
339 netxen_crb_writelit_adapter(adapter
,
340 NETXEN_NIU_GB_MAC_CONFIG_0(port
),
344 if (netxen_niu_gbe_enable_phy_interrupts(adapter
))
345 printk(KERN_ERR PFX
"ERROR enabling PHY interrupts\n");
346 if (netxen_niu_gbe_clear_phy_interrupts(adapter
))
347 printk(KERN_ERR PFX
"ERROR clearing PHY interrupts\n");
351 * netxen_niu_gbe_set_gmii_mode- Set GbE Mode for GbE MAC
353 static void netxen_niu_gbe_set_gmii_mode(struct netxen_adapter
*adapter
,
354 int port
, long enable
)
356 netxen_crb_writelit_adapter(adapter
, NETXEN_NIU_MODE
, 0x2);
357 netxen_crb_writelit_adapter(adapter
, NETXEN_NIU_GB_MAC_CONFIG_0(port
),
359 netxen_crb_writelit_adapter(adapter
, NETXEN_NIU_GB_MAC_CONFIG_0(port
),
361 netxen_crb_writelit_adapter(adapter
, NETXEN_NIU_GB_MAC_CONFIG_1(port
),
363 netxen_crb_writelit_adapter(adapter
,
364 NETXEN_NIU_GB0_MII_MODE
+ (port
<< 3), 0);
365 netxen_crb_writelit_adapter(adapter
,
366 NETXEN_NIU_GB0_GMII_MODE
+ (port
<< 3), 1);
367 netxen_crb_writelit_adapter(adapter
,
368 (NETXEN_NIU_GB0_HALF_DUPLEX
+ port
* 4), 0);
369 netxen_crb_writelit_adapter(adapter
,
370 NETXEN_NIU_GB_MII_MGMT_CONFIG(port
), 0x7);
374 * Do NOT enable flow control until a suitable solution for
375 * shutting down pause frames is found.
377 netxen_crb_writelit_adapter(adapter
,
378 NETXEN_NIU_GB_MAC_CONFIG_0(port
),
382 if (netxen_niu_gbe_enable_phy_interrupts(adapter
))
383 printk(KERN_ERR PFX
"ERROR enabling PHY interrupts\n");
384 if (netxen_niu_gbe_clear_phy_interrupts(adapter
))
385 printk(KERN_ERR PFX
"ERROR clearing PHY interrupts\n");
388 int netxen_niu_gbe_init_port(struct netxen_adapter
*adapter
, int port
)
393 if (NX_IS_REVISION_P3(adapter
->ahw
.revision_id
))
396 if (adapter
->disable_phy_interrupts
)
397 adapter
->disable_phy_interrupts(adapter
);
400 if (0 == netxen_niu_gbe_phy_read(adapter
,
401 NETXEN_NIU_GB_MII_MGMT_ADDR_PHY_STATUS
, &status
)) {
402 if (netxen_get_phy_link(status
)) {
403 if (netxen_get_phy_speed(status
) == 2) {
404 netxen_niu_gbe_set_gmii_mode(adapter
, port
, 1);
405 } else if ((netxen_get_phy_speed(status
) == 1)
406 || (netxen_get_phy_speed(status
) == 0)) {
407 netxen_niu_gbe_set_mii_mode(adapter
, port
, 1);
414 * We don't have link. Cable must be unconnected.
415 * Enable phy interrupts so we take action when
419 netxen_crb_writelit_adapter(adapter
,
420 NETXEN_NIU_GB_MAC_CONFIG_0
422 NETXEN_GB_MAC_SOFT_RESET
);
423 netxen_crb_writelit_adapter(adapter
,
424 NETXEN_NIU_GB_MAC_CONFIG_0
426 NETXEN_GB_MAC_RESET_PROT_BLK
427 | NETXEN_GB_MAC_ENABLE_TX_RX
429 NETXEN_GB_MAC_PAUSED_FRMS
);
430 if (netxen_niu_gbe_clear_phy_interrupts(adapter
))
432 "ERROR clearing PHY interrupts\n");
433 if (netxen_niu_gbe_enable_phy_interrupts(adapter
))
435 "ERROR enabling PHY interrupts\n");
436 if (netxen_niu_gbe_clear_phy_interrupts(adapter
))
438 "ERROR clearing PHY interrupts\n");
447 int netxen_niu_xg_init_port(struct netxen_adapter
*adapter
, int port
)
449 if (NX_IS_REVISION_P2(adapter
->ahw
.revision_id
)) {
450 netxen_crb_writelit_adapter(adapter
,
451 NETXEN_NIU_XGE_CONFIG_1
+(0x10000*port
), 0x1447);
452 netxen_crb_writelit_adapter(adapter
,
453 NETXEN_NIU_XGE_CONFIG_0
+(0x10000*port
), 0x5);
460 * Return the current station MAC address.
461 * Note that the passed-in value must already be in network byte order.
463 static int netxen_niu_macaddr_get(struct netxen_adapter
*adapter
,
464 netxen_ethernet_macaddr_t
* addr
)
468 int phy
= adapter
->physical_port
;
473 if ((phy
< 0) || (phy
> 3))
476 if (adapter
->hw_read_wx(adapter
, NETXEN_NIU_GB_STATION_ADDR_0(phy
),
479 if (adapter
->hw_read_wx(adapter
, NETXEN_NIU_GB_STATION_ADDR_1(phy
),
482 ((__le32
*)val
)[1] = cpu_to_le32(stationhigh
);
483 ((__le32
*)val
)[0] = cpu_to_le32(stationlow
);
485 memcpy(addr
, val
+ 2, 6);
491 * Set the station MAC address.
492 * Note that the passed-in value must already be in network byte order.
494 int netxen_niu_macaddr_set(struct netxen_adapter
*adapter
,
495 netxen_ethernet_macaddr_t addr
)
499 int phy
= adapter
->physical_port
;
500 unsigned char mac_addr
[6];
503 if (NX_IS_REVISION_P3(adapter
->ahw
.revision_id
))
506 for (i
= 0; i
< 10; i
++) {
507 temp
[0] = temp
[1] = 0;
508 memcpy(temp
+ 2, addr
, 2);
509 val
= le32_to_cpu(*(__le32
*)temp
);
510 if (adapter
->hw_write_wx(adapter
,
511 NETXEN_NIU_GB_STATION_ADDR_1(phy
), &val
, 4))
514 memcpy(temp
, ((u8
*) addr
) + 2, sizeof(__le32
));
515 val
= le32_to_cpu(*(__le32
*)temp
);
516 if (adapter
->hw_write_wx(adapter
,
517 NETXEN_NIU_GB_STATION_ADDR_0(phy
), &val
, 4))
520 netxen_niu_macaddr_get(adapter
,
521 (netxen_ethernet_macaddr_t
*) mac_addr
);
522 if (memcmp(mac_addr
, addr
, 6) == 0)
527 printk(KERN_ERR
"%s: cannot set Mac addr for %s\n",
528 netxen_nic_driver_name
, adapter
->netdev
->name
);
529 printk(KERN_ERR
"MAC address set: %pM.\n", addr
);
530 printk(KERN_ERR
"MAC address get: %pM.\n", mac_addr
);
535 /* Disable a GbE interface */
536 int netxen_niu_disable_gbe_port(struct netxen_adapter
*adapter
)
539 u32 port
= adapter
->physical_port
;
541 if (NX_IS_REVISION_P3(adapter
->ahw
.revision_id
))
544 if (port
> NETXEN_NIU_MAX_GBE_PORTS
)
547 netxen_gb_soft_reset(mac_cfg0
);
548 if (adapter
->hw_write_wx(adapter
, NETXEN_NIU_GB_MAC_CONFIG_0(port
),
554 /* Disable an XG interface */
555 int netxen_niu_disable_xg_port(struct netxen_adapter
*adapter
)
558 u32 port
= adapter
->physical_port
;
560 if (NX_IS_REVISION_P3(adapter
->ahw
.revision_id
))
563 if (port
> NETXEN_NIU_MAX_XG_PORTS
)
567 if (adapter
->hw_write_wx(adapter
,
568 NETXEN_NIU_XGE_CONFIG_0
+ (0x10000 * port
), &mac_cfg
, 4))
573 /* Set promiscuous mode for a GbE interface */
574 int netxen_niu_set_promiscuous_mode(struct netxen_adapter
*adapter
,
578 u32 port
= adapter
->physical_port
;
580 if (port
> NETXEN_NIU_MAX_GBE_PORTS
)
583 /* save previous contents */
584 if (adapter
->hw_read_wx(adapter
, NETXEN_NIU_GB_DROP_WRONGADDR
,
587 if (mode
== NETXEN_NIU_PROMISC_MODE
) {
590 netxen_clear_gb_drop_gb0(reg
);
593 netxen_clear_gb_drop_gb1(reg
);
596 netxen_clear_gb_drop_gb2(reg
);
599 netxen_clear_gb_drop_gb3(reg
);
607 netxen_set_gb_drop_gb0(reg
);
610 netxen_set_gb_drop_gb1(reg
);
613 netxen_set_gb_drop_gb2(reg
);
616 netxen_set_gb_drop_gb3(reg
);
622 if (adapter
->hw_write_wx(adapter
, NETXEN_NIU_GB_DROP_WRONGADDR
,
629 * Set the MAC address for an XG port
630 * Note that the passed-in value must already be in network byte order.
632 int netxen_niu_xg_macaddr_set(struct netxen_adapter
*adapter
,
633 netxen_ethernet_macaddr_t addr
)
635 int phy
= adapter
->physical_port
;
639 if (NX_IS_REVISION_P3(adapter
->ahw
.revision_id
))
642 if ((phy
< 0) || (phy
> NETXEN_NIU_MAX_XG_PORTS
))
645 temp
[0] = temp
[1] = 0;
648 memcpy(temp
+ 2, addr
, 2);
649 val
= le32_to_cpu(*(__le32
*)temp
);
650 if (adapter
->hw_write_wx(adapter
, NETXEN_NIU_XGE_STATION_ADDR_0_1
,
654 memcpy(&temp
, ((u8
*) addr
) + 2, sizeof(__le32
));
655 val
= le32_to_cpu(*(__le32
*)temp
);
656 if (adapter
->hw_write_wx(adapter
, NETXEN_NIU_XGE_STATION_ADDR_0_HI
,
662 memcpy(temp
+ 2, addr
, 2);
663 val
= le32_to_cpu(*(__le32
*)temp
);
664 if (adapter
->hw_write_wx(adapter
, NETXEN_NIU_XG1_STATION_ADDR_0_1
,
668 memcpy(&temp
, ((u8
*) addr
) + 2, sizeof(__le32
));
669 val
= le32_to_cpu(*(__le32
*)temp
);
670 if (adapter
->hw_write_wx(adapter
, NETXEN_NIU_XG1_STATION_ADDR_0_HI
,
676 printk(KERN_ERR
"Unknown port %d\n", phy
);
683 int netxen_niu_xg_set_promiscuous_mode(struct netxen_adapter
*adapter
,
687 u32 port
= adapter
->physical_port
;
689 if (port
> NETXEN_NIU_MAX_XG_PORTS
)
692 if (adapter
->hw_read_wx(adapter
,
693 NETXEN_NIU_XGE_CONFIG_1
+ (0x10000 * port
), ®
, 4))
695 if (mode
== NETXEN_NIU_PROMISC_MODE
)
696 reg
= (reg
| 0x2000UL
);
698 reg
= (reg
& ~0x2000UL
);
700 if (mode
== NETXEN_NIU_ALLMULTI_MODE
)
701 reg
= (reg
| 0x1000UL
);
703 reg
= (reg
& ~0x1000UL
);
705 netxen_crb_writelit_adapter(adapter
,
706 NETXEN_NIU_XGE_CONFIG_1
+ (0x10000 * port
), reg
);