31f40148fa5cbab05d03a178593a667ce517c5e0
[sfrench/cifs-2.6.git] / drivers / net / ethernet / qlogic / qlge / qlge_ethtool.c
1 #include <linux/kernel.h>
2 #include <linux/types.h>
3 #include <linux/module.h>
4 #include <linux/list.h>
5 #include <linux/pci.h>
6 #include <linux/dma-mapping.h>
7 #include <linux/pagemap.h>
8 #include <linux/sched.h>
9 #include <linux/dmapool.h>
10 #include <linux/mempool.h>
11 #include <linux/spinlock.h>
12 #include <linux/kthread.h>
13 #include <linux/interrupt.h>
14 #include <linux/errno.h>
15 #include <linux/ioport.h>
16 #include <linux/in.h>
17 #include <linux/ip.h>
18 #include <linux/ipv6.h>
19 #include <net/ipv6.h>
20 #include <linux/tcp.h>
21 #include <linux/udp.h>
22 #include <linux/if_arp.h>
23 #include <linux/if_ether.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/ethtool.h>
27 #include <linux/skbuff.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/if_vlan.h>
30 #include <linux/delay.h>
31 #include <linux/mm.h>
32 #include <linux/vmalloc.h>
33
34
35 #include "qlge.h"
36
37 struct ql_stats {
38         char stat_string[ETH_GSTRING_LEN];
39         int sizeof_stat;
40         int stat_offset;
41 };
42
43 #define QL_SIZEOF(m) FIELD_SIZEOF(struct ql_adapter, m)
44 #define QL_OFF(m) offsetof(struct ql_adapter, m)
45
46 static const struct ql_stats ql_gstrings_stats[] = {
47         {"tx_pkts", QL_SIZEOF(nic_stats.tx_pkts), QL_OFF(nic_stats.tx_pkts)},
48         {"tx_bytes", QL_SIZEOF(nic_stats.tx_bytes), QL_OFF(nic_stats.tx_bytes)},
49         {"tx_mcast_pkts", QL_SIZEOF(nic_stats.tx_mcast_pkts),
50                                         QL_OFF(nic_stats.tx_mcast_pkts)},
51         {"tx_bcast_pkts", QL_SIZEOF(nic_stats.tx_bcast_pkts),
52                                         QL_OFF(nic_stats.tx_bcast_pkts)},
53         {"tx_ucast_pkts", QL_SIZEOF(nic_stats.tx_ucast_pkts),
54                                         QL_OFF(nic_stats.tx_ucast_pkts)},
55         {"tx_ctl_pkts", QL_SIZEOF(nic_stats.tx_ctl_pkts),
56                                         QL_OFF(nic_stats.tx_ctl_pkts)},
57         {"tx_pause_pkts", QL_SIZEOF(nic_stats.tx_pause_pkts),
58                                         QL_OFF(nic_stats.tx_pause_pkts)},
59         {"tx_64_pkts", QL_SIZEOF(nic_stats.tx_64_pkt),
60                                         QL_OFF(nic_stats.tx_64_pkt)},
61         {"tx_65_to_127_pkts", QL_SIZEOF(nic_stats.tx_65_to_127_pkt),
62                                         QL_OFF(nic_stats.tx_65_to_127_pkt)},
63         {"tx_128_to_255_pkts", QL_SIZEOF(nic_stats.tx_128_to_255_pkt),
64                                         QL_OFF(nic_stats.tx_128_to_255_pkt)},
65         {"tx_256_511_pkts", QL_SIZEOF(nic_stats.tx_256_511_pkt),
66                                         QL_OFF(nic_stats.tx_256_511_pkt)},
67         {"tx_512_to_1023_pkts", QL_SIZEOF(nic_stats.tx_512_to_1023_pkt),
68                                         QL_OFF(nic_stats.tx_512_to_1023_pkt)},
69         {"tx_1024_to_1518_pkts", QL_SIZEOF(nic_stats.tx_1024_to_1518_pkt),
70                                         QL_OFF(nic_stats.tx_1024_to_1518_pkt)},
71         {"tx_1519_to_max_pkts", QL_SIZEOF(nic_stats.tx_1519_to_max_pkt),
72                                         QL_OFF(nic_stats.tx_1519_to_max_pkt)},
73         {"tx_undersize_pkts", QL_SIZEOF(nic_stats.tx_undersize_pkt),
74                                         QL_OFF(nic_stats.tx_undersize_pkt)},
75         {"tx_oversize_pkts", QL_SIZEOF(nic_stats.tx_oversize_pkt),
76                                         QL_OFF(nic_stats.tx_oversize_pkt)},
77         {"rx_bytes", QL_SIZEOF(nic_stats.rx_bytes), QL_OFF(nic_stats.rx_bytes)},
78         {"rx_bytes_ok", QL_SIZEOF(nic_stats.rx_bytes_ok),
79                                         QL_OFF(nic_stats.rx_bytes_ok)},
80         {"rx_pkts", QL_SIZEOF(nic_stats.rx_pkts), QL_OFF(nic_stats.rx_pkts)},
81         {"rx_pkts_ok", QL_SIZEOF(nic_stats.rx_pkts_ok),
82                                         QL_OFF(nic_stats.rx_pkts_ok)},
83         {"rx_bcast_pkts", QL_SIZEOF(nic_stats.rx_bcast_pkts),
84                                         QL_OFF(nic_stats.rx_bcast_pkts)},
85         {"rx_mcast_pkts", QL_SIZEOF(nic_stats.rx_mcast_pkts),
86                                         QL_OFF(nic_stats.rx_mcast_pkts)},
87         {"rx_ucast_pkts", QL_SIZEOF(nic_stats.rx_ucast_pkts),
88                                         QL_OFF(nic_stats.rx_ucast_pkts)},
89         {"rx_undersize_pkts", QL_SIZEOF(nic_stats.rx_undersize_pkts),
90                                         QL_OFF(nic_stats.rx_undersize_pkts)},
91         {"rx_oversize_pkts", QL_SIZEOF(nic_stats.rx_oversize_pkts),
92                                         QL_OFF(nic_stats.rx_oversize_pkts)},
93         {"rx_jabber_pkts", QL_SIZEOF(nic_stats.rx_jabber_pkts),
94                                         QL_OFF(nic_stats.rx_jabber_pkts)},
95         {"rx_undersize_fcerr_pkts",
96                 QL_SIZEOF(nic_stats.rx_undersize_fcerr_pkts),
97                                 QL_OFF(nic_stats.rx_undersize_fcerr_pkts)},
98         {"rx_drop_events", QL_SIZEOF(nic_stats.rx_drop_events),
99                                         QL_OFF(nic_stats.rx_drop_events)},
100         {"rx_fcerr_pkts", QL_SIZEOF(nic_stats.rx_fcerr_pkts),
101                                         QL_OFF(nic_stats.rx_fcerr_pkts)},
102         {"rx_align_err", QL_SIZEOF(nic_stats.rx_align_err),
103                                         QL_OFF(nic_stats.rx_align_err)},
104         {"rx_symbol_err", QL_SIZEOF(nic_stats.rx_symbol_err),
105                                         QL_OFF(nic_stats.rx_symbol_err)},
106         {"rx_mac_err", QL_SIZEOF(nic_stats.rx_mac_err),
107                                         QL_OFF(nic_stats.rx_mac_err)},
108         {"rx_ctl_pkts", QL_SIZEOF(nic_stats.rx_ctl_pkts),
109                                         QL_OFF(nic_stats.rx_ctl_pkts)},
110         {"rx_pause_pkts", QL_SIZEOF(nic_stats.rx_pause_pkts),
111                                         QL_OFF(nic_stats.rx_pause_pkts)},
112         {"rx_64_pkts", QL_SIZEOF(nic_stats.rx_64_pkts),
113                                         QL_OFF(nic_stats.rx_64_pkts)},
114         {"rx_65_to_127_pkts", QL_SIZEOF(nic_stats.rx_65_to_127_pkts),
115                                         QL_OFF(nic_stats.rx_65_to_127_pkts)},
116         {"rx_128_255_pkts", QL_SIZEOF(nic_stats.rx_128_255_pkts),
117                                         QL_OFF(nic_stats.rx_128_255_pkts)},
118         {"rx_256_511_pkts", QL_SIZEOF(nic_stats.rx_256_511_pkts),
119                                         QL_OFF(nic_stats.rx_256_511_pkts)},
120         {"rx_512_to_1023_pkts", QL_SIZEOF(nic_stats.rx_512_to_1023_pkts),
121                                         QL_OFF(nic_stats.rx_512_to_1023_pkts)},
122         {"rx_1024_to_1518_pkts", QL_SIZEOF(nic_stats.rx_1024_to_1518_pkts),
123                                         QL_OFF(nic_stats.rx_1024_to_1518_pkts)},
124         {"rx_1519_to_max_pkts", QL_SIZEOF(nic_stats.rx_1519_to_max_pkts),
125                                         QL_OFF(nic_stats.rx_1519_to_max_pkts)},
126         {"rx_len_err_pkts", QL_SIZEOF(nic_stats.rx_len_err_pkts),
127                                         QL_OFF(nic_stats.rx_len_err_pkts)},
128         {"rx_code_err", QL_SIZEOF(nic_stats.rx_code_err),
129                                         QL_OFF(nic_stats.rx_code_err)},
130         {"rx_oversize_err", QL_SIZEOF(nic_stats.rx_oversize_err),
131                                         QL_OFF(nic_stats.rx_oversize_err)},
132         {"rx_undersize_err", QL_SIZEOF(nic_stats.rx_undersize_err),
133                                         QL_OFF(nic_stats.rx_undersize_err)},
134         {"rx_preamble_err", QL_SIZEOF(nic_stats.rx_preamble_err),
135                                         QL_OFF(nic_stats.rx_preamble_err)},
136         {"rx_frame_len_err", QL_SIZEOF(nic_stats.rx_frame_len_err),
137                                         QL_OFF(nic_stats.rx_frame_len_err)},
138         {"rx_crc_err", QL_SIZEOF(nic_stats.rx_crc_err),
139                                         QL_OFF(nic_stats.rx_crc_err)},
140         {"rx_err_count", QL_SIZEOF(nic_stats.rx_err_count),
141                                         QL_OFF(nic_stats.rx_err_count)},
142         {"tx_cbfc_pause_frames0", QL_SIZEOF(nic_stats.tx_cbfc_pause_frames0),
143                                 QL_OFF(nic_stats.tx_cbfc_pause_frames0)},
144         {"tx_cbfc_pause_frames1", QL_SIZEOF(nic_stats.tx_cbfc_pause_frames1),
145                                 QL_OFF(nic_stats.tx_cbfc_pause_frames1)},
146         {"tx_cbfc_pause_frames2", QL_SIZEOF(nic_stats.tx_cbfc_pause_frames2),
147                                 QL_OFF(nic_stats.tx_cbfc_pause_frames2)},
148         {"tx_cbfc_pause_frames3", QL_SIZEOF(nic_stats.tx_cbfc_pause_frames3),
149                                 QL_OFF(nic_stats.tx_cbfc_pause_frames3)},
150         {"tx_cbfc_pause_frames4", QL_SIZEOF(nic_stats.tx_cbfc_pause_frames4),
151                                 QL_OFF(nic_stats.tx_cbfc_pause_frames4)},
152         {"tx_cbfc_pause_frames5", QL_SIZEOF(nic_stats.tx_cbfc_pause_frames5),
153                                 QL_OFF(nic_stats.tx_cbfc_pause_frames5)},
154         {"tx_cbfc_pause_frames6", QL_SIZEOF(nic_stats.tx_cbfc_pause_frames6),
155                                 QL_OFF(nic_stats.tx_cbfc_pause_frames6)},
156         {"tx_cbfc_pause_frames7", QL_SIZEOF(nic_stats.tx_cbfc_pause_frames7),
157                                 QL_OFF(nic_stats.tx_cbfc_pause_frames7)},
158         {"rx_cbfc_pause_frames0", QL_SIZEOF(nic_stats.rx_cbfc_pause_frames0),
159                                 QL_OFF(nic_stats.rx_cbfc_pause_frames0)},
160         {"rx_cbfc_pause_frames1", QL_SIZEOF(nic_stats.rx_cbfc_pause_frames1),
161                                 QL_OFF(nic_stats.rx_cbfc_pause_frames1)},
162         {"rx_cbfc_pause_frames2", QL_SIZEOF(nic_stats.rx_cbfc_pause_frames2),
163                                 QL_OFF(nic_stats.rx_cbfc_pause_frames2)},
164         {"rx_cbfc_pause_frames3", QL_SIZEOF(nic_stats.rx_cbfc_pause_frames3),
165                                 QL_OFF(nic_stats.rx_cbfc_pause_frames3)},
166         {"rx_cbfc_pause_frames4", QL_SIZEOF(nic_stats.rx_cbfc_pause_frames4),
167                                 QL_OFF(nic_stats.rx_cbfc_pause_frames4)},
168         {"rx_cbfc_pause_frames5", QL_SIZEOF(nic_stats.rx_cbfc_pause_frames5),
169                                 QL_OFF(nic_stats.rx_cbfc_pause_frames5)},
170         {"rx_cbfc_pause_frames6", QL_SIZEOF(nic_stats.rx_cbfc_pause_frames6),
171                                 QL_OFF(nic_stats.rx_cbfc_pause_frames6)},
172         {"rx_cbfc_pause_frames7", QL_SIZEOF(nic_stats.rx_cbfc_pause_frames7),
173                                 QL_OFF(nic_stats.rx_cbfc_pause_frames7)},
174         {"rx_nic_fifo_drop", QL_SIZEOF(nic_stats.rx_nic_fifo_drop),
175                                         QL_OFF(nic_stats.rx_nic_fifo_drop)},
176 };
177
178 static const char ql_gstrings_test[][ETH_GSTRING_LEN] = {
179         "Loopback test  (offline)"
180 };
181 #define QLGE_TEST_LEN (sizeof(ql_gstrings_test) / ETH_GSTRING_LEN)
182 #define QLGE_STATS_LEN ARRAY_SIZE(ql_gstrings_stats)
183 #define QLGE_RCV_MAC_ERR_STATS  7
184
185 static int ql_update_ring_coalescing(struct ql_adapter *qdev)
186 {
187         int i, status = 0;
188         struct rx_ring *rx_ring;
189         struct cqicb *cqicb;
190
191         if (!netif_running(qdev->ndev))
192                 return status;
193
194         /* Skip the default queue, and update the outbound handler
195          * queues if they changed.
196          */
197         cqicb = (struct cqicb *)&qdev->rx_ring[qdev->rss_ring_count];
198         if (le16_to_cpu(cqicb->irq_delay) != qdev->tx_coalesce_usecs ||
199                 le16_to_cpu(cqicb->pkt_delay) !=
200                                 qdev->tx_max_coalesced_frames) {
201                 for (i = qdev->rss_ring_count; i < qdev->rx_ring_count; i++) {
202                         rx_ring = &qdev->rx_ring[i];
203                         cqicb = (struct cqicb *)rx_ring;
204                         cqicb->irq_delay = cpu_to_le16(qdev->tx_coalesce_usecs);
205                         cqicb->pkt_delay =
206                             cpu_to_le16(qdev->tx_max_coalesced_frames);
207                         cqicb->flags = FLAGS_LI;
208                         status = ql_write_cfg(qdev, cqicb, sizeof(*cqicb),
209                                                 CFG_LCQ, rx_ring->cq_id);
210                         if (status) {
211                                 netif_err(qdev, ifup, qdev->ndev,
212                                           "Failed to load CQICB.\n");
213                                 goto exit;
214                         }
215                 }
216         }
217
218         /* Update the inbound (RSS) handler queues if they changed. */
219         cqicb = (struct cqicb *)&qdev->rx_ring[0];
220         if (le16_to_cpu(cqicb->irq_delay) != qdev->rx_coalesce_usecs ||
221                 le16_to_cpu(cqicb->pkt_delay) !=
222                                         qdev->rx_max_coalesced_frames) {
223                 for (i = 0; i < qdev->rss_ring_count; i++, rx_ring++) {
224                         rx_ring = &qdev->rx_ring[i];
225                         cqicb = (struct cqicb *)rx_ring;
226                         cqicb->irq_delay = cpu_to_le16(qdev->rx_coalesce_usecs);
227                         cqicb->pkt_delay =
228                             cpu_to_le16(qdev->rx_max_coalesced_frames);
229                         cqicb->flags = FLAGS_LI;
230                         status = ql_write_cfg(qdev, cqicb, sizeof(*cqicb),
231                                                 CFG_LCQ, rx_ring->cq_id);
232                         if (status) {
233                                 netif_err(qdev, ifup, qdev->ndev,
234                                           "Failed to load CQICB.\n");
235                                 goto exit;
236                         }
237                 }
238         }
239 exit:
240         return status;
241 }
242
243 static void ql_update_stats(struct ql_adapter *qdev)
244 {
245         u32 i;
246         u64 data;
247         u64 *iter = &qdev->nic_stats.tx_pkts;
248
249         spin_lock(&qdev->stats_lock);
250         if (ql_sem_spinlock(qdev, qdev->xg_sem_mask)) {
251                         netif_err(qdev, drv, qdev->ndev,
252                                   "Couldn't get xgmac sem.\n");
253                 goto quit;
254         }
255         /*
256          * Get TX statistics.
257          */
258         for (i = 0x200; i < 0x280; i += 8) {
259                 if (ql_read_xgmac_reg64(qdev, i, &data)) {
260                         netif_err(qdev, drv, qdev->ndev,
261                                   "Error reading status register 0x%.04x.\n",
262                                   i);
263                         goto end;
264                 } else
265                         *iter = data;
266                 iter++;
267         }
268
269         /*
270          * Get RX statistics.
271          */
272         for (i = 0x300; i < 0x3d0; i += 8) {
273                 if (ql_read_xgmac_reg64(qdev, i, &data)) {
274                         netif_err(qdev, drv, qdev->ndev,
275                                   "Error reading status register 0x%.04x.\n",
276                                   i);
277                         goto end;
278                 } else
279                         *iter = data;
280                 iter++;
281         }
282
283         /* Update receive mac error statistics */
284         iter += QLGE_RCV_MAC_ERR_STATS;
285
286         /*
287          * Get Per-priority TX pause frame counter statistics.
288          */
289         for (i = 0x500; i < 0x540; i += 8) {
290                 if (ql_read_xgmac_reg64(qdev, i, &data)) {
291                         netif_err(qdev, drv, qdev->ndev,
292                                   "Error reading status register 0x%.04x.\n",
293                                   i);
294                         goto end;
295                 } else
296                         *iter = data;
297                 iter++;
298         }
299
300         /*
301          * Get Per-priority RX pause frame counter statistics.
302          */
303         for (i = 0x568; i < 0x5a8; i += 8) {
304                 if (ql_read_xgmac_reg64(qdev, i, &data)) {
305                         netif_err(qdev, drv, qdev->ndev,
306                                   "Error reading status register 0x%.04x.\n",
307                                   i);
308                         goto end;
309                 } else
310                         *iter = data;
311                 iter++;
312         }
313
314         /*
315          * Get RX NIC FIFO DROP statistics.
316          */
317         if (ql_read_xgmac_reg64(qdev, 0x5b8, &data)) {
318                 netif_err(qdev, drv, qdev->ndev,
319                           "Error reading status register 0x%.04x.\n", i);
320                 goto end;
321         } else
322                 *iter = data;
323 end:
324         ql_sem_unlock(qdev, qdev->xg_sem_mask);
325 quit:
326         spin_unlock(&qdev->stats_lock);
327
328         QL_DUMP_STAT(qdev);
329 }
330
331 static void ql_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
332 {
333         int index;
334         switch (stringset) {
335         case ETH_SS_TEST:
336                 memcpy(buf, *ql_gstrings_test, QLGE_TEST_LEN * ETH_GSTRING_LEN);
337                 break;
338         case ETH_SS_STATS:
339                 for (index = 0; index < QLGE_STATS_LEN; index++) {
340                         memcpy(buf + index * ETH_GSTRING_LEN,
341                                 ql_gstrings_stats[index].stat_string,
342                                 ETH_GSTRING_LEN);
343                 }
344                 break;
345         }
346 }
347
348 static int ql_get_sset_count(struct net_device *dev, int sset)
349 {
350         switch (sset) {
351         case ETH_SS_TEST:
352                 return QLGE_TEST_LEN;
353         case ETH_SS_STATS:
354                 return QLGE_STATS_LEN;
355         default:
356                 return -EOPNOTSUPP;
357         }
358 }
359
360 static void
361 ql_get_ethtool_stats(struct net_device *ndev,
362                      struct ethtool_stats *stats, u64 *data)
363 {
364         struct ql_adapter *qdev = netdev_priv(ndev);
365         int index, length;
366
367         length = QLGE_STATS_LEN;
368         ql_update_stats(qdev);
369
370         for (index = 0; index < length; index++) {
371                 char *p = (char *)qdev +
372                         ql_gstrings_stats[index].stat_offset;
373                 *data++ = (ql_gstrings_stats[index].sizeof_stat ==
374                         sizeof(u64)) ? *(u64 *)p : (*(u32 *)p);
375         }
376 }
377
378 static int ql_get_link_ksettings(struct net_device *ndev,
379                                  struct ethtool_link_ksettings *ecmd)
380 {
381         struct ql_adapter *qdev = netdev_priv(ndev);
382         u32 supported, advertising;
383
384         supported = SUPPORTED_10000baseT_Full;
385         advertising = ADVERTISED_10000baseT_Full;
386
387         if ((qdev->link_status & STS_LINK_TYPE_MASK) ==
388                                 STS_LINK_TYPE_10GBASET) {
389                 supported |= (SUPPORTED_TP | SUPPORTED_Autoneg);
390                 advertising |= (ADVERTISED_TP | ADVERTISED_Autoneg);
391                 ecmd->base.port = PORT_TP;
392                 ecmd->base.autoneg = AUTONEG_ENABLE;
393         } else {
394                 supported |= SUPPORTED_FIBRE;
395                 advertising |= ADVERTISED_FIBRE;
396                 ecmd->base.port = PORT_FIBRE;
397         }
398
399         ecmd->base.speed = SPEED_10000;
400         ecmd->base.duplex = DUPLEX_FULL;
401
402         ethtool_convert_legacy_u32_to_link_mode(ecmd->link_modes.supported,
403                                                 supported);
404         ethtool_convert_legacy_u32_to_link_mode(ecmd->link_modes.advertising,
405                                                 advertising);
406
407         return 0;
408 }
409
410 static void ql_get_drvinfo(struct net_device *ndev,
411                            struct ethtool_drvinfo *drvinfo)
412 {
413         struct ql_adapter *qdev = netdev_priv(ndev);
414         strlcpy(drvinfo->driver, qlge_driver_name, sizeof(drvinfo->driver));
415         strlcpy(drvinfo->version, qlge_driver_version,
416                 sizeof(drvinfo->version));
417         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
418                  "v%d.%d.%d",
419                  (qdev->fw_rev_id & 0x00ff0000) >> 16,
420                  (qdev->fw_rev_id & 0x0000ff00) >> 8,
421                  (qdev->fw_rev_id & 0x000000ff));
422         strlcpy(drvinfo->bus_info, pci_name(qdev->pdev),
423                 sizeof(drvinfo->bus_info));
424 }
425
426 static void ql_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
427 {
428         struct ql_adapter *qdev = netdev_priv(ndev);
429         unsigned short ssys_dev = qdev->pdev->subsystem_device;
430
431         /* WOL is only supported for mezz card. */
432         if (ssys_dev == QLGE_MEZZ_SSYS_ID_068 ||
433                         ssys_dev == QLGE_MEZZ_SSYS_ID_180) {
434                 wol->supported = WAKE_MAGIC;
435                 wol->wolopts = qdev->wol;
436         }
437 }
438
439 static int ql_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
440 {
441         struct ql_adapter *qdev = netdev_priv(ndev);
442         unsigned short ssys_dev = qdev->pdev->subsystem_device;
443
444         /* WOL is only supported for mezz card. */
445         if (ssys_dev != QLGE_MEZZ_SSYS_ID_068 &&
446                         ssys_dev != QLGE_MEZZ_SSYS_ID_180) {
447                 netif_info(qdev, drv, qdev->ndev,
448                                 "WOL is only supported for mezz card\n");
449                 return -EOPNOTSUPP;
450         }
451         if (wol->wolopts & ~WAKE_MAGIC)
452                 return -EINVAL;
453         qdev->wol = wol->wolopts;
454
455         netif_info(qdev, drv, qdev->ndev, "Set wol option 0x%x\n", qdev->wol);
456         return 0;
457 }
458
459 static int ql_set_phys_id(struct net_device *ndev,
460                           enum ethtool_phys_id_state state)
461
462 {
463         struct ql_adapter *qdev = netdev_priv(ndev);
464
465         switch (state) {
466         case ETHTOOL_ID_ACTIVE:
467                 /* Save the current LED settings */
468                 if (ql_mb_get_led_cfg(qdev))
469                         return -EIO;
470
471                 /* Start blinking */
472                 ql_mb_set_led_cfg(qdev, QL_LED_BLINK);
473                 return 0;
474
475         case ETHTOOL_ID_INACTIVE:
476                 /* Restore LED settings */
477                 if (ql_mb_set_led_cfg(qdev, qdev->led_config))
478                         return -EIO;
479                 return 0;
480
481         default:
482                 return -EINVAL;
483         }
484 }
485
486 static int ql_start_loopback(struct ql_adapter *qdev)
487 {
488         if (netif_carrier_ok(qdev->ndev)) {
489                 set_bit(QL_LB_LINK_UP, &qdev->flags);
490                 netif_carrier_off(qdev->ndev);
491         } else
492                 clear_bit(QL_LB_LINK_UP, &qdev->flags);
493         qdev->link_config |= CFG_LOOPBACK_PCS;
494         return ql_mb_set_port_cfg(qdev);
495 }
496
497 static void ql_stop_loopback(struct ql_adapter *qdev)
498 {
499         qdev->link_config &= ~CFG_LOOPBACK_PCS;
500         ql_mb_set_port_cfg(qdev);
501         if (test_bit(QL_LB_LINK_UP, &qdev->flags)) {
502                 netif_carrier_on(qdev->ndev);
503                 clear_bit(QL_LB_LINK_UP, &qdev->flags);
504         }
505 }
506
507 static void ql_create_lb_frame(struct sk_buff *skb,
508                                         unsigned int frame_size)
509 {
510         memset(skb->data, 0xFF, frame_size);
511         frame_size &= ~1;
512         memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
513         memset(&skb->data[frame_size / 2 + 10], 0xBE, 1);
514         memset(&skb->data[frame_size / 2 + 12], 0xAF, 1);
515 }
516
517 void ql_check_lb_frame(struct ql_adapter *qdev,
518                                         struct sk_buff *skb)
519 {
520         unsigned int frame_size = skb->len;
521
522         if ((*(skb->data + 3) == 0xFF) &&
523                 (*(skb->data + frame_size / 2 + 10) == 0xBE) &&
524                 (*(skb->data + frame_size / 2 + 12) == 0xAF)) {
525                         atomic_dec(&qdev->lb_count);
526                         return;
527         }
528 }
529
530 static int ql_run_loopback_test(struct ql_adapter *qdev)
531 {
532         int i;
533         netdev_tx_t rc;
534         struct sk_buff *skb;
535         unsigned int size = SMALL_BUF_MAP_SIZE;
536
537         for (i = 0; i < 64; i++) {
538                 skb = netdev_alloc_skb(qdev->ndev, size);
539                 if (!skb)
540                         return -ENOMEM;
541
542                 skb->queue_mapping = 0;
543                 skb_put(skb, size);
544                 ql_create_lb_frame(skb, size);
545                 rc = ql_lb_send(skb, qdev->ndev);
546                 if (rc != NETDEV_TX_OK)
547                         return -EPIPE;
548                 atomic_inc(&qdev->lb_count);
549         }
550         /* Give queue time to settle before testing results. */
551         msleep(2);
552         ql_clean_lb_rx_ring(&qdev->rx_ring[0], 128);
553         return atomic_read(&qdev->lb_count) ? -EIO : 0;
554 }
555
556 static int ql_loopback_test(struct ql_adapter *qdev, u64 *data)
557 {
558         *data = ql_start_loopback(qdev);
559         if (*data)
560                 goto out;
561         *data = ql_run_loopback_test(qdev);
562 out:
563         ql_stop_loopback(qdev);
564         return *data;
565 }
566
567 static void ql_self_test(struct net_device *ndev,
568                                 struct ethtool_test *eth_test, u64 *data)
569 {
570         struct ql_adapter *qdev = netdev_priv(ndev);
571
572         memset(data, 0, sizeof(u64) * QLGE_TEST_LEN);
573
574         if (netif_running(ndev)) {
575                 set_bit(QL_SELFTEST, &qdev->flags);
576                 if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
577                         /* Offline tests */
578                         if (ql_loopback_test(qdev, &data[0]))
579                                 eth_test->flags |= ETH_TEST_FL_FAILED;
580
581                 } else {
582                         /* Online tests */
583                         data[0] = 0;
584                 }
585                 clear_bit(QL_SELFTEST, &qdev->flags);
586                 /* Give link time to come up after
587                  * port configuration changes.
588                  */
589                 msleep_interruptible(4 * 1000);
590         } else {
591                 netif_err(qdev, drv, qdev->ndev,
592                           "is down, Loopback test will fail.\n");
593                 eth_test->flags |= ETH_TEST_FL_FAILED;
594         }
595 }
596
597 static int ql_get_regs_len(struct net_device *ndev)
598 {
599         struct ql_adapter *qdev = netdev_priv(ndev);
600
601         if (!test_bit(QL_FRC_COREDUMP, &qdev->flags))
602                 return sizeof(struct ql_mpi_coredump);
603         else
604                 return sizeof(struct ql_reg_dump);
605 }
606
607 static void ql_get_regs(struct net_device *ndev,
608                         struct ethtool_regs *regs, void *p)
609 {
610         struct ql_adapter *qdev = netdev_priv(ndev);
611
612         ql_get_dump(qdev, p);
613         qdev->core_is_dumped = 0;
614         if (!test_bit(QL_FRC_COREDUMP, &qdev->flags))
615                 regs->len = sizeof(struct ql_mpi_coredump);
616         else
617                 regs->len = sizeof(struct ql_reg_dump);
618 }
619
620 static int ql_get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
621 {
622         struct ql_adapter *qdev = netdev_priv(dev);
623
624         c->rx_coalesce_usecs = qdev->rx_coalesce_usecs;
625         c->tx_coalesce_usecs = qdev->tx_coalesce_usecs;
626
627         /* This chip coalesces as follows:
628          * If a packet arrives, hold off interrupts until
629          * cqicb->int_delay expires, but if no other packets arrive don't
630          * wait longer than cqicb->pkt_int_delay. But ethtool doesn't use a
631          * timer to coalesce on a frame basis.  So, we have to take ethtool's
632          * max_coalesced_frames value and convert it to a delay in microseconds.
633          * We do this by using a basic thoughput of 1,000,000 frames per
634          * second @ (1024 bytes).  This means one frame per usec. So it's a
635          * simple one to one ratio.
636          */
637         c->rx_max_coalesced_frames = qdev->rx_max_coalesced_frames;
638         c->tx_max_coalesced_frames = qdev->tx_max_coalesced_frames;
639
640         return 0;
641 }
642
643 static int ql_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *c)
644 {
645         struct ql_adapter *qdev = netdev_priv(ndev);
646
647         /* Validate user parameters. */
648         if (c->rx_coalesce_usecs > qdev->rx_ring_size / 2)
649                 return -EINVAL;
650        /* Don't wait more than 10 usec. */
651         if (c->rx_max_coalesced_frames > MAX_INTER_FRAME_WAIT)
652                 return -EINVAL;
653         if (c->tx_coalesce_usecs > qdev->tx_ring_size / 2)
654                 return -EINVAL;
655         if (c->tx_max_coalesced_frames > MAX_INTER_FRAME_WAIT)
656                 return -EINVAL;
657
658         /* Verify a change took place before updating the hardware. */
659         if (qdev->rx_coalesce_usecs == c->rx_coalesce_usecs &&
660             qdev->tx_coalesce_usecs == c->tx_coalesce_usecs &&
661             qdev->rx_max_coalesced_frames == c->rx_max_coalesced_frames &&
662             qdev->tx_max_coalesced_frames == c->tx_max_coalesced_frames)
663                 return 0;
664
665         qdev->rx_coalesce_usecs = c->rx_coalesce_usecs;
666         qdev->tx_coalesce_usecs = c->tx_coalesce_usecs;
667         qdev->rx_max_coalesced_frames = c->rx_max_coalesced_frames;
668         qdev->tx_max_coalesced_frames = c->tx_max_coalesced_frames;
669
670         return ql_update_ring_coalescing(qdev);
671 }
672
673 static void ql_get_pauseparam(struct net_device *netdev,
674                         struct ethtool_pauseparam *pause)
675 {
676         struct ql_adapter *qdev = netdev_priv(netdev);
677
678         ql_mb_get_port_cfg(qdev);
679         if (qdev->link_config & CFG_PAUSE_STD) {
680                 pause->rx_pause = 1;
681                 pause->tx_pause = 1;
682         }
683 }
684
685 static int ql_set_pauseparam(struct net_device *netdev,
686                         struct ethtool_pauseparam *pause)
687 {
688         struct ql_adapter *qdev = netdev_priv(netdev);
689         int status = 0;
690
691         if ((pause->rx_pause) && (pause->tx_pause))
692                 qdev->link_config |= CFG_PAUSE_STD;
693         else if (!pause->rx_pause && !pause->tx_pause)
694                 qdev->link_config &= ~CFG_PAUSE_STD;
695         else
696                 return -EINVAL;
697
698         status = ql_mb_set_port_cfg(qdev);
699         return status;
700 }
701
702 static u32 ql_get_msglevel(struct net_device *ndev)
703 {
704         struct ql_adapter *qdev = netdev_priv(ndev);
705         return qdev->msg_enable;
706 }
707
708 static void ql_set_msglevel(struct net_device *ndev, u32 value)
709 {
710         struct ql_adapter *qdev = netdev_priv(ndev);
711         qdev->msg_enable = value;
712 }
713
714 const struct ethtool_ops qlge_ethtool_ops = {
715         .get_drvinfo = ql_get_drvinfo,
716         .get_wol = ql_get_wol,
717         .set_wol = ql_set_wol,
718         .get_regs_len   = ql_get_regs_len,
719         .get_regs = ql_get_regs,
720         .get_msglevel = ql_get_msglevel,
721         .set_msglevel = ql_set_msglevel,
722         .get_link = ethtool_op_get_link,
723         .set_phys_id             = ql_set_phys_id,
724         .self_test               = ql_self_test,
725         .get_pauseparam          = ql_get_pauseparam,
726         .set_pauseparam          = ql_set_pauseparam,
727         .get_coalesce = ql_get_coalesce,
728         .set_coalesce = ql_set_coalesce,
729         .get_sset_count = ql_get_sset_count,
730         .get_strings = ql_get_strings,
731         .get_ethtool_stats = ql_get_ethtool_stats,
732         .get_link_ksettings = ql_get_link_ksettings,
733 };
734