Merge branch 'for-2.6.31' of git://git.kernel.dk/linux-2.6-block
[linux-2.6] / drivers / net / qlge / qlge_ethtool.c
1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/types.h>
4 #include <linux/module.h>
5 #include <linux/list.h>
6 #include <linux/pci.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/pagemap.h>
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11 #include <linux/dmapool.h>
12 #include <linux/mempool.h>
13 #include <linux/spinlock.h>
14 #include <linux/kthread.h>
15 #include <linux/interrupt.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/in.h>
19 #include <linux/ip.h>
20 #include <linux/ipv6.h>
21 #include <net/ipv6.h>
22 #include <linux/tcp.h>
23 #include <linux/udp.h>
24 #include <linux/if_arp.h>
25 #include <linux/if_ether.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ethtool.h>
29 #include <linux/skbuff.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/if_vlan.h>
32 #include <linux/delay.h>
33 #include <linux/mm.h>
34 #include <linux/vmalloc.h>
35
36
37 #include "qlge.h"
38
39 static int ql_update_ring_coalescing(struct ql_adapter *qdev)
40 {
41         int i, status = 0;
42         struct rx_ring *rx_ring;
43         struct cqicb *cqicb;
44
45         if (!netif_running(qdev->ndev))
46                 return status;
47
48         spin_lock(&qdev->hw_lock);
49         /* Skip the default queue, and update the outbound handler
50          * queues if they changed.
51          */
52         cqicb = (struct cqicb *)&qdev->rx_ring[1];
53         if (le16_to_cpu(cqicb->irq_delay) != qdev->tx_coalesce_usecs ||
54             le16_to_cpu(cqicb->pkt_delay) != qdev->tx_max_coalesced_frames) {
55                 for (i = 1; i < qdev->rss_ring_first_cq_id; i++, rx_ring++) {
56                         rx_ring = &qdev->rx_ring[i];
57                         cqicb = (struct cqicb *)rx_ring;
58                         cqicb->irq_delay = cpu_to_le16(qdev->tx_coalesce_usecs);
59                         cqicb->pkt_delay =
60                             cpu_to_le16(qdev->tx_max_coalesced_frames);
61                         cqicb->flags = FLAGS_LI;
62                         status = ql_write_cfg(qdev, cqicb, sizeof(cqicb),
63                                                 CFG_LCQ, rx_ring->cq_id);
64                         if (status) {
65                                 QPRINTK(qdev, IFUP, ERR,
66                                         "Failed to load CQICB.\n");
67                                 goto exit;
68                         }
69                 }
70         }
71
72         /* Update the inbound (RSS) handler queues if they changed. */
73         cqicb = (struct cqicb *)&qdev->rx_ring[qdev->rss_ring_first_cq_id];
74         if (le16_to_cpu(cqicb->irq_delay) != qdev->rx_coalesce_usecs ||
75             le16_to_cpu(cqicb->pkt_delay) != qdev->rx_max_coalesced_frames) {
76                 for (i = qdev->rss_ring_first_cq_id;
77                      i <= qdev->rss_ring_first_cq_id + qdev->rss_ring_count;
78                      i++) {
79                         rx_ring = &qdev->rx_ring[i];
80                         cqicb = (struct cqicb *)rx_ring;
81                         cqicb->irq_delay = cpu_to_le16(qdev->rx_coalesce_usecs);
82                         cqicb->pkt_delay =
83                             cpu_to_le16(qdev->rx_max_coalesced_frames);
84                         cqicb->flags = FLAGS_LI;
85                         status = ql_write_cfg(qdev, cqicb, sizeof(cqicb),
86                                                 CFG_LCQ, rx_ring->cq_id);
87                         if (status) {
88                                 QPRINTK(qdev, IFUP, ERR,
89                                         "Failed to load CQICB.\n");
90                                 goto exit;
91                         }
92                 }
93         }
94 exit:
95         spin_unlock(&qdev->hw_lock);
96         return status;
97 }
98
99 static void ql_update_stats(struct ql_adapter *qdev)
100 {
101         u32 i;
102         u64 data;
103         u64 *iter = &qdev->nic_stats.tx_pkts;
104
105         spin_lock(&qdev->stats_lock);
106         if (ql_sem_spinlock(qdev, qdev->xg_sem_mask)) {
107                         QPRINTK(qdev, DRV, ERR,
108                                 "Couldn't get xgmac sem.\n");
109                 goto quit;
110         }
111         /*
112          * Get TX statistics.
113          */
114         for (i = 0x200; i < 0x280; i += 8) {
115                 if (ql_read_xgmac_reg64(qdev, i, &data)) {
116                         QPRINTK(qdev, DRV, ERR,
117                                 "Error reading status register 0x%.04x.\n", i);
118                         goto end;
119                 } else
120                         *iter = data;
121                 iter++;
122         }
123
124         /*
125          * Get RX statistics.
126          */
127         for (i = 0x300; i < 0x3d0; i += 8) {
128                 if (ql_read_xgmac_reg64(qdev, i, &data)) {
129                         QPRINTK(qdev, DRV, ERR,
130                                 "Error reading status register 0x%.04x.\n", i);
131                         goto end;
132                 } else
133                         *iter = data;
134                 iter++;
135         }
136
137 end:
138         ql_sem_unlock(qdev, qdev->xg_sem_mask);
139 quit:
140         spin_unlock(&qdev->stats_lock);
141
142         QL_DUMP_STAT(qdev);
143
144         return;
145 }
146
147 static char ql_stats_str_arr[][ETH_GSTRING_LEN] = {
148         {"tx_pkts"},
149         {"tx_bytes"},
150         {"tx_mcast_pkts"},
151         {"tx_bcast_pkts"},
152         {"tx_ucast_pkts"},
153         {"tx_ctl_pkts"},
154         {"tx_pause_pkts"},
155         {"tx_64_pkts"},
156         {"tx_65_to_127_pkts"},
157         {"tx_128_to_255_pkts"},
158         {"tx_256_511_pkts"},
159         {"tx_512_to_1023_pkts"},
160         {"tx_1024_to_1518_pkts"},
161         {"tx_1519_to_max_pkts"},
162         {"tx_undersize_pkts"},
163         {"tx_oversize_pkts"},
164         {"rx_bytes"},
165         {"rx_bytes_ok"},
166         {"rx_pkts"},
167         {"rx_pkts_ok"},
168         {"rx_bcast_pkts"},
169         {"rx_mcast_pkts"},
170         {"rx_ucast_pkts"},
171         {"rx_undersize_pkts"},
172         {"rx_oversize_pkts"},
173         {"rx_jabber_pkts"},
174         {"rx_undersize_fcerr_pkts"},
175         {"rx_drop_events"},
176         {"rx_fcerr_pkts"},
177         {"rx_align_err"},
178         {"rx_symbol_err"},
179         {"rx_mac_err"},
180         {"rx_ctl_pkts"},
181         {"rx_pause_pkts"},
182         {"rx_64_pkts"},
183         {"rx_65_to_127_pkts"},
184         {"rx_128_255_pkts"},
185         {"rx_256_511_pkts"},
186         {"rx_512_to_1023_pkts"},
187         {"rx_1024_to_1518_pkts"},
188         {"rx_1519_to_max_pkts"},
189         {"rx_len_err_pkts"},
190 };
191
192 static void ql_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
193 {
194         switch (stringset) {
195         case ETH_SS_STATS:
196                 memcpy(buf, ql_stats_str_arr, sizeof(ql_stats_str_arr));
197                 break;
198         }
199 }
200
201 static int ql_get_sset_count(struct net_device *dev, int sset)
202 {
203         switch (sset) {
204         case ETH_SS_STATS:
205                 return ARRAY_SIZE(ql_stats_str_arr);
206         default:
207                 return -EOPNOTSUPP;
208         }
209 }
210
211 static void
212 ql_get_ethtool_stats(struct net_device *ndev,
213                      struct ethtool_stats *stats, u64 *data)
214 {
215         struct ql_adapter *qdev = netdev_priv(ndev);
216         struct nic_stats *s = &qdev->nic_stats;
217
218         ql_update_stats(qdev);
219
220         *data++ = s->tx_pkts;
221         *data++ = s->tx_bytes;
222         *data++ = s->tx_mcast_pkts;
223         *data++ = s->tx_bcast_pkts;
224         *data++ = s->tx_ucast_pkts;
225         *data++ = s->tx_ctl_pkts;
226         *data++ = s->tx_pause_pkts;
227         *data++ = s->tx_64_pkt;
228         *data++ = s->tx_65_to_127_pkt;
229         *data++ = s->tx_128_to_255_pkt;
230         *data++ = s->tx_256_511_pkt;
231         *data++ = s->tx_512_to_1023_pkt;
232         *data++ = s->tx_1024_to_1518_pkt;
233         *data++ = s->tx_1519_to_max_pkt;
234         *data++ = s->tx_undersize_pkt;
235         *data++ = s->tx_oversize_pkt;
236         *data++ = s->rx_bytes;
237         *data++ = s->rx_bytes_ok;
238         *data++ = s->rx_pkts;
239         *data++ = s->rx_pkts_ok;
240         *data++ = s->rx_bcast_pkts;
241         *data++ = s->rx_mcast_pkts;
242         *data++ = s->rx_ucast_pkts;
243         *data++ = s->rx_undersize_pkts;
244         *data++ = s->rx_oversize_pkts;
245         *data++ = s->rx_jabber_pkts;
246         *data++ = s->rx_undersize_fcerr_pkts;
247         *data++ = s->rx_drop_events;
248         *data++ = s->rx_fcerr_pkts;
249         *data++ = s->rx_align_err;
250         *data++ = s->rx_symbol_err;
251         *data++ = s->rx_mac_err;
252         *data++ = s->rx_ctl_pkts;
253         *data++ = s->rx_pause_pkts;
254         *data++ = s->rx_64_pkts;
255         *data++ = s->rx_65_to_127_pkts;
256         *data++ = s->rx_128_255_pkts;
257         *data++ = s->rx_256_511_pkts;
258         *data++ = s->rx_512_to_1023_pkts;
259         *data++ = s->rx_1024_to_1518_pkts;
260         *data++ = s->rx_1519_to_max_pkts;
261         *data++ = s->rx_len_err_pkts;
262 }
263
264 static int ql_get_settings(struct net_device *ndev,
265                               struct ethtool_cmd *ecmd)
266 {
267         struct ql_adapter *qdev = netdev_priv(ndev);
268
269         ecmd->supported = SUPPORTED_10000baseT_Full;
270         ecmd->advertising = ADVERTISED_10000baseT_Full;
271         ecmd->autoneg = AUTONEG_ENABLE;
272         ecmd->transceiver = XCVR_EXTERNAL;
273         if ((qdev->link_status & STS_LINK_TYPE_MASK) ==
274                                 STS_LINK_TYPE_10GBASET) {
275                 ecmd->supported |= (SUPPORTED_TP | SUPPORTED_Autoneg);
276                 ecmd->advertising |= (ADVERTISED_TP | ADVERTISED_Autoneg);
277                 ecmd->port = PORT_TP;
278         } else {
279                 ecmd->supported |= SUPPORTED_FIBRE;
280                 ecmd->advertising |= ADVERTISED_FIBRE;
281                 ecmd->port = PORT_FIBRE;
282         }
283
284         ecmd->speed = SPEED_10000;
285         ecmd->duplex = DUPLEX_FULL;
286
287         return 0;
288 }
289
290 static void ql_get_drvinfo(struct net_device *ndev,
291                            struct ethtool_drvinfo *drvinfo)
292 {
293         struct ql_adapter *qdev = netdev_priv(ndev);
294         strncpy(drvinfo->driver, qlge_driver_name, 32);
295         strncpy(drvinfo->version, qlge_driver_version, 32);
296         strncpy(drvinfo->fw_version, "N/A", 32);
297         strncpy(drvinfo->bus_info, pci_name(qdev->pdev), 32);
298         drvinfo->n_stats = 0;
299         drvinfo->testinfo_len = 0;
300         drvinfo->regdump_len = 0;
301         drvinfo->eedump_len = 0;
302 }
303
304 static int ql_get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
305 {
306         struct ql_adapter *qdev = netdev_priv(dev);
307
308         c->rx_coalesce_usecs = qdev->rx_coalesce_usecs;
309         c->tx_coalesce_usecs = qdev->tx_coalesce_usecs;
310
311         /* This chip coalesces as follows:
312          * If a packet arrives, hold off interrupts until
313          * cqicb->int_delay expires, but if no other packets arrive don't
314          * wait longer than cqicb->pkt_int_delay. But ethtool doesn't use a
315          * timer to coalesce on a frame basis.  So, we have to take ethtool's
316          * max_coalesced_frames value and convert it to a delay in microseconds.
317          * We do this by using a basic thoughput of 1,000,000 frames per
318          * second @ (1024 bytes).  This means one frame per usec. So it's a
319          * simple one to one ratio.
320          */
321         c->rx_max_coalesced_frames = qdev->rx_max_coalesced_frames;
322         c->tx_max_coalesced_frames = qdev->tx_max_coalesced_frames;
323
324         return 0;
325 }
326
327 static int ql_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *c)
328 {
329         struct ql_adapter *qdev = netdev_priv(ndev);
330
331         /* Validate user parameters. */
332         if (c->rx_coalesce_usecs > qdev->rx_ring_size / 2)
333                 return -EINVAL;
334        /* Don't wait more than 10 usec. */
335         if (c->rx_max_coalesced_frames > MAX_INTER_FRAME_WAIT)
336                 return -EINVAL;
337         if (c->tx_coalesce_usecs > qdev->tx_ring_size / 2)
338                 return -EINVAL;
339         if (c->tx_max_coalesced_frames > MAX_INTER_FRAME_WAIT)
340                 return -EINVAL;
341
342         /* Verify a change took place before updating the hardware. */
343         if (qdev->rx_coalesce_usecs == c->rx_coalesce_usecs &&
344             qdev->tx_coalesce_usecs == c->tx_coalesce_usecs &&
345             qdev->rx_max_coalesced_frames == c->rx_max_coalesced_frames &&
346             qdev->tx_max_coalesced_frames == c->tx_max_coalesced_frames)
347                 return 0;
348
349         qdev->rx_coalesce_usecs = c->rx_coalesce_usecs;
350         qdev->tx_coalesce_usecs = c->tx_coalesce_usecs;
351         qdev->rx_max_coalesced_frames = c->rx_max_coalesced_frames;
352         qdev->tx_max_coalesced_frames = c->tx_max_coalesced_frames;
353
354         return ql_update_ring_coalescing(qdev);
355 }
356
357 static u32 ql_get_rx_csum(struct net_device *netdev)
358 {
359         struct ql_adapter *qdev = netdev_priv(netdev);
360         return qdev->rx_csum;
361 }
362
363 static int ql_set_rx_csum(struct net_device *netdev, uint32_t data)
364 {
365         struct ql_adapter *qdev = netdev_priv(netdev);
366         qdev->rx_csum = data;
367         return 0;
368 }
369
370 static int ql_set_tso(struct net_device *ndev, uint32_t data)
371 {
372
373         if (data) {
374                 ndev->features |= NETIF_F_TSO;
375                 ndev->features |= NETIF_F_TSO6;
376         } else {
377                 ndev->features &= ~NETIF_F_TSO;
378                 ndev->features &= ~NETIF_F_TSO6;
379         }
380         return 0;
381 }
382
383 static u32 ql_get_msglevel(struct net_device *ndev)
384 {
385         struct ql_adapter *qdev = netdev_priv(ndev);
386         return qdev->msg_enable;
387 }
388
389 static void ql_set_msglevel(struct net_device *ndev, u32 value)
390 {
391         struct ql_adapter *qdev = netdev_priv(ndev);
392         qdev->msg_enable = value;
393 }
394
395 const struct ethtool_ops qlge_ethtool_ops = {
396         .get_settings = ql_get_settings,
397         .get_drvinfo = ql_get_drvinfo,
398         .get_msglevel = ql_get_msglevel,
399         .set_msglevel = ql_set_msglevel,
400         .get_link = ethtool_op_get_link,
401         .get_rx_csum = ql_get_rx_csum,
402         .set_rx_csum = ql_set_rx_csum,
403         .get_tx_csum = ethtool_op_get_tx_csum,
404         .get_sg = ethtool_op_get_sg,
405         .set_sg = ethtool_op_set_sg,
406         .get_tso = ethtool_op_get_tso,
407         .set_tso = ql_set_tso,
408         .get_coalesce = ql_get_coalesce,
409         .set_coalesce = ql_set_coalesce,
410         .get_sset_count = ql_get_sset_count,
411         .get_strings = ql_get_strings,
412         .get_ethtool_stats = ql_get_ethtool_stats,
413 };
414