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 qlge_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 qlge_adapter, m)
44 #define QL_OFF(m) offsetof(struct qlge_adapter, m)
45 
46 static const struct qlge_stats qlge_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 qlge_gstrings_test[][ETH_GSTRING_LEN] = {
179 	"Loopback test  (offline)"
180 };
181 
182 #define QLGE_TEST_LEN (sizeof(qlge_gstrings_test) / ETH_GSTRING_LEN)
183 #define QLGE_STATS_LEN ARRAY_SIZE(qlge_gstrings_stats)
184 #define QLGE_RCV_MAC_ERR_STATS	7
185 
qlge_update_ring_coalescing(struct qlge_adapter * qdev)186 static int qlge_update_ring_coalescing(struct qlge_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 = qlge_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 = qlge_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 
qlge_update_stats(struct qlge_adapter * qdev)242 static void qlge_update_stats(struct qlge_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 (qlge_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 (qlge_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 (qlge_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 (qlge_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 (qlge_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 (qlge_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 	qlge_sem_unlock(qdev, qdev->xg_sem_mask);
329 quit:
330 	spin_unlock(&qdev->stats_lock);
331 }
332 
qlge_get_strings(struct net_device * dev,u32 stringset,u8 * buf)333 static void qlge_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
334 {
335 	int index;
336 
337 	switch (stringset) {
338 	case ETH_SS_TEST:
339 		memcpy(buf, *qlge_gstrings_test, QLGE_TEST_LEN * ETH_GSTRING_LEN);
340 		break;
341 	case ETH_SS_STATS:
342 		for (index = 0; index < QLGE_STATS_LEN; index++) {
343 			memcpy(buf + index * ETH_GSTRING_LEN,
344 			       qlge_gstrings_stats[index].stat_string,
345 			       ETH_GSTRING_LEN);
346 		}
347 		break;
348 	}
349 }
350 
qlge_get_sset_count(struct net_device * dev,int sset)351 static int qlge_get_sset_count(struct net_device *dev, int sset)
352 {
353 	switch (sset) {
354 	case ETH_SS_TEST:
355 		return QLGE_TEST_LEN;
356 	case ETH_SS_STATS:
357 		return QLGE_STATS_LEN;
358 	default:
359 		return -EOPNOTSUPP;
360 	}
361 }
362 
363 static void
qlge_get_ethtool_stats(struct net_device * ndev,struct ethtool_stats * stats,u64 * data)364 qlge_get_ethtool_stats(struct net_device *ndev,
365 		       struct ethtool_stats *stats, u64 *data)
366 {
367 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
368 	int index, length;
369 
370 	length = QLGE_STATS_LEN;
371 	qlge_update_stats(qdev);
372 
373 	for (index = 0; index < length; index++) {
374 		char *p = (char *)qdev +
375 			qlge_gstrings_stats[index].stat_offset;
376 		*data++ = (qlge_gstrings_stats[index].sizeof_stat ==
377 			   sizeof(u64)) ? *(u64 *)p : (*(u32 *)p);
378 	}
379 }
380 
qlge_get_link_ksettings(struct net_device * ndev,struct ethtool_link_ksettings * ecmd)381 static int qlge_get_link_ksettings(struct net_device *ndev,
382 				   struct ethtool_link_ksettings *ecmd)
383 {
384 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
385 	u32 supported, advertising;
386 
387 	supported = SUPPORTED_10000baseT_Full;
388 	advertising = ADVERTISED_10000baseT_Full;
389 
390 	if ((qdev->link_status & STS_LINK_TYPE_MASK) ==
391 	    STS_LINK_TYPE_10GBASET) {
392 		supported |= (SUPPORTED_TP | SUPPORTED_Autoneg);
393 		advertising |= (ADVERTISED_TP | ADVERTISED_Autoneg);
394 		ecmd->base.port = PORT_TP;
395 		ecmd->base.autoneg = AUTONEG_ENABLE;
396 	} else {
397 		supported |= SUPPORTED_FIBRE;
398 		advertising |= ADVERTISED_FIBRE;
399 		ecmd->base.port = PORT_FIBRE;
400 	}
401 
402 	ecmd->base.speed = SPEED_10000;
403 	ecmd->base.duplex = DUPLEX_FULL;
404 
405 	ethtool_convert_legacy_u32_to_link_mode(ecmd->link_modes.supported,
406 						supported);
407 	ethtool_convert_legacy_u32_to_link_mode(ecmd->link_modes.advertising,
408 						advertising);
409 
410 	return 0;
411 }
412 
qlge_get_drvinfo(struct net_device * ndev,struct ethtool_drvinfo * drvinfo)413 static void qlge_get_drvinfo(struct net_device *ndev,
414 			     struct ethtool_drvinfo *drvinfo)
415 {
416 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
417 
418 	strscpy(drvinfo->driver, qlge_driver_name, sizeof(drvinfo->driver));
419 	strscpy(drvinfo->version, qlge_driver_version,
420 		sizeof(drvinfo->version));
421 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
422 		 "v%d.%d.%d",
423 		 (qdev->fw_rev_id & 0x00ff0000) >> 16,
424 		 (qdev->fw_rev_id & 0x0000ff00) >> 8,
425 		 (qdev->fw_rev_id & 0x000000ff));
426 	strscpy(drvinfo->bus_info, pci_name(qdev->pdev),
427 		sizeof(drvinfo->bus_info));
428 }
429 
qlge_get_wol(struct net_device * ndev,struct ethtool_wolinfo * wol)430 static void qlge_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
431 {
432 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
433 	unsigned short ssys_dev = qdev->pdev->subsystem_device;
434 
435 	/* WOL is only supported for mezz card. */
436 	if (ssys_dev == QLGE_MEZZ_SSYS_ID_068 ||
437 	    ssys_dev == QLGE_MEZZ_SSYS_ID_180) {
438 		wol->supported = WAKE_MAGIC;
439 		wol->wolopts = qdev->wol;
440 	}
441 }
442 
qlge_set_wol(struct net_device * ndev,struct ethtool_wolinfo * wol)443 static int qlge_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
444 {
445 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
446 	unsigned short ssys_dev = qdev->pdev->subsystem_device;
447 
448 	/* WOL is only supported for mezz card. */
449 	if (ssys_dev != QLGE_MEZZ_SSYS_ID_068 &&
450 	    ssys_dev != QLGE_MEZZ_SSYS_ID_180) {
451 		netif_info(qdev, drv, qdev->ndev,
452 			   "WOL is only supported for mezz card\n");
453 		return -EOPNOTSUPP;
454 	}
455 	if (wol->wolopts & ~WAKE_MAGIC)
456 		return -EINVAL;
457 	qdev->wol = wol->wolopts;
458 
459 	netif_info(qdev, drv, qdev->ndev, "Set wol option 0x%x\n", qdev->wol);
460 	return 0;
461 }
462 
qlge_set_phys_id(struct net_device * ndev,enum ethtool_phys_id_state state)463 static int qlge_set_phys_id(struct net_device *ndev,
464 			    enum ethtool_phys_id_state state)
465 
466 {
467 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
468 
469 	switch (state) {
470 	case ETHTOOL_ID_ACTIVE:
471 		/* Save the current LED settings */
472 		if (qlge_mb_get_led_cfg(qdev))
473 			return -EIO;
474 
475 		/* Start blinking */
476 		qlge_mb_set_led_cfg(qdev, QL_LED_BLINK);
477 		return 0;
478 
479 	case ETHTOOL_ID_INACTIVE:
480 		/* Restore LED settings */
481 		if (qlge_mb_set_led_cfg(qdev, qdev->led_config))
482 			return -EIO;
483 		return 0;
484 
485 	default:
486 		return -EINVAL;
487 	}
488 }
489 
qlge_start_loopback(struct qlge_adapter * qdev)490 static int qlge_start_loopback(struct qlge_adapter *qdev)
491 {
492 	if (netif_carrier_ok(qdev->ndev)) {
493 		set_bit(QL_LB_LINK_UP, &qdev->flags);
494 		netif_carrier_off(qdev->ndev);
495 	} else {
496 		clear_bit(QL_LB_LINK_UP, &qdev->flags);
497 	}
498 	qdev->link_config |= CFG_LOOPBACK_PCS;
499 	return qlge_mb_set_port_cfg(qdev);
500 }
501 
qlge_stop_loopback(struct qlge_adapter * qdev)502 static void qlge_stop_loopback(struct qlge_adapter *qdev)
503 {
504 	qdev->link_config &= ~CFG_LOOPBACK_PCS;
505 	qlge_mb_set_port_cfg(qdev);
506 	if (test_bit(QL_LB_LINK_UP, &qdev->flags)) {
507 		netif_carrier_on(qdev->ndev);
508 		clear_bit(QL_LB_LINK_UP, &qdev->flags);
509 	}
510 }
511 
qlge_create_lb_frame(struct sk_buff * skb,unsigned int frame_size)512 static void qlge_create_lb_frame(struct sk_buff *skb,
513 				 unsigned int frame_size)
514 {
515 	memset(skb->data, 0xFF, frame_size);
516 	frame_size &= ~1;
517 	memset(&skb->data[frame_size / 2], 0xAA, frame_size / 2 - 1);
518 	skb->data[frame_size / 2 + 10] = (unsigned char)0xBE;
519 	skb->data[frame_size / 2 + 12] = (unsigned char)0xAF;
520 }
521 
qlge_check_lb_frame(struct qlge_adapter * qdev,struct sk_buff * skb)522 void qlge_check_lb_frame(struct qlge_adapter *qdev,
523 			 struct sk_buff *skb)
524 {
525 	unsigned int frame_size = skb->len;
526 
527 	if ((*(skb->data + 3) == 0xFF) &&
528 	    (*(skb->data + frame_size / 2 + 10) == 0xBE) &&
529 	    (*(skb->data + frame_size / 2 + 12) == 0xAF)) {
530 		atomic_dec(&qdev->lb_count);
531 		return;
532 	}
533 }
534 
qlge_run_loopback_test(struct qlge_adapter * qdev)535 static int qlge_run_loopback_test(struct qlge_adapter *qdev)
536 {
537 	int i;
538 	netdev_tx_t rc;
539 	struct sk_buff *skb;
540 	unsigned int size = SMALL_BUF_MAP_SIZE;
541 
542 	for (i = 0; i < 64; i++) {
543 		skb = netdev_alloc_skb(qdev->ndev, size);
544 		if (!skb)
545 			return -ENOMEM;
546 
547 		skb->queue_mapping = 0;
548 		skb_put(skb, size);
549 		qlge_create_lb_frame(skb, size);
550 		rc = qlge_lb_send(skb, qdev->ndev);
551 		if (rc != NETDEV_TX_OK)
552 			return -EPIPE;
553 		atomic_inc(&qdev->lb_count);
554 	}
555 	/* Give queue time to settle before testing results. */
556 	usleep_range(2000, 2100);
557 	qlge_clean_lb_rx_ring(&qdev->rx_ring[0], 128);
558 	return atomic_read(&qdev->lb_count) ? -EIO : 0;
559 }
560 
qlge_loopback_test(struct qlge_adapter * qdev,u64 * data)561 static int qlge_loopback_test(struct qlge_adapter *qdev, u64 *data)
562 {
563 	*data = qlge_start_loopback(qdev);
564 	if (*data)
565 		goto out;
566 	*data = qlge_run_loopback_test(qdev);
567 out:
568 	qlge_stop_loopback(qdev);
569 	return *data;
570 }
571 
qlge_self_test(struct net_device * ndev,struct ethtool_test * eth_test,u64 * data)572 static void qlge_self_test(struct net_device *ndev,
573 			   struct ethtool_test *eth_test, u64 *data)
574 {
575 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
576 
577 	memset(data, 0, sizeof(u64) * QLGE_TEST_LEN);
578 
579 	if (netif_running(ndev)) {
580 		set_bit(QL_SELFTEST, &qdev->flags);
581 		if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
582 			/* Offline tests */
583 			if (qlge_loopback_test(qdev, &data[0]))
584 				eth_test->flags |= ETH_TEST_FL_FAILED;
585 
586 		} else {
587 			/* Online tests */
588 			data[0] = 0;
589 		}
590 		clear_bit(QL_SELFTEST, &qdev->flags);
591 		/* Give link time to come up after
592 		 * port configuration changes.
593 		 */
594 		msleep_interruptible(4 * 1000);
595 	} else {
596 		netif_err(qdev, drv, qdev->ndev,
597 			  "is down, Loopback test will fail.\n");
598 		eth_test->flags |= ETH_TEST_FL_FAILED;
599 	}
600 }
601 
qlge_get_regs_len(struct net_device * ndev)602 static int qlge_get_regs_len(struct net_device *ndev)
603 {
604 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
605 
606 	if (!test_bit(QL_FRC_COREDUMP, &qdev->flags))
607 		return sizeof(struct qlge_mpi_coredump);
608 	else
609 		return sizeof(struct qlge_reg_dump);
610 }
611 
qlge_get_regs(struct net_device * ndev,struct ethtool_regs * regs,void * p)612 static void qlge_get_regs(struct net_device *ndev,
613 			  struct ethtool_regs *regs, void *p)
614 {
615 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
616 
617 	qlge_get_dump(qdev, p);
618 	if (!test_bit(QL_FRC_COREDUMP, &qdev->flags))
619 		regs->len = sizeof(struct qlge_mpi_coredump);
620 	else
621 		regs->len = sizeof(struct qlge_reg_dump);
622 }
623 
qlge_get_coalesce(struct net_device * ndev,struct ethtool_coalesce * c,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)624 static int qlge_get_coalesce(struct net_device *ndev,
625 			     struct ethtool_coalesce *c,
626 			     struct kernel_ethtool_coalesce *kernel_coal,
627 			     struct netlink_ext_ack *extack)
628 {
629 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
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 
qlge_set_coalesce(struct net_device * ndev,struct ethtool_coalesce * c,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)650 static int qlge_set_coalesce(struct net_device *ndev,
651 			     struct ethtool_coalesce *c,
652 			     struct kernel_ethtool_coalesce *kernel_coal,
653 			     struct netlink_ext_ack *extack)
654 {
655 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
656 
657 	/* Validate user parameters. */
658 	if (c->rx_coalesce_usecs > qdev->rx_ring_size / 2)
659 		return -EINVAL;
660 	/* Don't wait more than 10 usec. */
661 	if (c->rx_max_coalesced_frames > MAX_INTER_FRAME_WAIT)
662 		return -EINVAL;
663 	if (c->tx_coalesce_usecs > qdev->tx_ring_size / 2)
664 		return -EINVAL;
665 	if (c->tx_max_coalesced_frames > MAX_INTER_FRAME_WAIT)
666 		return -EINVAL;
667 
668 	/* Verify a change took place before updating the hardware. */
669 	if (qdev->rx_coalesce_usecs == c->rx_coalesce_usecs &&
670 	    qdev->tx_coalesce_usecs == c->tx_coalesce_usecs &&
671 	    qdev->rx_max_coalesced_frames == c->rx_max_coalesced_frames &&
672 	    qdev->tx_max_coalesced_frames == c->tx_max_coalesced_frames)
673 		return 0;
674 
675 	qdev->rx_coalesce_usecs = c->rx_coalesce_usecs;
676 	qdev->tx_coalesce_usecs = c->tx_coalesce_usecs;
677 	qdev->rx_max_coalesced_frames = c->rx_max_coalesced_frames;
678 	qdev->tx_max_coalesced_frames = c->tx_max_coalesced_frames;
679 
680 	return qlge_update_ring_coalescing(qdev);
681 }
682 
qlge_get_pauseparam(struct net_device * ndev,struct ethtool_pauseparam * pause)683 static void qlge_get_pauseparam(struct net_device *ndev,
684 				struct ethtool_pauseparam *pause)
685 {
686 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
687 
688 	qlge_mb_get_port_cfg(qdev);
689 	if (qdev->link_config & CFG_PAUSE_STD) {
690 		pause->rx_pause = 1;
691 		pause->tx_pause = 1;
692 	}
693 }
694 
qlge_set_pauseparam(struct net_device * ndev,struct ethtool_pauseparam * pause)695 static int qlge_set_pauseparam(struct net_device *ndev,
696 			       struct ethtool_pauseparam *pause)
697 {
698 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
699 
700 	if ((pause->rx_pause) && (pause->tx_pause))
701 		qdev->link_config |= CFG_PAUSE_STD;
702 	else if (!pause->rx_pause && !pause->tx_pause)
703 		qdev->link_config &= ~CFG_PAUSE_STD;
704 	else
705 		return -EINVAL;
706 
707 	return qlge_mb_set_port_cfg(qdev);
708 }
709 
qlge_get_msglevel(struct net_device * ndev)710 static u32 qlge_get_msglevel(struct net_device *ndev)
711 {
712 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
713 
714 	return qdev->msg_enable;
715 }
716 
qlge_set_msglevel(struct net_device * ndev,u32 value)717 static void qlge_set_msglevel(struct net_device *ndev, u32 value)
718 {
719 	struct qlge_adapter *qdev = netdev_to_qdev(ndev);
720 
721 	qdev->msg_enable = value;
722 }
723 
724 const struct ethtool_ops qlge_ethtool_ops = {
725 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
726 				     ETHTOOL_COALESCE_MAX_FRAMES,
727 	.get_drvinfo = qlge_get_drvinfo,
728 	.get_wol = qlge_get_wol,
729 	.set_wol = qlge_set_wol,
730 	.get_regs_len	= qlge_get_regs_len,
731 	.get_regs = qlge_get_regs,
732 	.get_msglevel = qlge_get_msglevel,
733 	.set_msglevel = qlge_set_msglevel,
734 	.get_link = ethtool_op_get_link,
735 	.set_phys_id		 = qlge_set_phys_id,
736 	.self_test		 = qlge_self_test,
737 	.get_pauseparam		 = qlge_get_pauseparam,
738 	.set_pauseparam		 = qlge_set_pauseparam,
739 	.get_coalesce = qlge_get_coalesce,
740 	.set_coalesce = qlge_set_coalesce,
741 	.get_sset_count = qlge_get_sset_count,
742 	.get_strings = qlge_get_strings,
743 	.get_ethtool_stats = qlge_get_ethtool_stats,
744 	.get_link_ksettings = qlge_get_link_ksettings,
745 };
746 
747