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