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