Staging: Octeon Ethernet: Convert to NAPI.
[safe/jmp/linux-2.6] / drivers / staging / octeon / ethernet.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/init.h>
29 #include <linux/module.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/delay.h>
33 #include <linux/phy.h>
34
35 #include <net/dst.h>
36
37 #include <asm/octeon/octeon.h>
38
39 #include "ethernet-defines.h"
40 #include "octeon-ethernet.h"
41 #include "ethernet-mem.h"
42 #include "ethernet-rx.h"
43 #include "ethernet-tx.h"
44 #include "ethernet-mdio.h"
45 #include "ethernet-util.h"
46 #include "ethernet-proc.h"
47
48
49 #include "cvmx-pip.h"
50 #include "cvmx-pko.h"
51 #include "cvmx-fau.h"
52 #include "cvmx-ipd.h"
53 #include "cvmx-helper.h"
54
55 #include "cvmx-gmxx-defs.h"
56 #include "cvmx-smix-defs.h"
57
58 #if defined(CONFIG_CAVIUM_OCTEON_NUM_PACKET_BUFFERS) \
59         && CONFIG_CAVIUM_OCTEON_NUM_PACKET_BUFFERS
60 int num_packet_buffers = CONFIG_CAVIUM_OCTEON_NUM_PACKET_BUFFERS;
61 #else
62 int num_packet_buffers = 1024;
63 #endif
64 module_param(num_packet_buffers, int, 0444);
65 MODULE_PARM_DESC(num_packet_buffers, "\n"
66         "\tNumber of packet buffers to allocate and store in the\n"
67         "\tFPA. By default, 1024 packet buffers are used unless\n"
68         "\tCONFIG_CAVIUM_OCTEON_NUM_PACKET_BUFFERS is defined.");
69
70 int pow_receive_group = 15;
71 module_param(pow_receive_group, int, 0444);
72 MODULE_PARM_DESC(pow_receive_group, "\n"
73         "\tPOW group to receive packets from. All ethernet hardware\n"
74         "\twill be configured to send incomming packets to this POW\n"
75         "\tgroup. Also any other software can submit packets to this\n"
76         "\tgroup for the kernel to process.");
77
78 int pow_send_group = -1;
79 module_param(pow_send_group, int, 0644);
80 MODULE_PARM_DESC(pow_send_group, "\n"
81         "\tPOW group to send packets to other software on. This\n"
82         "\tcontrols the creation of the virtual device pow0.\n"
83         "\talways_use_pow also depends on this value.");
84
85 int always_use_pow;
86 module_param(always_use_pow, int, 0444);
87 MODULE_PARM_DESC(always_use_pow, "\n"
88         "\tWhen set, always send to the pow group. This will cause\n"
89         "\tpackets sent to real ethernet devices to be sent to the\n"
90         "\tPOW group instead of the hardware. Unless some other\n"
91         "\tapplication changes the config, packets will still be\n"
92         "\treceived from the low level hardware. Use this option\n"
93         "\tto allow a CVMX app to intercept all packets from the\n"
94         "\tlinux kernel. You must specify pow_send_group along with\n"
95         "\tthis option.");
96
97 char pow_send_list[128] = "";
98 module_param_string(pow_send_list, pow_send_list, sizeof(pow_send_list), 0444);
99 MODULE_PARM_DESC(pow_send_list, "\n"
100         "\tComma separated list of ethernet devices that should use the\n"
101         "\tPOW for transmit instead of the actual ethernet hardware. This\n"
102         "\tis a per port version of always_use_pow. always_use_pow takes\n"
103         "\tprecedence over this list. For example, setting this to\n"
104         "\t\"eth2,spi3,spi7\" would cause these three devices to transmit\n"
105         "\tusing the pow_send_group.");
106
107 int max_rx_cpus = -1;
108 module_param(max_rx_cpus, int, 0444);
109 MODULE_PARM_DESC(max_rx_cpus, "\n"
110         "\t\tThe maximum number of CPUs to use for packet reception.\n"
111         "\t\tUse -1 to use all available CPUs.");
112
113 int rx_napi_weight = 32;
114 module_param(rx_napi_weight, int, 0444);
115 MODULE_PARM_DESC(rx_napi_weight, "The NAPI WEIGHT parameter.");
116
117 /*
118  * The offset from mac_addr_base that should be used for the next port
119  * that is configured.  By convention, if any mgmt ports exist on the
120  * chip, they get the first mac addresses, The ports controlled by
121  * this driver are numbered sequencially following any mgmt addresses
122  * that may exist.
123  */
124 static unsigned int cvm_oct_mac_addr_offset;
125
126 /**
127  * Periodic timer to check auto negotiation
128  */
129 static struct timer_list cvm_oct_poll_timer;
130
131 /**
132  * Array of every ethernet device owned by this driver indexed by
133  * the ipd input port number.
134  */
135 struct net_device *cvm_oct_device[TOTAL_NUMBER_OF_PORTS];
136
137 /**
138  * Periodic timer tick for slow management operations
139  *
140  * @arg:    Device to check
141  */
142 static void cvm_do_timer(unsigned long arg)
143 {
144         static int port;
145         if (port < CVMX_PIP_NUM_INPUT_PORTS) {
146                 if (cvm_oct_device[port]) {
147                         struct octeon_ethernet *priv = netdev_priv(cvm_oct_device[port]);
148                         if (priv->poll)
149                                 priv->poll(cvm_oct_device[port]);
150                         cvm_oct_free_tx_skbs(priv);
151                         cvm_oct_device[port]->netdev_ops->ndo_get_stats(cvm_oct_device[port]);
152                 }
153                 port++;
154                 /*
155                  * Poll the next port in a 50th of a second.  This
156                  * spreads the polling of ports out a little bit.
157                  */
158                 mod_timer(&cvm_oct_poll_timer, jiffies + HZ/50);
159         } else {
160                 port = 0;
161                 /*
162                  * FPA 0 may have been drained, try to refill it if we
163                  * need more than num_packet_buffers / 2, otherwise
164                  * normal receive processing will refill it.  If it
165                  * were drained, no packets could be received so
166                  * cvm_oct_napi_poll would never be invoked to do the
167                  * refill.
168                  */
169                 cvm_oct_rx_refill_pool(num_packet_buffers / 2);
170                 /*
171                  * All ports have been polled. Start the next iteration through
172                  * the ports in one second.
173                  */
174                 mod_timer(&cvm_oct_poll_timer, jiffies + HZ);
175         }
176 }
177
178 /**
179  * Configure common hardware for all interfaces
180  */
181 static __init void cvm_oct_configure_common_hw(void)
182 {
183         /* Setup the FPA */
184         cvmx_fpa_enable();
185         cvm_oct_mem_fill_fpa(CVMX_FPA_PACKET_POOL, CVMX_FPA_PACKET_POOL_SIZE,
186                              num_packet_buffers);
187         cvm_oct_mem_fill_fpa(CVMX_FPA_WQE_POOL, CVMX_FPA_WQE_POOL_SIZE,
188                              num_packet_buffers);
189         if (CVMX_FPA_OUTPUT_BUFFER_POOL != CVMX_FPA_PACKET_POOL)
190                 cvm_oct_mem_fill_fpa(CVMX_FPA_OUTPUT_BUFFER_POOL,
191                                      CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE, 128);
192
193         if (USE_RED)
194                 cvmx_helper_setup_red(num_packet_buffers / 4,
195                                       num_packet_buffers / 8);
196
197 }
198
199 /**
200  * Free a work queue entry received in a intercept callback.
201  *
202  * @work_queue_entry:
203  *               Work queue entry to free
204  * Returns Zero on success, Negative on failure.
205  */
206 int cvm_oct_free_work(void *work_queue_entry)
207 {
208         cvmx_wqe_t *work = work_queue_entry;
209
210         int segments = work->word2.s.bufs;
211         union cvmx_buf_ptr segment_ptr = work->packet_ptr;
212
213         while (segments--) {
214                 union cvmx_buf_ptr next_ptr = *(union cvmx_buf_ptr *)
215                         cvmx_phys_to_ptr(segment_ptr.s.addr - 8);
216                 if (unlikely(!segment_ptr.s.i))
217                         cvmx_fpa_free(cvm_oct_get_buffer_ptr(segment_ptr),
218                                       segment_ptr.s.pool,
219                                       DONT_WRITEBACK(CVMX_FPA_PACKET_POOL_SIZE /
220                                                      128));
221                 segment_ptr = next_ptr;
222         }
223         cvmx_fpa_free(work, CVMX_FPA_WQE_POOL, DONT_WRITEBACK(1));
224
225         return 0;
226 }
227 EXPORT_SYMBOL(cvm_oct_free_work);
228
229 /**
230  * Get the low level ethernet statistics
231  *
232  * @dev:    Device to get the statistics from
233  * Returns Pointer to the statistics
234  */
235 static struct net_device_stats *cvm_oct_common_get_stats(struct net_device *dev)
236 {
237         cvmx_pip_port_status_t rx_status;
238         cvmx_pko_port_status_t tx_status;
239         struct octeon_ethernet *priv = netdev_priv(dev);
240
241         if (priv->port < CVMX_PIP_NUM_INPUT_PORTS) {
242                 if (octeon_is_simulation()) {
243                         /* The simulator doesn't support statistics */
244                         memset(&rx_status, 0, sizeof(rx_status));
245                         memset(&tx_status, 0, sizeof(tx_status));
246                 } else {
247                         cvmx_pip_get_port_status(priv->port, 1, &rx_status);
248                         cvmx_pko_get_port_status(priv->port, 1, &tx_status);
249                 }
250
251                 priv->stats.rx_packets += rx_status.inb_packets;
252                 priv->stats.tx_packets += tx_status.packets;
253                 priv->stats.rx_bytes += rx_status.inb_octets;
254                 priv->stats.tx_bytes += tx_status.octets;
255                 priv->stats.multicast += rx_status.multicast_packets;
256                 priv->stats.rx_crc_errors += rx_status.inb_errors;
257                 priv->stats.rx_frame_errors += rx_status.fcs_align_err_packets;
258
259                 /*
260                  * The drop counter must be incremented atomically
261                  * since the RX tasklet also increments it.
262                  */
263 #ifdef CONFIG_64BIT
264                 atomic64_add(rx_status.dropped_packets,
265                              (atomic64_t *)&priv->stats.rx_dropped);
266 #else
267                 atomic_add(rx_status.dropped_packets,
268                              (atomic_t *)&priv->stats.rx_dropped);
269 #endif
270         }
271
272         return &priv->stats;
273 }
274
275 /**
276  * Change the link MTU. Unimplemented
277  *
278  * @dev:     Device to change
279  * @new_mtu: The new MTU
280  *
281  * Returns Zero on success
282  */
283 static int cvm_oct_common_change_mtu(struct net_device *dev, int new_mtu)
284 {
285         struct octeon_ethernet *priv = netdev_priv(dev);
286         int interface = INTERFACE(priv->port);
287         int index = INDEX(priv->port);
288 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
289         int vlan_bytes = 4;
290 #else
291         int vlan_bytes = 0;
292 #endif
293
294         /*
295          * Limit the MTU to make sure the ethernet packets are between
296          * 64 bytes and 65535 bytes.
297          */
298         if ((new_mtu + 14 + 4 + vlan_bytes < 64)
299             || (new_mtu + 14 + 4 + vlan_bytes > 65392)) {
300                 pr_err("MTU must be between %d and %d.\n",
301                        64 - 14 - 4 - vlan_bytes, 65392 - 14 - 4 - vlan_bytes);
302                 return -EINVAL;
303         }
304         dev->mtu = new_mtu;
305
306         if ((interface < 2)
307             && (cvmx_helper_interface_get_mode(interface) !=
308                 CVMX_HELPER_INTERFACE_MODE_SPI)) {
309                 /* Add ethernet header and FCS, and VLAN if configured. */
310                 int max_packet = new_mtu + 14 + 4 + vlan_bytes;
311
312                 if (OCTEON_IS_MODEL(OCTEON_CN3XXX)
313                     || OCTEON_IS_MODEL(OCTEON_CN58XX)) {
314                         /* Signal errors on packets larger than the MTU */
315                         cvmx_write_csr(CVMX_GMXX_RXX_FRM_MAX(index, interface),
316                                        max_packet);
317                 } else {
318                         /*
319                          * Set the hardware to truncate packets larger
320                          * than the MTU and smaller the 64 bytes.
321                          */
322                         union cvmx_pip_frm_len_chkx frm_len_chk;
323                         frm_len_chk.u64 = 0;
324                         frm_len_chk.s.minlen = 64;
325                         frm_len_chk.s.maxlen = max_packet;
326                         cvmx_write_csr(CVMX_PIP_FRM_LEN_CHKX(interface),
327                                        frm_len_chk.u64);
328                 }
329                 /*
330                  * Set the hardware to truncate packets larger than
331                  * the MTU. The jabber register must be set to a
332                  * multiple of 8 bytes, so round up.
333                  */
334                 cvmx_write_csr(CVMX_GMXX_RXX_JABBER(index, interface),
335                                (max_packet + 7) & ~7u);
336         }
337         return 0;
338 }
339
340 /**
341  * Set the multicast list. Currently unimplemented.
342  *
343  * @dev:    Device to work on
344  */
345 static void cvm_oct_common_set_multicast_list(struct net_device *dev)
346 {
347         union cvmx_gmxx_prtx_cfg gmx_cfg;
348         struct octeon_ethernet *priv = netdev_priv(dev);
349         int interface = INTERFACE(priv->port);
350         int index = INDEX(priv->port);
351
352         if ((interface < 2)
353             && (cvmx_helper_interface_get_mode(interface) !=
354                 CVMX_HELPER_INTERFACE_MODE_SPI)) {
355                 union cvmx_gmxx_rxx_adr_ctl control;
356                 control.u64 = 0;
357                 control.s.bcst = 1;     /* Allow broadcast MAC addresses */
358
359                 if (dev->mc_list || (dev->flags & IFF_ALLMULTI) ||
360                     (dev->flags & IFF_PROMISC))
361                         /* Force accept multicast packets */
362                         control.s.mcst = 2;
363                 else
364                         /* Force reject multicat packets */
365                         control.s.mcst = 1;
366
367                 if (dev->flags & IFF_PROMISC)
368                         /*
369                          * Reject matches if promisc. Since CAM is
370                          * shut off, should accept everything.
371                          */
372                         control.s.cam_mode = 0;
373                 else
374                         /* Filter packets based on the CAM */
375                         control.s.cam_mode = 1;
376
377                 gmx_cfg.u64 =
378                     cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
379                 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
380                                gmx_cfg.u64 & ~1ull);
381
382                 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CTL(index, interface),
383                                control.u64);
384                 if (dev->flags & IFF_PROMISC)
385                         cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM_EN
386                                        (index, interface), 0);
387                 else
388                         cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM_EN
389                                        (index, interface), 1);
390
391                 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
392                                gmx_cfg.u64);
393         }
394 }
395
396 /**
397  * Set the hardware MAC address for a device
398  *
399  * @dev:    Device to change the MAC address for
400  * @addr:   Address structure to change it too. MAC address is addr + 2.
401  * Returns Zero on success
402  */
403 static int cvm_oct_common_set_mac_address(struct net_device *dev, void *addr)
404 {
405         struct octeon_ethernet *priv = netdev_priv(dev);
406         union cvmx_gmxx_prtx_cfg gmx_cfg;
407         int interface = INTERFACE(priv->port);
408         int index = INDEX(priv->port);
409
410         memcpy(dev->dev_addr, addr + 2, 6);
411
412         if ((interface < 2)
413             && (cvmx_helper_interface_get_mode(interface) !=
414                 CVMX_HELPER_INTERFACE_MODE_SPI)) {
415                 int i;
416                 uint8_t *ptr = addr;
417                 uint64_t mac = 0;
418                 for (i = 0; i < 6; i++)
419                         mac = (mac << 8) | (uint64_t) (ptr[i + 2]);
420
421                 gmx_cfg.u64 =
422                     cvmx_read_csr(CVMX_GMXX_PRTX_CFG(index, interface));
423                 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
424                                gmx_cfg.u64 & ~1ull);
425
426                 cvmx_write_csr(CVMX_GMXX_SMACX(index, interface), mac);
427                 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM0(index, interface),
428                                ptr[2]);
429                 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM1(index, interface),
430                                ptr[3]);
431                 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM2(index, interface),
432                                ptr[4]);
433                 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM3(index, interface),
434                                ptr[5]);
435                 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM4(index, interface),
436                                ptr[6]);
437                 cvmx_write_csr(CVMX_GMXX_RXX_ADR_CAM5(index, interface),
438                                ptr[7]);
439                 cvm_oct_common_set_multicast_list(dev);
440                 cvmx_write_csr(CVMX_GMXX_PRTX_CFG(index, interface),
441                                gmx_cfg.u64);
442         }
443         return 0;
444 }
445
446 /**
447  * Per network device initialization
448  *
449  * @dev:    Device to initialize
450  * Returns Zero on success
451  */
452 int cvm_oct_common_init(struct net_device *dev)
453 {
454         struct octeon_ethernet *priv = netdev_priv(dev);
455         struct sockaddr sa;
456         u64 mac = ((u64)(octeon_bootinfo->mac_addr_base[0] & 0xff) << 40) |
457                 ((u64)(octeon_bootinfo->mac_addr_base[1] & 0xff) << 32) |
458                 ((u64)(octeon_bootinfo->mac_addr_base[2] & 0xff) << 24) |
459                 ((u64)(octeon_bootinfo->mac_addr_base[3] & 0xff) << 16) |
460                 ((u64)(octeon_bootinfo->mac_addr_base[4] & 0xff) << 8) |
461                 (u64)(octeon_bootinfo->mac_addr_base[5] & 0xff);
462
463         mac += cvm_oct_mac_addr_offset;
464         sa.sa_data[0] = (mac >> 40) & 0xff;
465         sa.sa_data[1] = (mac >> 32) & 0xff;
466         sa.sa_data[2] = (mac >> 24) & 0xff;
467         sa.sa_data[3] = (mac >> 16) & 0xff;
468         sa.sa_data[4] = (mac >> 8) & 0xff;
469         sa.sa_data[5] = mac & 0xff;
470
471         if (cvm_oct_mac_addr_offset >= octeon_bootinfo->mac_addr_count)
472                 printk(KERN_DEBUG "%s: Using MAC outside of the assigned range:"
473                         " %02x:%02x:%02x:%02x:%02x:%02x\n", dev->name,
474                         sa.sa_data[0] & 0xff, sa.sa_data[1] & 0xff,
475                         sa.sa_data[2] & 0xff, sa.sa_data[3] & 0xff,
476                         sa.sa_data[4] & 0xff, sa.sa_data[5] & 0xff);
477         cvm_oct_mac_addr_offset++;
478
479         /*
480          * Force the interface to use the POW send if always_use_pow
481          * was specified or it is in the pow send list.
482          */
483         if ((pow_send_group != -1)
484             && (always_use_pow || strstr(pow_send_list, dev->name)))
485                 priv->queue = -1;
486
487         if (priv->queue != -1 && USE_HW_TCPUDP_CHECKSUM)
488                 dev->features |= NETIF_F_IP_CSUM;
489
490         /* We do our own locking, Linux doesn't need to */
491         dev->features |= NETIF_F_LLTX;
492         SET_ETHTOOL_OPS(dev, &cvm_oct_ethtool_ops);
493
494         cvm_oct_phy_setup_device(dev);
495         dev->netdev_ops->ndo_set_mac_address(dev, &sa);
496         dev->netdev_ops->ndo_change_mtu(dev, dev->mtu);
497
498         /*
499          * Zero out stats for port so we won't mistakenly show
500          * counters from the bootloader.
501          */
502         memset(dev->netdev_ops->ndo_get_stats(dev), 0,
503                sizeof(struct net_device_stats));
504
505         return 0;
506 }
507
508 void cvm_oct_common_uninit(struct net_device *dev)
509 {
510         struct octeon_ethernet *priv = netdev_priv(dev);
511
512         if (priv->phydev)
513                 phy_disconnect(priv->phydev);
514 }
515
516 static const struct net_device_ops cvm_oct_npi_netdev_ops = {
517         .ndo_init               = cvm_oct_common_init,
518         .ndo_uninit             = cvm_oct_common_uninit,
519         .ndo_start_xmit         = cvm_oct_xmit,
520         .ndo_set_multicast_list = cvm_oct_common_set_multicast_list,
521         .ndo_set_mac_address    = cvm_oct_common_set_mac_address,
522         .ndo_do_ioctl           = cvm_oct_ioctl,
523         .ndo_change_mtu         = cvm_oct_common_change_mtu,
524         .ndo_get_stats          = cvm_oct_common_get_stats,
525 #ifdef CONFIG_NET_POLL_CONTROLLER
526         .ndo_poll_controller    = cvm_oct_poll_controller,
527 #endif
528 };
529 static const struct net_device_ops cvm_oct_xaui_netdev_ops = {
530         .ndo_init               = cvm_oct_xaui_init,
531         .ndo_uninit             = cvm_oct_xaui_uninit,
532         .ndo_open               = cvm_oct_xaui_open,
533         .ndo_stop               = cvm_oct_xaui_stop,
534         .ndo_start_xmit         = cvm_oct_xmit,
535         .ndo_set_multicast_list = cvm_oct_common_set_multicast_list,
536         .ndo_set_mac_address    = cvm_oct_common_set_mac_address,
537         .ndo_do_ioctl           = cvm_oct_ioctl,
538         .ndo_change_mtu         = cvm_oct_common_change_mtu,
539         .ndo_get_stats          = cvm_oct_common_get_stats,
540 #ifdef CONFIG_NET_POLL_CONTROLLER
541         .ndo_poll_controller    = cvm_oct_poll_controller,
542 #endif
543 };
544 static const struct net_device_ops cvm_oct_sgmii_netdev_ops = {
545         .ndo_init               = cvm_oct_sgmii_init,
546         .ndo_uninit             = cvm_oct_sgmii_uninit,
547         .ndo_open               = cvm_oct_sgmii_open,
548         .ndo_stop               = cvm_oct_sgmii_stop,
549         .ndo_start_xmit         = cvm_oct_xmit,
550         .ndo_set_multicast_list = cvm_oct_common_set_multicast_list,
551         .ndo_set_mac_address    = cvm_oct_common_set_mac_address,
552         .ndo_do_ioctl           = cvm_oct_ioctl,
553         .ndo_change_mtu         = cvm_oct_common_change_mtu,
554         .ndo_get_stats          = cvm_oct_common_get_stats,
555 #ifdef CONFIG_NET_POLL_CONTROLLER
556         .ndo_poll_controller    = cvm_oct_poll_controller,
557 #endif
558 };
559 static const struct net_device_ops cvm_oct_spi_netdev_ops = {
560         .ndo_init               = cvm_oct_spi_init,
561         .ndo_uninit             = cvm_oct_spi_uninit,
562         .ndo_start_xmit         = cvm_oct_xmit,
563         .ndo_set_multicast_list = cvm_oct_common_set_multicast_list,
564         .ndo_set_mac_address    = cvm_oct_common_set_mac_address,
565         .ndo_do_ioctl           = cvm_oct_ioctl,
566         .ndo_change_mtu         = cvm_oct_common_change_mtu,
567         .ndo_get_stats          = cvm_oct_common_get_stats,
568 #ifdef CONFIG_NET_POLL_CONTROLLER
569         .ndo_poll_controller    = cvm_oct_poll_controller,
570 #endif
571 };
572 static const struct net_device_ops cvm_oct_rgmii_netdev_ops = {
573         .ndo_init               = cvm_oct_rgmii_init,
574         .ndo_uninit             = cvm_oct_rgmii_uninit,
575         .ndo_open               = cvm_oct_rgmii_open,
576         .ndo_stop               = cvm_oct_rgmii_stop,
577         .ndo_start_xmit         = cvm_oct_xmit,
578         .ndo_set_multicast_list = cvm_oct_common_set_multicast_list,
579         .ndo_set_mac_address    = cvm_oct_common_set_mac_address,
580         .ndo_do_ioctl           = cvm_oct_ioctl,
581         .ndo_change_mtu         = cvm_oct_common_change_mtu,
582         .ndo_get_stats          = cvm_oct_common_get_stats,
583 #ifdef CONFIG_NET_POLL_CONTROLLER
584         .ndo_poll_controller    = cvm_oct_poll_controller,
585 #endif
586 };
587 static const struct net_device_ops cvm_oct_pow_netdev_ops = {
588         .ndo_init               = cvm_oct_common_init,
589         .ndo_start_xmit         = cvm_oct_xmit_pow,
590         .ndo_set_multicast_list = cvm_oct_common_set_multicast_list,
591         .ndo_set_mac_address    = cvm_oct_common_set_mac_address,
592         .ndo_do_ioctl           = cvm_oct_ioctl,
593         .ndo_change_mtu         = cvm_oct_common_change_mtu,
594         .ndo_get_stats          = cvm_oct_common_get_stats,
595 #ifdef CONFIG_NET_POLL_CONTROLLER
596         .ndo_poll_controller    = cvm_oct_poll_controller,
597 #endif
598 };
599
600 extern void octeon_mdiobus_force_mod_depencency(void);
601
602 /**
603  * Module/ driver initialization. Creates the linux network
604  * devices.
605  *
606  * Returns Zero on success
607  */
608 static int __init cvm_oct_init_module(void)
609 {
610         int num_interfaces;
611         int interface;
612         int fau = FAU_NUM_PACKET_BUFFERS_TO_FREE;
613         int qos;
614
615         octeon_mdiobus_force_mod_depencency();
616         pr_notice("cavium-ethernet %s\n", OCTEON_ETHERNET_VERSION);
617
618         if (OCTEON_IS_MODEL(OCTEON_CN52XX))
619                 cvm_oct_mac_addr_offset = 2; /* First two are the mgmt ports. */
620         else if (OCTEON_IS_MODEL(OCTEON_CN56XX))
621                 cvm_oct_mac_addr_offset = 1; /* First one is the mgmt port. */
622         else
623                 cvm_oct_mac_addr_offset = 0;
624
625         cvm_oct_proc_initialize();
626         cvm_oct_configure_common_hw();
627
628         cvmx_helper_initialize_packet_io_global();
629
630         /* Change the input group for all ports before input is enabled */
631         num_interfaces = cvmx_helper_get_number_of_interfaces();
632         for (interface = 0; interface < num_interfaces; interface++) {
633                 int num_ports = cvmx_helper_ports_on_interface(interface);
634                 int port;
635
636                 for (port = cvmx_helper_get_ipd_port(interface, 0);
637                      port < cvmx_helper_get_ipd_port(interface, num_ports);
638                      port++) {
639                         union cvmx_pip_prt_tagx pip_prt_tagx;
640                         pip_prt_tagx.u64 =
641                             cvmx_read_csr(CVMX_PIP_PRT_TAGX(port));
642                         pip_prt_tagx.s.grp = pow_receive_group;
643                         cvmx_write_csr(CVMX_PIP_PRT_TAGX(port),
644                                        pip_prt_tagx.u64);
645                 }
646         }
647
648         cvmx_helper_ipd_and_packet_input_enable();
649
650         memset(cvm_oct_device, 0, sizeof(cvm_oct_device));
651
652         /*
653          * Initialize the FAU used for counting packet buffers that
654          * need to be freed.
655          */
656         cvmx_fau_atomic_write32(FAU_NUM_PACKET_BUFFERS_TO_FREE, 0);
657
658         if ((pow_send_group != -1)) {
659                 struct net_device *dev;
660                 pr_info("\tConfiguring device for POW only access\n");
661                 dev = alloc_etherdev(sizeof(struct octeon_ethernet));
662                 if (dev) {
663                         /* Initialize the device private structure. */
664                         struct octeon_ethernet *priv = netdev_priv(dev);
665
666                         hrtimer_init(&priv->tx_restart_timer,
667                                      CLOCK_MONOTONIC,
668                                      HRTIMER_MODE_REL);
669                         priv->tx_restart_timer.function = cvm_oct_restart_tx;
670
671                         /*
672                          * Default for 10GE 5000nS enough time to
673                          * transmit about 100 64byte packtes.  1GE
674                          * interfaces will get 50000nS below.
675                          */
676                         priv->tx_restart_interval = ktime_set(0, 5000);
677
678                         dev->netdev_ops = &cvm_oct_pow_netdev_ops;
679                         priv->imode = CVMX_HELPER_INTERFACE_MODE_DISABLED;
680                         priv->port = CVMX_PIP_NUM_INPUT_PORTS;
681                         priv->queue = -1;
682                         strcpy(dev->name, "pow%d");
683                         for (qos = 0; qos < 16; qos++)
684                                 skb_queue_head_init(&priv->tx_free_list[qos]);
685
686                         if (register_netdev(dev) < 0) {
687                                 pr_err("Failed to register ethernet device for POW\n");
688                                 kfree(dev);
689                         } else {
690                                 cvm_oct_device[CVMX_PIP_NUM_INPUT_PORTS] = dev;
691                                 pr_info("%s: POW send group %d, receive group %d\n",
692                                         dev->name, pow_send_group,
693                                         pow_receive_group);
694                         }
695                 } else {
696                         pr_err("Failed to allocate ethernet device for POW\n");
697                 }
698         }
699
700         num_interfaces = cvmx_helper_get_number_of_interfaces();
701         for (interface = 0; interface < num_interfaces; interface++) {
702                 cvmx_helper_interface_mode_t imode =
703                     cvmx_helper_interface_get_mode(interface);
704                 int num_ports = cvmx_helper_ports_on_interface(interface);
705                 int port;
706
707                 for (port = cvmx_helper_get_ipd_port(interface, 0);
708                      port < cvmx_helper_get_ipd_port(interface, num_ports);
709                      port++) {
710                         struct octeon_ethernet *priv;
711                         struct net_device *dev =
712                             alloc_etherdev(sizeof(struct octeon_ethernet));
713                         if (!dev) {
714                                 pr_err("Failed to allocate ethernet device for port %d\n", port);
715                                 continue;
716                         }
717
718                         /* Initialize the device private structure. */
719                         priv = netdev_priv(dev);
720                         memset(priv, 0, sizeof(struct octeon_ethernet));
721
722                         priv->imode = imode;
723                         priv->port = port;
724                         priv->queue = cvmx_pko_get_base_queue(priv->port);
725                         priv->fau = fau - cvmx_pko_get_num_queues(port) * 4;
726                         for (qos = 0; qos < 16; qos++)
727                                 skb_queue_head_init(&priv->tx_free_list[qos]);
728                         for (qos = 0; qos < cvmx_pko_get_num_queues(port);
729                              qos++)
730                                 cvmx_fau_atomic_write32(priv->fau + qos * 4, 0);
731
732                         switch (priv->imode) {
733
734                         /* These types don't support ports to IPD/PKO */
735                         case CVMX_HELPER_INTERFACE_MODE_DISABLED:
736                         case CVMX_HELPER_INTERFACE_MODE_PCIE:
737                         case CVMX_HELPER_INTERFACE_MODE_PICMG:
738                                 break;
739
740                         case CVMX_HELPER_INTERFACE_MODE_NPI:
741                                 dev->netdev_ops = &cvm_oct_npi_netdev_ops;
742                                 strcpy(dev->name, "npi%d");
743                                 break;
744
745                         case CVMX_HELPER_INTERFACE_MODE_XAUI:
746                                 dev->netdev_ops = &cvm_oct_xaui_netdev_ops;
747                                 strcpy(dev->name, "xaui%d");
748                                 break;
749
750                         case CVMX_HELPER_INTERFACE_MODE_LOOP:
751                                 dev->netdev_ops = &cvm_oct_npi_netdev_ops;
752                                 strcpy(dev->name, "loop%d");
753                                 break;
754
755                         case CVMX_HELPER_INTERFACE_MODE_SGMII:
756                                 dev->netdev_ops = &cvm_oct_sgmii_netdev_ops;
757                                 priv->tx_restart_interval = ktime_set(0, 50000);
758                                 strcpy(dev->name, "eth%d");
759                                 break;
760
761                         case CVMX_HELPER_INTERFACE_MODE_SPI:
762                                 dev->netdev_ops = &cvm_oct_spi_netdev_ops;
763                                 strcpy(dev->name, "spi%d");
764                                 break;
765
766                         case CVMX_HELPER_INTERFACE_MODE_RGMII:
767                         case CVMX_HELPER_INTERFACE_MODE_GMII:
768                                 dev->netdev_ops = &cvm_oct_rgmii_netdev_ops;
769                                 priv->tx_restart_interval = ktime_set(0, 50000);
770                                 strcpy(dev->name, "eth%d");
771                                 break;
772                         }
773
774                         if (!dev->netdev_ops) {
775                                 kfree(dev);
776                         } else if (register_netdev(dev) < 0) {
777                                 pr_err("Failed to register ethernet device "
778                                          "for interface %d, port %d\n",
779                                          interface, priv->port);
780                                 kfree(dev);
781                         } else {
782                                 cvm_oct_device[priv->port] = dev;
783                                 fau -=
784                                     cvmx_pko_get_num_queues(priv->port) *
785                                     sizeof(uint32_t);
786                         }
787                 }
788         }
789
790         cvm_oct_rx_initialize();
791
792         /* Enable the poll timer for checking RGMII status */
793         init_timer(&cvm_oct_poll_timer);
794         cvm_oct_poll_timer.data = 0;
795         cvm_oct_poll_timer.function = cvm_do_timer;
796         mod_timer(&cvm_oct_poll_timer, jiffies + HZ);
797
798         return 0;
799 }
800
801 /**
802  * Module / driver shutdown
803  *
804  * Returns Zero on success
805  */
806 static void __exit cvm_oct_cleanup_module(void)
807 {
808         int port;
809
810         /* Disable POW interrupt */
811         cvmx_write_csr(CVMX_POW_WQ_INT_THRX(pow_receive_group), 0);
812
813         cvmx_ipd_disable();
814
815         /* Free the interrupt handler */
816         free_irq(OCTEON_IRQ_WORKQ0 + pow_receive_group, cvm_oct_device);
817
818         del_timer(&cvm_oct_poll_timer);
819         cvm_oct_rx_shutdown();
820         cvmx_pko_disable();
821
822         /* Free the ethernet devices */
823         for (port = 0; port < TOTAL_NUMBER_OF_PORTS; port++) {
824                 if (cvm_oct_device[port]) {
825                         cvm_oct_tx_shutdown(cvm_oct_device[port]);
826                         unregister_netdev(cvm_oct_device[port]);
827                         kfree(cvm_oct_device[port]);
828                         cvm_oct_device[port] = NULL;
829                 }
830         }
831
832         cvmx_pko_shutdown();
833         cvm_oct_proc_shutdown();
834
835         cvmx_ipd_free_ptr();
836
837         /* Free the HW pools */
838         cvm_oct_mem_empty_fpa(CVMX_FPA_PACKET_POOL, CVMX_FPA_PACKET_POOL_SIZE,
839                               num_packet_buffers);
840         cvm_oct_mem_empty_fpa(CVMX_FPA_WQE_POOL, CVMX_FPA_WQE_POOL_SIZE,
841                               num_packet_buffers);
842         if (CVMX_FPA_OUTPUT_BUFFER_POOL != CVMX_FPA_PACKET_POOL)
843                 cvm_oct_mem_empty_fpa(CVMX_FPA_OUTPUT_BUFFER_POOL,
844                                       CVMX_FPA_OUTPUT_BUFFER_POOL_SIZE, 128);
845 }
846
847 MODULE_LICENSE("GPL");
848 MODULE_AUTHOR("Cavium Networks <support@caviumnetworks.com>");
849 MODULE_DESCRIPTION("Cavium Networks Octeon ethernet driver.");
850 module_init(cvm_oct_init_module);
851 module_exit(cvm_oct_cleanup_module);