Staging: sxg: Ethtool framework and Receive code path changes
[cascardo/linux.git] / drivers / staging / sxg / sxg_ethtool.c
1 /**************************************************************************
2  *
3  * Copyright (C) 2000-2008 Alacritech, Inc.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above
12  *    copyright notice, this list of conditions and the following
13  *    disclaimer in the documentation and/or other materials provided
14  *    with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY ALACRITECH, INC. ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ALACRITECH, INC. OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * The views and conclusions contained in the software and documentation
30  * are those of the authors and should not be interpreted as representing
31  * official policies, either expressed or implied, of Alacritech, Inc.
32  *
33  **************************************************************************/
34
35 /*
36  * FILENAME: sxg_ethtool.c
37  *
38  * The ethtool support for SXG driver for Alacritech's 10Gbe products.
39  *
40  * NOTE: This is the standard, non-accelerated version of Alacritech's
41  *       IS-NIC driver.
42  */
43 #include <linux/kernel.h>
44 #include <linux/errno.h>
45 #include <linux/module.h>
46 #include <linux/netdevice.h>
47 #include <linux/etherdevice.h>
48 #include <linux/ethtool.h>
49 #include <linux/skbuff.h>
50 #include <linux/pci.h>
51
52 #include "sxg_os.h"
53 #include "sxghw.h"
54 #include "sxghif.h"
55 #include "sxg.h"
56 //#include "sxg.c"
57 #include "saharadbgdownload.h"
58
59 struct sxg_nic_stats {
60         char stat_string[ETH_GSTRING_LEN];
61         int sizeof_stat;
62         int stat_offset;
63 };
64
65 #define SXG_NIC_STATS(m) sizeof(((struct adapter_t *)0)->m), \
66                                 offsetof(struct adapter_t, m)
67
68 #define USER_VIEWABLE_EEPROM_SIZE       28
69
70 static struct sxg_nic_stats sxg_nic_gstrings_stats[] = {
71         {"xmit_ring_0_full", SXG_NIC_STATS(Stats.XmtZeroFull)},
72
73         /* May be will need in future */
74 /*      {"dumb_xmit_broadcast_packets", SXG_NIC_STATS(Stats.DumbXmtBcastPkts)},
75         {"dumb_xmit_broadcast_bytes", SXG_NIC_STATS(Stats.DumbXmtBcastBytes)},
76         {"dumb_xmit_unicast_packets", SXG_NIC_STATS(Stats.DumbXmtUcastPkts)},
77         {"dumb_xmit_unicast_bytes", SXG_NIC_STATS(Stats.DumbXmtUcastBytes)},
78 */
79         {"xmit_queue_length", SXG_NIC_STATS(Stats.XmtQLen)},
80         {"memory_allocation_failure", SXG_NIC_STATS(Stats.NoMem)},
81         {"Interrupts", SXG_NIC_STATS(Stats.NumInts)},
82         {"false_interrupts", SXG_NIC_STATS(Stats.FalseInts)},
83         {"processed_data_queue_full", SXG_NIC_STATS(Stats.PdqFull)},
84         {"event_ring_full", SXG_NIC_STATS(Stats.EventRingFull)},
85         {"transport_checksum_error", SXG_NIC_STATS(Stats.TransportCsum)},
86         {"transport_underflow_error", SXG_NIC_STATS(Stats.TransportUflow)},
87         {"transport_header_length_error", SXG_NIC_STATS(Stats.TransportHdrLen)},
88         {"network_checksum_error", SXG_NIC_STATS(Stats.NetworkCsum)},
89         {"network_underflow_error", SXG_NIC_STATS(Stats.NetworkUflow)},
90         {"network_header_length_error", SXG_NIC_STATS(Stats.NetworkHdrLen)},
91         {"receive_parity_error", SXG_NIC_STATS(Stats.Parity)},
92         {"link_parity_error", SXG_NIC_STATS(Stats.LinkParity)},
93         {"link/data early_error", SXG_NIC_STATS(Stats.LinkEarly)},
94         {"buffer_overflow_error", SXG_NIC_STATS(Stats.LinkBufOflow)},
95         {"link_code_error", SXG_NIC_STATS(Stats.LinkCode)},
96         {"dribble nibble", SXG_NIC_STATS(Stats.LinkDribble)},
97         {"CRC_error", SXG_NIC_STATS(Stats.LinkCrc)},
98         {"link_overflow_error", SXG_NIC_STATS(Stats.LinkOflow)},
99         {"link_underflow_error", SXG_NIC_STATS(Stats.LinkUflow)},
100
101         /* May be need in future */
102 /*      {"dumb_rcv_broadcast_packets", SXG_NIC_STATS(Stats.DumbRcvBcastPkts)},
103         {"dumb_rcv_broadcast_bytes", SXG_NIC_STATS(Stats.DumbRcvBcastBytes)},
104         {"dumb_rcv_multicast_packets", SXG_NIC_STATS(Stats.DumbRcvMcastPkts)},
105         {"dumb_rcv_multicast_bytes", SXG_NIC_STATS(Stats.DumbRcvMcastBytes)},
106         {"dumb_rcv_unicast_packets", SXG_NIC_STATS(Stats.DumbRcvUcastPkts)},
107         {"dumb_rcv_unicast_bytes", SXG_NIC_STATS(Stats.DumbRcvUcastBytes)},
108 */
109         {"no_sgl_buffer", SXG_NIC_STATS(Stats.NoSglBuf)},
110 };
111
112 #define SXG_NIC_STATS_LEN       ARRAY_SIZE(sxg_nic_gstrings_stats)
113
114 static inline void sxg_reg32_write(void __iomem *reg, u32 value, bool flush)
115 {
116         writel(value, reg);
117         if (flush)
118                 mb();
119 }
120
121 static inline void sxg_reg64_write(struct adapter_t *adapter, void __iomem *reg,
122                                    u64 value, u32 cpu)
123 {
124         u32 value_high = (u32) (value >> 32);
125         u32 value_low = (u32) (value & 0x00000000FFFFFFFF);
126         unsigned long flags;
127
128         spin_lock_irqsave(&adapter->Bit64RegLock, flags);
129         writel(value_high, (void __iomem *)(&adapter->UcodeRegs[cpu].Upper));
130         writel(value_low, reg);
131         spin_unlock_irqrestore(&adapter->Bit64RegLock, flags);
132 }
133
134 static void
135 sxg_nic_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *drvinfo)
136 {
137         struct adapter_t *adapter = netdev_priv(dev);
138         strncpy(drvinfo->driver, sxg_driver_name, 32);
139         strncpy(drvinfo->version, SXG_DRV_VERSION, 32);
140         strncpy(drvinfo->fw_version, SAHARA_UCODE_VERS_STRING, 32);
141         strncpy(drvinfo->bus_info, pci_name(adapter->pcidev), 32);
142         /* TODO : Read the major and minor number of firmware. Is this
143          * from the FLASH/EEPROM or download file ?
144          */
145         /* LINSYS : Check if this is correct or if not find the right value
146          * Also check what is the right EEPROM length : EEPROM_SIZE_XFMR or EEPROM_SIZE_NO_XFMR
147          */
148 }
149
150 static int sxg_nic_set_settings(struct net_device *netdev,
151                               struct ethtool_cmd *ecmd)
152 {
153         /* No settings are applicable as we support only 10Gb/FIBRE_media */
154         return -EOPNOTSUPP;
155 }
156
157 static int
158 sxg_nic_get_strings(struct net_device *netdev, u32 stringset, u8 * data)
159 {
160         int index;
161
162         switch(stringset) {
163                 case ETH_SS_TEST:
164                         return -EOPNOTSUPP;
165                 break;
166         case ETH_SS_STATS:
167                 for (index = 0; index < SXG_NIC_STATS_LEN; index++) {
168                         memcpy(data + index * ETH_GSTRING_LEN,
169                                 sxg_nic_gstrings_stats[index].stat_string,
170                                 ETH_GSTRING_LEN);
171                 }
172                 break;
173         }
174 }
175
176 static void
177 sxg_nic_get_ethtool_stats(struct net_device *netdev,
178                         struct ethtool_stats *stats, u64 * data)
179 {
180         struct adapter_t *adapter = netdev_priv(netdev);
181         int index;
182         for (index = 0; index < SXG_NIC_STATS_LEN; index++) {
183                 char *p = (char *)adapter +
184                                 sxg_nic_gstrings_stats[index].stat_offset;
185                 data[index] = (sxg_nic_gstrings_stats[index].sizeof_stat ==
186                                      sizeof(u64)) ? *(u64 *) p : *(u32 *) p;
187         }
188 }
189
190 static int sxg_nic_get_sset_count(struct net_device *netdev, int sset)
191 {
192         switch (sset) {
193         case ETH_SS_STATS:
194                 return SXG_NIC_STATS_LEN;
195         default:
196                 return -EOPNOTSUPP;
197         }
198 }
199
200 static int sxg_nic_get_settings(struct net_device *netdev,
201                                 struct ethtool_cmd *ecmd)
202 {
203         struct adapter_t *adapter = netdev_priv(netdev);
204
205         ecmd->supported = SUPPORTED_10000baseT_Full;
206         ecmd->autoneg = AUTONEG_ENABLE;         //VSS check This
207         ecmd->transceiver = XCVR_EXTERNAL;      //VSS check This
208
209         /* For Fibre Channel */
210         ecmd->supported |= SUPPORTED_FIBRE;
211         ecmd->advertising = (ADVERTISED_10000baseT_Full |
212                                 ADVERTISED_FIBRE);
213         ecmd->port = PORT_FIBRE;
214
215
216         /* Link Speed */
217         if(adapter->LinkState & SXG_LINK_UP) {
218                 ecmd->speed = SPEED_10000;      //adapter->LinkSpeed;
219                 ecmd->duplex = DUPLEX_FULL;
220         }
221         return 0;
222 }
223
224 static int sxg_nic_get_rx_csum(struct net_device *netdev)
225 {
226         struct adapter_t *adapter = netdev_priv(netdev);
227         return ((adapter->flags & SXG_RCV_IP_CSUM_ENABLED) ||
228                  (adapter->flags & SXG_RCV_TCP_CSUM_ENABLED));
229 }
230
231 static int sxg_nic_set_rx_csum(struct net_device *netdev, u32 data)
232 {
233         struct adapter_t *adapter = netdev_priv(netdev);
234         if (data)
235                 adapter->flags |= SXG_RCV_IP_CSUM_ENABLED;
236         else
237                 adapter->flags &= ~SXG_RCV_IP_CSUM_ENABLED;
238
239         /* Reset the card here (call the reset functions .. currently unavailable)*/
240
241         return 0;
242 }
243
244 static int sxg_nic_get_regs_len(struct net_device *dev)
245 {
246         return (SXG_HWREG_MEMSIZE + SXG_UCODEREG_MEMSIZE);
247 }
248
249 static void sxg_nic_get_regs(struct net_device *netdev,
250                         struct ethtool_regs *regs, void *p)
251 {
252         struct adapter_t *adapter = netdev_priv(netdev);
253         struct sxg_hw_regs *HwRegs = adapter->HwRegs;
254         struct sxg_ucode_regs *UcodeRegs = adapter->UcodeRegs;
255         u32 *buff = p;
256
257         memset(p, 0, (sizeof(struct sxg_hw_regs)+sizeof(struct sxg_ucode_regs)));
258         memcpy(buff, HwRegs, sizeof(struct sxg_hw_regs));
259         memcpy((buff+sizeof(struct sxg_hw_regs)), UcodeRegs, sizeof(struct sxg_ucode_regs));
260 }
261
262 static int sxg_nic_get_wol(struct net_device *netdev,
263                                 struct ethtool_wolinfo *wol)
264 {
265         /* We dont support wake-on-lan */
266         return -EOPNOTSUPP;
267 }
268
269 static int sxg_nic_get_eeprom_len(struct net_device *netdev)
270 {
271         return (USER_VIEWABLE_EEPROM_SIZE);
272 }
273
274 static int sxg_nic_get_eeprom(struct net_device *netdev,
275                                 struct ethtool_eeprom *eeprom, u8 *bytes)
276 {
277         struct adapter_t *adapter = netdev_priv(netdev);
278         struct sw_cfg_data *data;
279         unsigned long           i, status;
280         dma_addr_t p_addr;
281
282         data = pci_alloc_consistent(adapter->pcidev, sizeof(struct sw_cfg_data),
283                                          &p_addr);
284         if(!data) {
285                 /*
286                  * We cant get even this much memory. Raise a hell
287                  * Get out of here
288                  */
289                 printk(KERN_ERR"%s : Could not allocate memory for reading \
290                                 EEPROM\n", __FUNCTION__);
291                 return -ENOMEM;
292         }
293
294         WRITE_REG(adapter->UcodeRegs[0].ConfigStat, SXG_CFG_TIMEOUT, TRUE);
295         WRITE_REG64(adapter, adapter->UcodeRegs[0].Config, p_addr, 0);
296         for(i=0; i<1000; i++) {
297                 READ_REG(adapter->UcodeRegs[0].ConfigStat, status);
298                 if (status != SXG_CFG_TIMEOUT)
299                         break;
300                 mdelay(1);      /* Do we really need this */
301         }
302
303         memset(bytes, 0, eeprom->len);
304         memcpy(bytes, data->MacAddr[0].MacAddr, sizeof(struct sxg_config_mac));
305         memcpy(bytes+6, data->AtkFru.PartNum, 6);
306         memcpy(bytes+12, data->AtkFru.Revision, 2);
307         memcpy(bytes+14, data->AtkFru.Serial, 14);
308
309         return 0;
310 }
311
312 struct ethtool_ops sxg_nic_ethtool_ops = {
313         .get_settings = sxg_nic_get_settings,
314         .set_settings = sxg_nic_set_settings,
315         .get_drvinfo = sxg_nic_get_drvinfo,
316         .get_regs_len = sxg_nic_get_regs_len,
317         .get_regs = sxg_nic_get_regs,
318         .get_link = ethtool_op_get_link,
319         .get_wol = sxg_nic_get_wol,
320         .get_eeprom_len = sxg_nic_get_eeprom_len,
321         .get_eeprom = sxg_nic_get_eeprom,
322 //      .get_ringparam = sxg_nic_get_ringparam,
323 //      .get_pauseparam = sxg_nic_get_pauseparam,
324 //      .set_pauseparam = sxg_nic_set_pauseparam,
325         .set_tx_csum = ethtool_op_set_tx_csum,
326         .get_sg = ethtool_op_get_sg,
327         .set_sg = ethtool_op_set_sg,
328 //      .get_tso = sxg_nic_get_tso,
329 //      .set_tso = sxg_nic_set_tso,
330 //      .self_test = sxg_nic_diag_test,
331         .get_strings = sxg_nic_get_strings,
332         .get_ethtool_stats = sxg_nic_get_ethtool_stats,
333         .get_sset_count = sxg_nic_get_sset_count,
334         .get_rx_csum = sxg_nic_get_rx_csum,
335         .set_rx_csum = sxg_nic_set_rx_csum,
336 //      .get_coalesce = sxg_nic_get_intr_coalesce,
337 //      .set_coalesce = sxg_nic_set_intr_coalesce,
338 };