1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* atlx.c -- common functions for Attansic network drivers
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright(c) 2005 - 2006 Attansic Corporation. All rights reserved.
5*4882a593Smuzhiyun * Copyright(c) 2006 - 2007 Chris Snook <csnook@redhat.com>
6*4882a593Smuzhiyun * Copyright(c) 2006 - 2008 Jay Cliburn <jcliburn@gmail.com>
7*4882a593Smuzhiyun * Copyright(c) 2007 Atheros Corporation. All rights reserved.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Derived from Intel e1000 driver
10*4882a593Smuzhiyun * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun /* Including this file like a header is a temporary hack, I promise. -- CHS */
14*4882a593Smuzhiyun #ifndef ATLX_C
15*4882a593Smuzhiyun #define ATLX_C
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <linux/device.h>
18*4882a593Smuzhiyun #include <linux/errno.h>
19*4882a593Smuzhiyun #include <linux/etherdevice.h>
20*4882a593Smuzhiyun #include <linux/if.h>
21*4882a593Smuzhiyun #include <linux/netdevice.h>
22*4882a593Smuzhiyun #include <linux/socket.h>
23*4882a593Smuzhiyun #include <linux/sockios.h>
24*4882a593Smuzhiyun #include <linux/spinlock.h>
25*4882a593Smuzhiyun #include <linux/string.h>
26*4882a593Smuzhiyun #include <linux/types.h>
27*4882a593Smuzhiyun #include <linux/workqueue.h>
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "atlx.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun static s32 atlx_read_phy_reg(struct atl1_hw *hw, u16 reg_addr, u16 *phy_data);
32*4882a593Smuzhiyun static u32 atlx_hash_mc_addr(struct atl1_hw *hw, u8 *mc_addr);
33*4882a593Smuzhiyun static void atlx_set_mac_addr(struct atl1_hw *hw);
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun static struct atlx_spi_flash_dev flash_table[] = {
36*4882a593Smuzhiyun /* MFR_NAME WRSR READ PRGM WREN WRDI RDSR RDID SEC_ERS CHIP_ERS */
37*4882a593Smuzhiyun {"Atmel", 0x00, 0x03, 0x02, 0x06, 0x04, 0x05, 0x15, 0x52, 0x62},
38*4882a593Smuzhiyun {"SST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0x90, 0x20, 0x60},
39*4882a593Smuzhiyun {"ST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0xAB, 0xD8, 0xC7},
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun
atlx_ioctl(struct net_device * netdev,struct ifreq * ifr,int cmd)42*4882a593Smuzhiyun static int atlx_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun switch (cmd) {
45*4882a593Smuzhiyun case SIOCGMIIPHY:
46*4882a593Smuzhiyun case SIOCGMIIREG:
47*4882a593Smuzhiyun case SIOCSMIIREG:
48*4882a593Smuzhiyun return atlx_mii_ioctl(netdev, ifr, cmd);
49*4882a593Smuzhiyun default:
50*4882a593Smuzhiyun return -EOPNOTSUPP;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun * atlx_set_mac - Change the Ethernet Address of the NIC
56*4882a593Smuzhiyun * @netdev: network interface device structure
57*4882a593Smuzhiyun * @p: pointer to an address structure
58*4882a593Smuzhiyun *
59*4882a593Smuzhiyun * Returns 0 on success, negative on failure
60*4882a593Smuzhiyun */
atlx_set_mac(struct net_device * netdev,void * p)61*4882a593Smuzhiyun static int atlx_set_mac(struct net_device *netdev, void *p)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun struct atlx_adapter *adapter = netdev_priv(netdev);
64*4882a593Smuzhiyun struct sockaddr *addr = p;
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun if (netif_running(netdev))
67*4882a593Smuzhiyun return -EBUSY;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun if (!is_valid_ether_addr(addr->sa_data))
70*4882a593Smuzhiyun return -EADDRNOTAVAIL;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
73*4882a593Smuzhiyun memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun atlx_set_mac_addr(&adapter->hw);
76*4882a593Smuzhiyun return 0;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
atlx_check_for_link(struct atlx_adapter * adapter)79*4882a593Smuzhiyun static void atlx_check_for_link(struct atlx_adapter *adapter)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun struct net_device *netdev = adapter->netdev;
82*4882a593Smuzhiyun u16 phy_data = 0;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun spin_lock(&adapter->lock);
85*4882a593Smuzhiyun adapter->phy_timer_pending = false;
86*4882a593Smuzhiyun atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
87*4882a593Smuzhiyun atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
88*4882a593Smuzhiyun spin_unlock(&adapter->lock);
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* notify upper layer link down ASAP */
91*4882a593Smuzhiyun if (!(phy_data & BMSR_LSTATUS)) {
92*4882a593Smuzhiyun /* Link Down */
93*4882a593Smuzhiyun if (netif_carrier_ok(netdev)) {
94*4882a593Smuzhiyun /* old link state: Up */
95*4882a593Smuzhiyun dev_info(&adapter->pdev->dev, "%s link is down\n",
96*4882a593Smuzhiyun netdev->name);
97*4882a593Smuzhiyun adapter->link_speed = SPEED_0;
98*4882a593Smuzhiyun netif_carrier_off(netdev);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun schedule_work(&adapter->link_chg_task);
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /**
105*4882a593Smuzhiyun * atlx_set_multi - Multicast and Promiscuous mode set
106*4882a593Smuzhiyun * @netdev: network interface device structure
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * The set_multi entry point is called whenever the multicast address
109*4882a593Smuzhiyun * list or the network interface flags are updated. This routine is
110*4882a593Smuzhiyun * responsible for configuring the hardware for proper multicast,
111*4882a593Smuzhiyun * promiscuous mode, and all-multi behavior.
112*4882a593Smuzhiyun */
atlx_set_multi(struct net_device * netdev)113*4882a593Smuzhiyun static void atlx_set_multi(struct net_device *netdev)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun struct atlx_adapter *adapter = netdev_priv(netdev);
116*4882a593Smuzhiyun struct atlx_hw *hw = &adapter->hw;
117*4882a593Smuzhiyun struct netdev_hw_addr *ha;
118*4882a593Smuzhiyun u32 rctl;
119*4882a593Smuzhiyun u32 hash_value;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun /* Check for Promiscuous and All Multicast modes */
122*4882a593Smuzhiyun rctl = ioread32(hw->hw_addr + REG_MAC_CTRL);
123*4882a593Smuzhiyun if (netdev->flags & IFF_PROMISC)
124*4882a593Smuzhiyun rctl |= MAC_CTRL_PROMIS_EN;
125*4882a593Smuzhiyun else if (netdev->flags & IFF_ALLMULTI) {
126*4882a593Smuzhiyun rctl |= MAC_CTRL_MC_ALL_EN;
127*4882a593Smuzhiyun rctl &= ~MAC_CTRL_PROMIS_EN;
128*4882a593Smuzhiyun } else
129*4882a593Smuzhiyun rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun iowrite32(rctl, hw->hw_addr + REG_MAC_CTRL);
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /* clear the old settings from the multicast hash table */
134*4882a593Smuzhiyun iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
135*4882a593Smuzhiyun iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /* compute mc addresses' hash value ,and put it into hash table */
138*4882a593Smuzhiyun netdev_for_each_mc_addr(ha, netdev) {
139*4882a593Smuzhiyun hash_value = atlx_hash_mc_addr(hw, ha->addr);
140*4882a593Smuzhiyun atlx_hash_set(hw, hash_value);
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
atlx_imr_set(struct atlx_adapter * adapter,unsigned int imr)144*4882a593Smuzhiyun static inline void atlx_imr_set(struct atlx_adapter *adapter,
145*4882a593Smuzhiyun unsigned int imr)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun iowrite32(imr, adapter->hw.hw_addr + REG_IMR);
148*4882a593Smuzhiyun ioread32(adapter->hw.hw_addr + REG_IMR);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /**
152*4882a593Smuzhiyun * atlx_irq_enable - Enable default interrupt generation settings
153*4882a593Smuzhiyun * @adapter: board private structure
154*4882a593Smuzhiyun */
atlx_irq_enable(struct atlx_adapter * adapter)155*4882a593Smuzhiyun static void atlx_irq_enable(struct atlx_adapter *adapter)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun atlx_imr_set(adapter, IMR_NORMAL_MASK);
158*4882a593Smuzhiyun adapter->int_enabled = true;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /**
162*4882a593Smuzhiyun * atlx_irq_disable - Mask off interrupt generation on the NIC
163*4882a593Smuzhiyun * @adapter: board private structure
164*4882a593Smuzhiyun */
atlx_irq_disable(struct atlx_adapter * adapter)165*4882a593Smuzhiyun static void atlx_irq_disable(struct atlx_adapter *adapter)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun adapter->int_enabled = false;
168*4882a593Smuzhiyun atlx_imr_set(adapter, 0);
169*4882a593Smuzhiyun synchronize_irq(adapter->pdev->irq);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
atlx_clear_phy_int(struct atlx_adapter * adapter)172*4882a593Smuzhiyun static void atlx_clear_phy_int(struct atlx_adapter *adapter)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun u16 phy_data;
175*4882a593Smuzhiyun unsigned long flags;
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun spin_lock_irqsave(&adapter->lock, flags);
178*4882a593Smuzhiyun atlx_read_phy_reg(&adapter->hw, 19, &phy_data);
179*4882a593Smuzhiyun spin_unlock_irqrestore(&adapter->lock, flags);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /**
183*4882a593Smuzhiyun * atlx_tx_timeout - Respond to a Tx Hang
184*4882a593Smuzhiyun * @netdev: network interface device structure
185*4882a593Smuzhiyun */
atlx_tx_timeout(struct net_device * netdev,unsigned int txqueue)186*4882a593Smuzhiyun static void atlx_tx_timeout(struct net_device *netdev, unsigned int txqueue)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun struct atlx_adapter *adapter = netdev_priv(netdev);
189*4882a593Smuzhiyun /* Do the reset outside of interrupt context */
190*4882a593Smuzhiyun schedule_work(&adapter->reset_dev_task);
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun /*
194*4882a593Smuzhiyun * atlx_link_chg_task - deal with link change event Out of interrupt context
195*4882a593Smuzhiyun */
atlx_link_chg_task(struct work_struct * work)196*4882a593Smuzhiyun static void atlx_link_chg_task(struct work_struct *work)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun struct atlx_adapter *adapter;
199*4882a593Smuzhiyun unsigned long flags;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun adapter = container_of(work, struct atlx_adapter, link_chg_task);
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun spin_lock_irqsave(&adapter->lock, flags);
204*4882a593Smuzhiyun atlx_check_link(adapter);
205*4882a593Smuzhiyun spin_unlock_irqrestore(&adapter->lock, flags);
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
__atlx_vlan_mode(netdev_features_t features,u32 * ctrl)208*4882a593Smuzhiyun static void __atlx_vlan_mode(netdev_features_t features, u32 *ctrl)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun if (features & NETIF_F_HW_VLAN_CTAG_RX) {
211*4882a593Smuzhiyun /* enable VLAN tag insert/strip */
212*4882a593Smuzhiyun *ctrl |= MAC_CTRL_RMV_VLAN;
213*4882a593Smuzhiyun } else {
214*4882a593Smuzhiyun /* disable VLAN tag insert/strip */
215*4882a593Smuzhiyun *ctrl &= ~MAC_CTRL_RMV_VLAN;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
atlx_vlan_mode(struct net_device * netdev,netdev_features_t features)219*4882a593Smuzhiyun static void atlx_vlan_mode(struct net_device *netdev,
220*4882a593Smuzhiyun netdev_features_t features)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun struct atlx_adapter *adapter = netdev_priv(netdev);
223*4882a593Smuzhiyun unsigned long flags;
224*4882a593Smuzhiyun u32 ctrl;
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun spin_lock_irqsave(&adapter->lock, flags);
227*4882a593Smuzhiyun /* atlx_irq_disable(adapter); FIXME: confirm/remove */
228*4882a593Smuzhiyun ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
229*4882a593Smuzhiyun __atlx_vlan_mode(features, &ctrl);
230*4882a593Smuzhiyun iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
231*4882a593Smuzhiyun /* atlx_irq_enable(adapter); FIXME */
232*4882a593Smuzhiyun spin_unlock_irqrestore(&adapter->lock, flags);
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun
atlx_restore_vlan(struct atlx_adapter * adapter)235*4882a593Smuzhiyun static void atlx_restore_vlan(struct atlx_adapter *adapter)
236*4882a593Smuzhiyun {
237*4882a593Smuzhiyun atlx_vlan_mode(adapter->netdev, adapter->netdev->features);
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
atlx_fix_features(struct net_device * netdev,netdev_features_t features)240*4882a593Smuzhiyun static netdev_features_t atlx_fix_features(struct net_device *netdev,
241*4882a593Smuzhiyun netdev_features_t features)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun /*
244*4882a593Smuzhiyun * Since there is no support for separate rx/tx vlan accel
245*4882a593Smuzhiyun * enable/disable make sure tx flag is always in same state as rx.
246*4882a593Smuzhiyun */
247*4882a593Smuzhiyun if (features & NETIF_F_HW_VLAN_CTAG_RX)
248*4882a593Smuzhiyun features |= NETIF_F_HW_VLAN_CTAG_TX;
249*4882a593Smuzhiyun else
250*4882a593Smuzhiyun features &= ~NETIF_F_HW_VLAN_CTAG_TX;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun return features;
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun
atlx_set_features(struct net_device * netdev,netdev_features_t features)255*4882a593Smuzhiyun static int atlx_set_features(struct net_device *netdev,
256*4882a593Smuzhiyun netdev_features_t features)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun netdev_features_t changed = netdev->features ^ features;
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun if (changed & NETIF_F_HW_VLAN_CTAG_RX)
261*4882a593Smuzhiyun atlx_vlan_mode(netdev, features);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun return 0;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun #endif /* ATLX_C */
267