1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.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/dmapool.h>
11 #include <linux/mempool.h>
12 #include <linux/spinlock.h>
13 #include <linux/kthread.h>
14 #include <linux/interrupt.h>
15 #include <linux/errno.h>
16 #include <linux/ioport.h>
17 #include <linux/in.h>
18 #include <linux/ip.h>
19 #include <linux/ipv6.h>
20 #include <net/ipv6.h>
21 #include <linux/tcp.h>
22 #include <linux/udp.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_ether.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/ethtool.h>
28 #include <linux/skbuff.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/if_vlan.h>
31 #include <linux/delay.h>
32 #include <linux/mm.h>
33 #include <linux/vmalloc.h>
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) sizeof_field(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 
182 #define QLGE_TEST_LEN (sizeof(ql_gstrings_test) / ETH_GSTRING_LEN)
183 #define QLGE_STATS_LEN ARRAY_SIZE(ql_gstrings_stats)
184 #define QLGE_RCV_MAC_ERR_STATS	7
185 
186 static int ql_update_ring_coalescing(struct ql_adapter *qdev)
187 {
188 	int i, status = 0;
189 	struct rx_ring *rx_ring;
190 	struct cqicb *cqicb;
191 
192 	if (!netif_running(qdev->ndev))
193 		return status;
194 
195 	/* Skip the default queue, and update the outbound handler
196 	 * queues if they changed.
197 	 */
198 	cqicb = (struct cqicb *)&qdev->rx_ring[qdev->rss_ring_count];
199 	if (le16_to_cpu(cqicb->irq_delay) != qdev->tx_coalesce_usecs ||
200 	    le16_to_cpu(cqicb->pkt_delay) != 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) != qdev->rx_max_coalesced_frames) {
222 		for (i = 0; i < qdev->rss_ring_count; i++, rx_ring++) {
223 			rx_ring = &qdev->rx_ring[i];
224 			cqicb = (struct cqicb *)rx_ring;
225 			cqicb->irq_delay = cpu_to_le16(qdev->rx_coalesce_usecs);
226 			cqicb->pkt_delay =
227 			    cpu_to_le16(qdev->rx_max_coalesced_frames);
228 			cqicb->flags = FLAGS_LI;
229 			status = ql_write_cfg(qdev, cqicb, sizeof(*cqicb),
230 					      CFG_LCQ, rx_ring->cq_id);
231 			if (status) {
232 				netif_err(qdev, ifup, qdev->ndev,
233 					  "Failed to load CQICB.\n");
234 				goto exit;
235 			}
236 		}
237 	}
238 exit:
239 	return status;
240 }
241 
242 static void ql_update_stats(struct ql_adapter *qdev)
243 {
244 	u32 i;
245 	u64 data;
246 	u64 *iter = &qdev->nic_stats.tx_pkts;
247 
248 	spin_lock(&qdev->stats_lock);
249 	if (ql_sem_spinlock(qdev, qdev->xg_sem_mask)) {
250 		netif_err(qdev, drv, qdev->ndev,
251 			  "Couldn't get xgmac sem.\n");
252 		goto quit;
253 	}
254 	/*
255 	 * Get TX statistics.
256 	 */
257 	for (i = 0x200; i < 0x280; i += 8) {
258 		if (ql_read_xgmac_reg64(qdev, i, &data)) {
259 			netif_err(qdev, drv, qdev->ndev,
260 				  "Error reading status register 0x%.04x.\n",
261 				  i);
262 			goto end;
263 		} else {
264 			*iter = data;
265 		}
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 		}
281 		iter++;
282 	}
283 
284 	/* Update receive mac error statistics */
285 	iter += QLGE_RCV_MAC_ERR_STATS;
286 
287 	/*
288 	 * Get Per-priority TX pause frame counter statistics.
289 	 */
290 	for (i = 0x500; i < 0x540; i += 8) {
291 		if (ql_read_xgmac_reg64(qdev, i, &data)) {
292 			netif_err(qdev, drv, qdev->ndev,
293 				  "Error reading status register 0x%.04x.\n",
294 				  i);
295 			goto end;
296 		} else {
297 			*iter = data;
298 		}
299 		iter++;
300 	}
301 
302 	/*
303 	 * Get Per-priority RX pause frame counter statistics.
304 	 */
305 	for (i = 0x568; i < 0x5a8; i += 8) {
306 		if (ql_read_xgmac_reg64(qdev, i, &data)) {
307 			netif_err(qdev, drv, qdev->ndev,
308 				  "Error reading status register 0x%.04x.\n",
309 				  i);
310 			goto end;
311 		} else {
312 			*iter = data;
313 		}
314 		iter++;
315 	}
316 
317 	/*
318 	 * Get RX NIC FIFO DROP statistics.
319 	 */
320 	if (ql_read_xgmac_reg64(qdev, 0x5b8, &data)) {
321 		netif_err(qdev, drv, qdev->ndev,
322 			  "Error reading status register 0x%.04x.\n", i);
323 		goto end;
324 	} else {
325 		*iter = data;
326 	}
327 end:
328 	ql_sem_unlock(qdev, qdev->xg_sem_mask);
329 quit:
330 	spin_unlock(&qdev->stats_lock);
331 
332 	QL_DUMP_STAT(qdev);
333 }
334 
335 static void ql_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
336 {
337 	int index;
338 
339 	switch (stringset) {
340 	case ETH_SS_TEST:
341 		memcpy(buf, *ql_gstrings_test, QLGE_TEST_LEN * ETH_GSTRING_LEN);
342 		break;
343 	case ETH_SS_STATS:
344 		for (index = 0; index < QLGE_STATS_LEN; index++) {
345 			memcpy(buf + index * ETH_GSTRING_LEN,
346 			       ql_gstrings_stats[index].stat_string,
347 			       ETH_GSTRING_LEN);
348 		}
349 		break;
350 	}
351 }
352 
353 static int ql_get_sset_count(struct net_device *dev, int sset)
354 {
355 	switch (sset) {
356 	case ETH_SS_TEST:
357 		return QLGE_TEST_LEN;
358 	case ETH_SS_STATS:
359 		return QLGE_STATS_LEN;
360 	default:
361 		return -EOPNOTSUPP;
362 	}
363 }
364 
365 static void
366 ql_get_ethtool_stats(struct net_device *ndev,
367 		     struct ethtool_stats *stats, u64 *data)
368 {
369 	struct ql_adapter *qdev = netdev_priv(ndev);
370 	int index, length;
371 
372 	length = QLGE_STATS_LEN;
373 	ql_update_stats(qdev);
374 
375 	for (index = 0; index < length; index++) {
376 		char *p = (char *)qdev +
377 			ql_gstrings_stats[index].stat_offset;
378 		*data++ = (ql_gstrings_stats[index].sizeof_stat ==
379 			sizeof(u64)) ? *(u64 *)p : (*(u32 *)p);
380 	}
381 }
382 
383 static int ql_get_link_ksettings(struct net_device *ndev,
384 				 struct ethtool_link_ksettings *ecmd)
385 {
386 	struct ql_adapter *qdev = netdev_priv(ndev);
387 	u32 supported, advertising;
388 
389 	supported = SUPPORTED_10000baseT_Full;
390 	advertising = ADVERTISED_10000baseT_Full;
391 
392 	if ((qdev->link_status & STS_LINK_TYPE_MASK) ==
393 				STS_LINK_TYPE_10GBASET) {
394 		supported |= (SUPPORTED_TP | SUPPORTED_Autoneg);
395 		advertising |= (ADVERTISED_TP | ADVERTISED_Autoneg);
396 		ecmd->base.port = PORT_TP;
397 		ecmd->base.autoneg = AUTONEG_ENABLE;
398 	} else {
399 		supported |= SUPPORTED_FIBRE;
400 		advertising |= ADVERTISED_FIBRE;
401 		ecmd->base.port = PORT_FIBRE;
402 	}
403 
404 	ecmd->base.speed = SPEED_10000;
405 	ecmd->base.duplex = DUPLEX_FULL;
406 
407 	ethtool_convert_legacy_u32_to_link_mode(ecmd->link_modes.supported,
408 						supported);
409 	ethtool_convert_legacy_u32_to_link_mode(ecmd->link_modes.advertising,
410 						advertising);
411 
412 	return 0;
413 }
414 
415 static void ql_get_drvinfo(struct net_device *ndev,
416 			   struct ethtool_drvinfo *drvinfo)
417 {
418 	struct ql_adapter *qdev = netdev_priv(ndev);
419 
420 	strlcpy(drvinfo->driver, qlge_driver_name, sizeof(drvinfo->driver));
421 	strlcpy(drvinfo->version, qlge_driver_version,
422 		sizeof(drvinfo->version));
423 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
424 		 "v%d.%d.%d",
425 		 (qdev->fw_rev_id & 0x00ff0000) >> 16,
426 		 (qdev->fw_rev_id & 0x0000ff00) >> 8,
427 		 (qdev->fw_rev_id & 0x000000ff));
428 	strlcpy(drvinfo->bus_info, pci_name(qdev->pdev),
429 		sizeof(drvinfo->bus_info));
430 }
431 
432 static void ql_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
433 {
434 	struct ql_adapter *qdev = netdev_priv(ndev);
435 	unsigned short ssys_dev = qdev->pdev->subsystem_device;
436 
437 	/* WOL is only supported for mezz card. */
438 	if (ssys_dev == QLGE_MEZZ_SSYS_ID_068 ||
439 	    ssys_dev == QLGE_MEZZ_SSYS_ID_180) {
440 		wol->supported = WAKE_MAGIC;
441 		wol->wolopts = qdev->wol;
442 	}
443 }
444 
445 static int ql_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
446 {
447 	struct ql_adapter *qdev = netdev_priv(ndev);
448 	unsigned short ssys_dev = qdev->pdev->subsystem_device;
449 
450 	/* WOL is only supported for mezz card. */
451 	if (ssys_dev != QLGE_MEZZ_SSYS_ID_068 &&
452 	    ssys_dev != QLGE_MEZZ_SSYS_ID_180) {
453 		netif_info(qdev, drv, qdev->ndev,
454 			   "WOL is only supported for mezz card\n");
455 		return -EOPNOTSUPP;
456 	}
457 	if (wol->wolopts & ~WAKE_MAGIC)
458 		return -EINVAL;
459 	qdev->wol = wol->wolopts;
460 
461 	netif_info(qdev, drv, qdev->ndev, "Set wol option 0x%x\n", qdev->wol);
462 	return 0;
463 }
464 
465 static int ql_set_phys_id(struct net_device *ndev,
466 			  enum ethtool_phys_id_state state)
467 
468 {
469 	struct ql_adapter *qdev = netdev_priv(ndev);
470 
471 	switch (state) {
472 	case ETHTOOL_ID_ACTIVE:
473 		/* Save the current LED settings */
474 		if (ql_mb_get_led_cfg(qdev))
475 			return -EIO;
476 
477 		/* Start blinking */
478 		ql_mb_set_led_cfg(qdev, QL_LED_BLINK);
479 		return 0;
480 
481 	case ETHTOOL_ID_INACTIVE:
482 		/* Restore LED settings */
483 		if (ql_mb_set_led_cfg(qdev, qdev->led_config))
484 			return -EIO;
485 		return 0;
486 
487 	default:
488 		return -EINVAL;
489 	}
490 }
491 
492 static int ql_start_loopback(struct ql_adapter *qdev)
493 {
494 	if (netif_carrier_ok(qdev->ndev)) {
495 		set_bit(QL_LB_LINK_UP, &qdev->flags);
496 		netif_carrier_off(qdev->ndev);
497 	} else {
498 		clear_bit(QL_LB_LINK_UP, &qdev->flags);
499 	}
500 	qdev->link_config |= CFG_LOOPBACK_PCS;
501 	return ql_mb_set_port_cfg(qdev);
502 }
503 
504 static void ql_stop_loopback(struct ql_adapter *qdev)
505 {
506 	qdev->link_config &= ~CFG_LOOPBACK_PCS;
507 	ql_mb_set_port_cfg(qdev);
508 	if (test_bit(QL_LB_LINK_UP, &qdev->flags)) {
509 		netif_carrier_on(qdev->ndev);
510 		clear_bit(QL_LB_LINK_UP, &qdev->flags);
511 	}
512 }
513 
514 static void ql_create_lb_frame(struct sk_buff *skb,
515 			       unsigned int frame_size)
516 {
517 	memset(skb->data, 0xFF, frame_size);
518 	frame_size &= ~1;
519 	memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
520 	skb->data[frame_size / 2 + 10] = (unsigned char)0xBE;
521 	skb->data[frame_size / 2 + 12] = (unsigned char)0xAF;
522 }
523 
524 void ql_check_lb_frame(struct ql_adapter *qdev,
525 		       struct sk_buff *skb)
526 {
527 	unsigned int frame_size = skb->len;
528 
529 	if ((*(skb->data + 3) == 0xFF) &&
530 	    (*(skb->data + frame_size / 2 + 10) == 0xBE) &&
531 	    (*(skb->data + frame_size / 2 + 12) == 0xAF)) {
532 		atomic_dec(&qdev->lb_count);
533 		return;
534 	}
535 }
536 
537 static int ql_run_loopback_test(struct ql_adapter *qdev)
538 {
539 	int i;
540 	netdev_tx_t rc;
541 	struct sk_buff *skb;
542 	unsigned int size = SMALL_BUF_MAP_SIZE;
543 
544 	for (i = 0; i < 64; i++) {
545 		skb = netdev_alloc_skb(qdev->ndev, size);
546 		if (!skb)
547 			return -ENOMEM;
548 
549 		skb->queue_mapping = 0;
550 		skb_put(skb, size);
551 		ql_create_lb_frame(skb, size);
552 		rc = ql_lb_send(skb, qdev->ndev);
553 		if (rc != NETDEV_TX_OK)
554 			return -EPIPE;
555 		atomic_inc(&qdev->lb_count);
556 	}
557 	/* Give queue time to settle before testing results. */
558 	msleep(2);
559 	ql_clean_lb_rx_ring(&qdev->rx_ring[0], 128);
560 	return atomic_read(&qdev->lb_count) ? -EIO : 0;
561 }
562 
563 static int ql_loopback_test(struct ql_adapter *qdev, u64 *data)
564 {
565 	*data = ql_start_loopback(qdev);
566 	if (*data)
567 		goto out;
568 	*data = ql_run_loopback_test(qdev);
569 out:
570 	ql_stop_loopback(qdev);
571 	return *data;
572 }
573 
574 static void ql_self_test(struct net_device *ndev,
575 			 struct ethtool_test *eth_test, u64 *data)
576 {
577 	struct ql_adapter *qdev = netdev_priv(ndev);
578 
579 	memset(data, 0, sizeof(u64) * QLGE_TEST_LEN);
580 
581 	if (netif_running(ndev)) {
582 		set_bit(QL_SELFTEST, &qdev->flags);
583 		if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
584 			/* Offline tests */
585 			if (ql_loopback_test(qdev, &data[0]))
586 				eth_test->flags |= ETH_TEST_FL_FAILED;
587 
588 		} else {
589 			/* Online tests */
590 			data[0] = 0;
591 		}
592 		clear_bit(QL_SELFTEST, &qdev->flags);
593 		/* Give link time to come up after
594 		 * port configuration changes.
595 		 */
596 		msleep_interruptible(4 * 1000);
597 	} else {
598 		netif_err(qdev, drv, qdev->ndev,
599 			  "is down, Loopback test will fail.\n");
600 		eth_test->flags |= ETH_TEST_FL_FAILED;
601 	}
602 }
603 
604 static int ql_get_regs_len(struct net_device *ndev)
605 {
606 	struct ql_adapter *qdev = netdev_priv(ndev);
607 
608 	if (!test_bit(QL_FRC_COREDUMP, &qdev->flags))
609 		return sizeof(struct ql_mpi_coredump);
610 	else
611 		return sizeof(struct ql_reg_dump);
612 }
613 
614 static void ql_get_regs(struct net_device *ndev,
615 			struct ethtool_regs *regs, void *p)
616 {
617 	struct ql_adapter *qdev = netdev_priv(ndev);
618 
619 	ql_get_dump(qdev, p);
620 	qdev->core_is_dumped = 0;
621 	if (!test_bit(QL_FRC_COREDUMP, &qdev->flags))
622 		regs->len = sizeof(struct ql_mpi_coredump);
623 	else
624 		regs->len = sizeof(struct ql_reg_dump);
625 }
626 
627 static int ql_get_coalesce(struct net_device *dev, struct ethtool_coalesce *c)
628 {
629 	struct ql_adapter *qdev = netdev_priv(dev);
630 
631 	c->rx_coalesce_usecs = qdev->rx_coalesce_usecs;
632 	c->tx_coalesce_usecs = qdev->tx_coalesce_usecs;
633 
634 	/* This chip coalesces as follows:
635 	 * If a packet arrives, hold off interrupts until
636 	 * cqicb->int_delay expires, but if no other packets arrive don't
637 	 * wait longer than cqicb->pkt_int_delay. But ethtool doesn't use a
638 	 * timer to coalesce on a frame basis.  So, we have to take ethtool's
639 	 * max_coalesced_frames value and convert it to a delay in microseconds.
640 	 * We do this by using a basic thoughput of 1,000,000 frames per
641 	 * second @ (1024 bytes).  This means one frame per usec. So it's a
642 	 * simple one to one ratio.
643 	 */
644 	c->rx_max_coalesced_frames = qdev->rx_max_coalesced_frames;
645 	c->tx_max_coalesced_frames = qdev->tx_max_coalesced_frames;
646 
647 	return 0;
648 }
649 
650 static int ql_set_coalesce(struct net_device *ndev, struct ethtool_coalesce *c)
651 {
652 	struct ql_adapter *qdev = netdev_priv(ndev);
653 
654 	/* Validate user parameters. */
655 	if (c->rx_coalesce_usecs > qdev->rx_ring_size / 2)
656 		return -EINVAL;
657        /* Don't wait more than 10 usec. */
658 	if (c->rx_max_coalesced_frames > MAX_INTER_FRAME_WAIT)
659 		return -EINVAL;
660 	if (c->tx_coalesce_usecs > qdev->tx_ring_size / 2)
661 		return -EINVAL;
662 	if (c->tx_max_coalesced_frames > MAX_INTER_FRAME_WAIT)
663 		return -EINVAL;
664 
665 	/* Verify a change took place before updating the hardware. */
666 	if (qdev->rx_coalesce_usecs == c->rx_coalesce_usecs &&
667 	    qdev->tx_coalesce_usecs == c->tx_coalesce_usecs &&
668 	    qdev->rx_max_coalesced_frames == c->rx_max_coalesced_frames &&
669 	    qdev->tx_max_coalesced_frames == c->tx_max_coalesced_frames)
670 		return 0;
671 
672 	qdev->rx_coalesce_usecs = c->rx_coalesce_usecs;
673 	qdev->tx_coalesce_usecs = c->tx_coalesce_usecs;
674 	qdev->rx_max_coalesced_frames = c->rx_max_coalesced_frames;
675 	qdev->tx_max_coalesced_frames = c->tx_max_coalesced_frames;
676 
677 	return ql_update_ring_coalescing(qdev);
678 }
679 
680 static void ql_get_pauseparam(struct net_device *netdev,
681 			      struct ethtool_pauseparam *pause)
682 {
683 	struct ql_adapter *qdev = netdev_priv(netdev);
684 
685 	ql_mb_get_port_cfg(qdev);
686 	if (qdev->link_config & CFG_PAUSE_STD) {
687 		pause->rx_pause = 1;
688 		pause->tx_pause = 1;
689 	}
690 }
691 
692 static int ql_set_pauseparam(struct net_device *netdev,
693 			     struct ethtool_pauseparam *pause)
694 {
695 	struct ql_adapter *qdev = netdev_priv(netdev);
696 
697 	if ((pause->rx_pause) && (pause->tx_pause))
698 		qdev->link_config |= CFG_PAUSE_STD;
699 	else if (!pause->rx_pause && !pause->tx_pause)
700 		qdev->link_config &= ~CFG_PAUSE_STD;
701 	else
702 		return -EINVAL;
703 
704 	return ql_mb_set_port_cfg(qdev);
705 }
706 
707 static u32 ql_get_msglevel(struct net_device *ndev)
708 {
709 	struct ql_adapter *qdev = netdev_priv(ndev);
710 
711 	return qdev->msg_enable;
712 }
713 
714 static void ql_set_msglevel(struct net_device *ndev, u32 value)
715 {
716 	struct ql_adapter *qdev = netdev_priv(ndev);
717 
718 	qdev->msg_enable = value;
719 }
720 
721 const struct ethtool_ops qlge_ethtool_ops = {
722 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
723 				     ETHTOOL_COALESCE_MAX_FRAMES,
724 	.get_drvinfo = ql_get_drvinfo,
725 	.get_wol = ql_get_wol,
726 	.set_wol = ql_set_wol,
727 	.get_regs_len	= ql_get_regs_len,
728 	.get_regs = ql_get_regs,
729 	.get_msglevel = ql_get_msglevel,
730 	.set_msglevel = ql_set_msglevel,
731 	.get_link = ethtool_op_get_link,
732 	.set_phys_id		 = ql_set_phys_id,
733 	.self_test		 = ql_self_test,
734 	.get_pauseparam		 = ql_get_pauseparam,
735 	.set_pauseparam		 = ql_set_pauseparam,
736 	.get_coalesce = ql_get_coalesce,
737 	.set_coalesce = ql_set_coalesce,
738 	.get_sset_count = ql_get_sset_count,
739 	.get_strings = ql_get_strings,
740 	.get_ethtool_stats = ql_get_ethtool_stats,
741 	.get_link_ksettings = ql_get_link_ksettings,
742 };
743 
744