Staging: Add octeon-ethernet driver files.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / octeon / ethernet-common.c
1 /**********************************************************************
2 * Author: Cavium Networks
3 *
4 * Contact: support@caviumnetworks.com
5 * This file is part of the OCTEON SDK
6 *
7 * Copyright (c) 2003-2007 Cavium Networks
8 *
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
12 *
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this file; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * or visit http://www.gnu.org/licenses/.
23 *
24 * This file may also be available under a different license from Cavium.
25 * Contact Cavium Networks for more information
26 **********************************************************************/
27 #include <linux/kernel.h>
28 #include <linux/mii.h>
29 #include <net/dst.h>
30
31 #include <asm/atomic.h>
32 #include <asm/octeon/octeon.h>
33
34 #include "ethernet-defines.h"
35 #include "ethernet-tx.h"
36 #include "ethernet-mdio.h"
37 #include "ethernet-util.h"
38 #include "octeon-ethernet.h"
39 #include "ethernet-common.h"
40
41 #include "cvmx-pip.h"
42 #include "cvmx-pko.h"
43 #include "cvmx-fau.h"
44 #include "cvmx-helper.h"
45
46 #include "cvmx-gmxx-defs.h"
47
48 /**
49 * Get the low level ethernet statistics
50 *
51 * @dev: Device to get the statistics from
52 * Returns Pointer to the statistics
53 */
54 static struct net_device_stats *cvm_oct_common_get_stats(struct net_device *dev)
55 {
56 cvmx_pip_port_status_t rx_status;
57 cvmx_pko_port_status_t tx_status;
58 struct octeon_ethernet *priv = netdev_priv(dev);
59
60 if (priv->port < CVMX_PIP_NUM_INPUT_PORTS) {
61 if (octeon_is_simulation()) {
62 /* The simulator doesn't support statistics */
63 memset(&rx_status, 0, sizeof(rx_status));
64 memset(&tx_status, 0, sizeof(tx_status));
65 } else {
66 cvmx_pip_get_port_status(priv->port, 1, &rx_status);
67 cvmx_pko_get_port_status(priv->port, 1, &tx_status);
68 }
69
70 priv->stats.rx_packets += rx_status.inb_packets;
71 priv->stats.tx_packets += tx_status.packets;
72 priv->stats.rx_bytes += rx_status.inb_octets;
73 priv->stats.tx_bytes += tx_status.octets;
74 priv->stats.multicast += rx_status.multicast_packets;
75 priv->stats.rx_crc_errors += rx_status.inb_errors;
76 priv->stats.rx_frame_errors += rx_status.fcs_align_err_packets;
77
78 /*
79 * The drop counter must be incremented atomically
80 * since the RX tasklet also increments it.
81 */
82 #ifdef CONFIG_64BIT
83 atomic64_add(rx_status.dropped_packets,
84 (atomic64_t *)&priv->stats.rx_dropped);
85 #else
86 atomic_add(rx_status.dropped_packets,
87 (atomic_t *)&priv->stats.rx_dropped);
88 #endif
89 }
90
91 return &priv->stats;
92 }
93
94 /**
95 * Set the multicast list. Currently unimplemented.
96 *
97 * @dev: Device to work on
98 */
99 static void cvm_oct_common_set_multicast_list(struct net_device *dev)
100 {
101 union cvmx_gmxx_prtx_cfg gmx_cfg;
102 struct octeon_ethernet *priv = netdev_priv(dev);
103 int interface = INTERFACE(priv->port);
104 int index = INDEX(priv->port);
105
106 if ((interface < 2)
107 && (cvmx_helper_interface_get_mode(interface) !=
108 CVMX_HELPER_INTERFACE_MODE_SPI)) {
109 union cvmx_gmxx_rxx_adr_ctl control;
110 control.u64 = 0;
111 control.s.bcst = 1; /* Allow broadcast MAC addresses */
112
113 if (dev->mc_list || (dev->flags & IFF_ALLMULTI) ||
114 (dev->flags & IFF_PROMISC))
115 /* Force accept multicast packets */
116 control.s.mcst = 2;
117 else
118 /* Force reject multicat packets */
119 control.s.mcst = 1;
120
121 if (dev->flags & IFF_PROMISC)
122 /*
123 * Reject matches if promisc. Since CAM is
124 * shut off, should accept everything.
125 */
126 control.s.cam_mode = 0;
127 else
128 /* Filter packets based on the CAM */
129 control.s.cam_mode = 1;
130
131 gmx_cfg.u64 =
132 cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
133 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
134 gmx_cfg.u64 & ~1ull);
135
136 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CTL(index, interface),
137 control.u64);
138 if (dev->flags & IFF_PROMISC)
139 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM_EN
140 (index, interface), 0);
141 else
142 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM_EN
143 (index, interface), 1);
144
145 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
146 gmx_cfg.u64);
147 }
148 }
149
150 /**
151 * Set the hardware MAC address for a device
152 *
153 * @dev: Device to change the MAC address for
154 * @addr: Address structure to change it too. MAC address is addr + 2.
155 * Returns Zero on success
156 */
157 static int cvm_oct_common_set_mac_address(struct net_device *dev, void *addr)
158 {
159 struct octeon_ethernet *priv = netdev_priv(dev);
160 union cvmx_gmxx_prtx_cfg gmx_cfg;
161 int interface = INTERFACE(priv->port);
162 int index = INDEX(priv->port);
163
164 memcpy(dev->dev_addr, addr + 2, 6);
165
166 if ((interface < 2)
167 && (cvmx_helper_interface_get_mode(interface) !=
168 CVMX_HELPER_INTERFACE_MODE_SPI)) {
169 int i;
170 uint8_t *ptr = addr;
171 uint64_t mac = 0;
172 for (i = 0; i < 6; i++)
173 mac = (mac << 8) | (uint64_t) (ptr[i + 2]);
174
175 gmx_cfg.u64 =
176 cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
177 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
178 gmx_cfg.u64 & ~1ull);
179
180 cvmx_write_csr(CVMX_GMXX_SMACX(index, interface), mac);
181 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM0(index, interface),
182 ptr[2]);
183 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM1(index, interface),
184 ptr[3]);
185 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM2(index, interface),
186 ptr[4]);
187 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM3(index, interface),
188 ptr[5]);
189 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM4(index, interface),
190 ptr[6]);
191 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM5(index, interface),
192 ptr[7]);
193 cvm_oct_common_set_multicast_list(dev);
194 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
195 gmx_cfg.u64);
196 }
197 return 0;
198 }
199
200 /**
201 * Change the link MTU. Unimplemented
202 *
203 * @dev: Device to change
204 * @new_mtu: The new MTU
205 *
206 * Returns Zero on success
207 */
208 static int cvm_oct_common_change_mtu(struct net_device *dev, int new_mtu)
209 {
210 struct octeon_ethernet *priv = netdev_priv(dev);
211 int interface = INTERFACE(priv->port);
212 int index = INDEX(priv->port);
213 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
214 int vlan_bytes = 4;
215 #else
216 int vlan_bytes = 0;
217 #endif
218
219 /*
220 * Limit the MTU to make sure the ethernet packets are between
221 * 64 bytes and 65535 bytes.
222 */
223 if ((new_mtu + 14 + 4 + vlan_bytes < 64)
224 || (new_mtu + 14 + 4 + vlan_bytes > 65392)) {
225 pr_err("MTU must be between %d and %d.\n",
226 64 - 14 - 4 - vlan_bytes, 65392 - 14 - 4 - vlan_bytes);
227 return -EINVAL;
228 }
229 dev->mtu = new_mtu;
230
231 if ((interface < 2)
232 && (cvmx_helper_interface_get_mode(interface) !=
233 CVMX_HELPER_INTERFACE_MODE_SPI)) {
234 /* Add ethernet header and FCS, and VLAN if configured. */
235 int max_packet = new_mtu + 14 + 4 + vlan_bytes;
236
237 if (OCTEON_IS_MODEL(OCTEON_CN3XXX)
238 || OCTEON_IS_MODEL(OCTEON_CN58XX)) {
239 /* Signal errors on packets larger than the MTU */
240 cvmx_write_csr(CVMX_GMXX_RXX_FRM_MAX(index, interface),
241 max_packet);
242 } else {
243 /*
244 * Set the hardware to truncate packets larger
245 * than the MTU and smaller the 64 bytes.
246 */
247 union cvmx_pip_frm_len_chkx frm_len_chk;
248 frm_len_chk.u64 = 0;
249 frm_len_chk.s.minlen = 64;
250 frm_len_chk.s.maxlen = max_packet;
251 cvmx_write_csr(CVMX_PIP_FRM_LEN_CHKX(interface),
252 frm_len_chk.u64);
253 }
254 /*
255 * Set the hardware to truncate packets larger than
256 * the MTU. The jabber register must be set to a
257 * multiple of 8 bytes, so round up.
258 */
259 cvmx_write_csr(CVMX_GMXX_RXX_JABBER(index, interface),
260 (max_packet + 7) & ~7u);
261 }
262 return 0;
263 }
264
265 /**
266 * Per network device initialization
267 *
268 * @dev: Device to initialize
269 * Returns Zero on success
270 */
271 int cvm_oct_common_init(struct net_device *dev)
272 {
273 static int count;
274 char mac[8] = { 0x00, 0x00,
275 octeon_bootinfo->mac_addr_base[0],
276 octeon_bootinfo->mac_addr_base[1],
277 octeon_bootinfo->mac_addr_base[2],
278 octeon_bootinfo->mac_addr_base[3],
279 octeon_bootinfo->mac_addr_base[4],
280 octeon_bootinfo->mac_addr_base[5] + count
281 };
282 struct octeon_ethernet *priv = netdev_priv(dev);
283
284 /*
285 * Force the interface to use the POW send if always_use_pow
286 * was specified or it is in the pow send list.
287 */
288 if ((pow_send_group != -1)
289 && (always_use_pow || strstr(pow_send_list, dev->name)))
290 priv->queue = -1;
291
292 if (priv->queue != -1) {
293 dev->hard_start_xmit = cvm_oct_xmit;
294 if (USE_HW_TCPUDP_CHECKSUM)
295 dev->features |= NETIF_F_IP_CSUM;
296 } else
297 dev->hard_start_xmit = cvm_oct_xmit_pow;
298 count++;
299
300 dev->get_stats = cvm_oct_common_get_stats;
301 dev->set_mac_address = cvm_oct_common_set_mac_address;
302 dev->set_multicast_list = cvm_oct_common_set_multicast_list;
303 dev->change_mtu = cvm_oct_common_change_mtu;
304 dev->do_ioctl = cvm_oct_ioctl;
305 /* We do our own locking, Linux doesn't need to */
306 dev->features |= NETIF_F_LLTX;
307 SET_ETHTOOL_OPS(dev, &cvm_oct_ethtool_ops);
308 #ifdef CONFIG_NET_POLL_CONTROLLER
309 dev->poll_controller = cvm_oct_poll_controller;
310 #endif
311
312 cvm_oct_mdio_setup_device(dev);
313 dev->set_mac_address(dev, mac);
314 dev->change_mtu(dev, dev->mtu);
315
316 /*
317 * Zero out stats for port so we won't mistakenly show
318 * counters from the bootloader.
319 */
320 memset(dev->get_stats(dev), 0, sizeof(struct net_device_stats));
321
322 return 0;
323 }
324
325 void cvm_oct_common_uninit(struct net_device *dev)
326 {
327 /* Currently nothing to do */
328 }