• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 Cavium, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License
6  * as published by the Free Software Foundation.
7  */
8 
9 /* ETHTOOL Support for VNIC_VF Device*/
10 
11 #include <linux/pci.h>
12 
13 #include "nic_reg.h"
14 #include "nic.h"
15 #include "nicvf_queues.h"
16 #include "q_struct.h"
17 #include "thunder_bgx.h"
18 
19 #define DRV_NAME	"thunder-nicvf"
20 #define DRV_VERSION     "1.0"
21 
22 struct nicvf_stat {
23 	char name[ETH_GSTRING_LEN];
24 	unsigned int index;
25 };
26 
27 #define NICVF_HW_STAT(stat) { \
28 	.name = #stat, \
29 	.index = offsetof(struct nicvf_hw_stats, stat) / sizeof(u64), \
30 }
31 
32 #define NICVF_DRV_STAT(stat) { \
33 	.name = #stat, \
34 	.index = offsetof(struct nicvf_drv_stats, stat) / sizeof(u64), \
35 }
36 
37 static const struct nicvf_stat nicvf_hw_stats[] = {
38 	NICVF_HW_STAT(rx_bytes),
39 	NICVF_HW_STAT(rx_ucast_frames),
40 	NICVF_HW_STAT(rx_bcast_frames),
41 	NICVF_HW_STAT(rx_mcast_frames),
42 	NICVF_HW_STAT(rx_fcs_errors),
43 	NICVF_HW_STAT(rx_l2_errors),
44 	NICVF_HW_STAT(rx_drop_red),
45 	NICVF_HW_STAT(rx_drop_red_bytes),
46 	NICVF_HW_STAT(rx_drop_overrun),
47 	NICVF_HW_STAT(rx_drop_overrun_bytes),
48 	NICVF_HW_STAT(rx_drop_bcast),
49 	NICVF_HW_STAT(rx_drop_mcast),
50 	NICVF_HW_STAT(rx_drop_l3_bcast),
51 	NICVF_HW_STAT(rx_drop_l3_mcast),
52 	NICVF_HW_STAT(rx_bgx_truncated_pkts),
53 	NICVF_HW_STAT(rx_jabber_errs),
54 	NICVF_HW_STAT(rx_fcs_errs),
55 	NICVF_HW_STAT(rx_bgx_errs),
56 	NICVF_HW_STAT(rx_prel2_errs),
57 	NICVF_HW_STAT(rx_l2_hdr_malformed),
58 	NICVF_HW_STAT(rx_oversize),
59 	NICVF_HW_STAT(rx_undersize),
60 	NICVF_HW_STAT(rx_l2_len_mismatch),
61 	NICVF_HW_STAT(rx_l2_pclp),
62 	NICVF_HW_STAT(rx_ip_ver_errs),
63 	NICVF_HW_STAT(rx_ip_csum_errs),
64 	NICVF_HW_STAT(rx_ip_hdr_malformed),
65 	NICVF_HW_STAT(rx_ip_payload_malformed),
66 	NICVF_HW_STAT(rx_ip_ttl_errs),
67 	NICVF_HW_STAT(rx_l3_pclp),
68 	NICVF_HW_STAT(rx_l4_malformed),
69 	NICVF_HW_STAT(rx_l4_csum_errs),
70 	NICVF_HW_STAT(rx_udp_len_errs),
71 	NICVF_HW_STAT(rx_l4_port_errs),
72 	NICVF_HW_STAT(rx_tcp_flag_errs),
73 	NICVF_HW_STAT(rx_tcp_offset_errs),
74 	NICVF_HW_STAT(rx_l4_pclp),
75 	NICVF_HW_STAT(rx_truncated_pkts),
76 	NICVF_HW_STAT(tx_bytes_ok),
77 	NICVF_HW_STAT(tx_ucast_frames_ok),
78 	NICVF_HW_STAT(tx_bcast_frames_ok),
79 	NICVF_HW_STAT(tx_mcast_frames_ok),
80 };
81 
82 static const struct nicvf_stat nicvf_drv_stats[] = {
83 	NICVF_DRV_STAT(rx_frames_ok),
84 	NICVF_DRV_STAT(rx_frames_64),
85 	NICVF_DRV_STAT(rx_frames_127),
86 	NICVF_DRV_STAT(rx_frames_255),
87 	NICVF_DRV_STAT(rx_frames_511),
88 	NICVF_DRV_STAT(rx_frames_1023),
89 	NICVF_DRV_STAT(rx_frames_1518),
90 	NICVF_DRV_STAT(rx_frames_jumbo),
91 	NICVF_DRV_STAT(rx_drops),
92 	NICVF_DRV_STAT(tx_frames_ok),
93 	NICVF_DRV_STAT(tx_tso),
94 	NICVF_DRV_STAT(tx_drops),
95 	NICVF_DRV_STAT(txq_stop),
96 	NICVF_DRV_STAT(txq_wake),
97 };
98 
99 static const struct nicvf_stat nicvf_queue_stats[] = {
100 	{ "bytes", 0 },
101 	{ "frames", 1 },
102 };
103 
104 static const unsigned int nicvf_n_hw_stats = ARRAY_SIZE(nicvf_hw_stats);
105 static const unsigned int nicvf_n_drv_stats = ARRAY_SIZE(nicvf_drv_stats);
106 static const unsigned int nicvf_n_queue_stats = ARRAY_SIZE(nicvf_queue_stats);
107 
nicvf_get_settings(struct net_device * netdev,struct ethtool_cmd * cmd)108 static int nicvf_get_settings(struct net_device *netdev,
109 			      struct ethtool_cmd *cmd)
110 {
111 	struct nicvf *nic = netdev_priv(netdev);
112 
113 	cmd->supported = 0;
114 	cmd->transceiver = XCVR_EXTERNAL;
115 
116 	if (!nic->link_up) {
117 		cmd->duplex = DUPLEX_UNKNOWN;
118 		ethtool_cmd_speed_set(cmd, SPEED_UNKNOWN);
119 		return 0;
120 	}
121 
122 	if (nic->speed <= 1000) {
123 		cmd->port = PORT_MII;
124 		cmd->autoneg = AUTONEG_ENABLE;
125 	} else {
126 		cmd->port = PORT_FIBRE;
127 		cmd->autoneg = AUTONEG_DISABLE;
128 	}
129 	cmd->duplex = nic->duplex;
130 	ethtool_cmd_speed_set(cmd, nic->speed);
131 
132 	return 0;
133 }
134 
nicvf_get_link(struct net_device * netdev)135 static u32 nicvf_get_link(struct net_device *netdev)
136 {
137 	struct nicvf *nic = netdev_priv(netdev);
138 
139 	return nic->link_up;
140 }
141 
nicvf_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * info)142 static void nicvf_get_drvinfo(struct net_device *netdev,
143 			      struct ethtool_drvinfo *info)
144 {
145 	struct nicvf *nic = netdev_priv(netdev);
146 
147 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
148 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
149 	strlcpy(info->bus_info, pci_name(nic->pdev), sizeof(info->bus_info));
150 }
151 
nicvf_get_msglevel(struct net_device * netdev)152 static u32 nicvf_get_msglevel(struct net_device *netdev)
153 {
154 	struct nicvf *nic = netdev_priv(netdev);
155 
156 	return nic->msg_enable;
157 }
158 
nicvf_set_msglevel(struct net_device * netdev,u32 lvl)159 static void nicvf_set_msglevel(struct net_device *netdev, u32 lvl)
160 {
161 	struct nicvf *nic = netdev_priv(netdev);
162 
163 	nic->msg_enable = lvl;
164 }
165 
nicvf_get_qset_strings(struct nicvf * nic,u8 ** data,int qset)166 static void nicvf_get_qset_strings(struct nicvf *nic, u8 **data, int qset)
167 {
168 	int stats, qidx;
169 	int start_qidx = qset * MAX_RCV_QUEUES_PER_QS;
170 
171 	for (qidx = 0; qidx < nic->qs->rq_cnt; qidx++) {
172 		for (stats = 0; stats < nicvf_n_queue_stats; stats++) {
173 			sprintf(*data, "rxq%d: %s", qidx + start_qidx,
174 				nicvf_queue_stats[stats].name);
175 			*data += ETH_GSTRING_LEN;
176 		}
177 	}
178 
179 	for (qidx = 0; qidx < nic->qs->sq_cnt; qidx++) {
180 		for (stats = 0; stats < nicvf_n_queue_stats; stats++) {
181 			sprintf(*data, "txq%d: %s", qidx + start_qidx,
182 				nicvf_queue_stats[stats].name);
183 			*data += ETH_GSTRING_LEN;
184 		}
185 	}
186 }
187 
nicvf_get_strings(struct net_device * netdev,u32 sset,u8 * data)188 static void nicvf_get_strings(struct net_device *netdev, u32 sset, u8 *data)
189 {
190 	struct nicvf *nic = netdev_priv(netdev);
191 	int stats;
192 	int sqs;
193 
194 	if (sset != ETH_SS_STATS)
195 		return;
196 
197 	for (stats = 0; stats < nicvf_n_hw_stats; stats++) {
198 		memcpy(data, nicvf_hw_stats[stats].name, ETH_GSTRING_LEN);
199 		data += ETH_GSTRING_LEN;
200 	}
201 
202 	for (stats = 0; stats < nicvf_n_drv_stats; stats++) {
203 		memcpy(data, nicvf_drv_stats[stats].name, ETH_GSTRING_LEN);
204 		data += ETH_GSTRING_LEN;
205 	}
206 
207 	nicvf_get_qset_strings(nic, &data, 0);
208 
209 	for (sqs = 0; sqs < nic->sqs_count; sqs++) {
210 		if (!nic->snicvf[sqs])
211 			continue;
212 		nicvf_get_qset_strings(nic->snicvf[sqs], &data, sqs + 1);
213 	}
214 
215 	for (stats = 0; stats < BGX_RX_STATS_COUNT; stats++) {
216 		sprintf(data, "bgx_rxstat%d: ", stats);
217 		data += ETH_GSTRING_LEN;
218 	}
219 
220 	for (stats = 0; stats < BGX_TX_STATS_COUNT; stats++) {
221 		sprintf(data, "bgx_txstat%d: ", stats);
222 		data += ETH_GSTRING_LEN;
223 	}
224 }
225 
nicvf_get_sset_count(struct net_device * netdev,int sset)226 static int nicvf_get_sset_count(struct net_device *netdev, int sset)
227 {
228 	struct nicvf *nic = netdev_priv(netdev);
229 	int qstats_count;
230 	int sqs;
231 
232 	if (sset != ETH_SS_STATS)
233 		return -EINVAL;
234 
235 	qstats_count = nicvf_n_queue_stats *
236 		       (nic->qs->rq_cnt + nic->qs->sq_cnt);
237 	for (sqs = 0; sqs < nic->sqs_count; sqs++) {
238 		struct nicvf *snic;
239 
240 		snic = nic->snicvf[sqs];
241 		if (!snic)
242 			continue;
243 		qstats_count += nicvf_n_queue_stats *
244 				(snic->qs->rq_cnt + snic->qs->sq_cnt);
245 	}
246 
247 	return nicvf_n_hw_stats + nicvf_n_drv_stats +
248 		qstats_count +
249 		BGX_RX_STATS_COUNT + BGX_TX_STATS_COUNT;
250 }
251 
nicvf_get_qset_stats(struct nicvf * nic,struct ethtool_stats * stats,u64 ** data)252 static void nicvf_get_qset_stats(struct nicvf *nic,
253 				 struct ethtool_stats *stats, u64 **data)
254 {
255 	int stat, qidx;
256 
257 	if (!nic)
258 		return;
259 
260 	for (qidx = 0; qidx < nic->qs->rq_cnt; qidx++) {
261 		nicvf_update_rq_stats(nic, qidx);
262 		for (stat = 0; stat < nicvf_n_queue_stats; stat++)
263 			*((*data)++) = ((u64 *)&nic->qs->rq[qidx].stats)
264 					[nicvf_queue_stats[stat].index];
265 	}
266 
267 	for (qidx = 0; qidx < nic->qs->sq_cnt; qidx++) {
268 		nicvf_update_sq_stats(nic, qidx);
269 		for (stat = 0; stat < nicvf_n_queue_stats; stat++)
270 			*((*data)++) = ((u64 *)&nic->qs->sq[qidx].stats)
271 					[nicvf_queue_stats[stat].index];
272 	}
273 }
274 
nicvf_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)275 static void nicvf_get_ethtool_stats(struct net_device *netdev,
276 				    struct ethtool_stats *stats, u64 *data)
277 {
278 	struct nicvf *nic = netdev_priv(netdev);
279 	int stat;
280 	int sqs;
281 
282 	nicvf_update_stats(nic);
283 
284 	/* Update LMAC stats */
285 	nicvf_update_lmac_stats(nic);
286 
287 	for (stat = 0; stat < nicvf_n_hw_stats; stat++)
288 		*(data++) = ((u64 *)&nic->hw_stats)
289 				[nicvf_hw_stats[stat].index];
290 	for (stat = 0; stat < nicvf_n_drv_stats; stat++)
291 		*(data++) = ((u64 *)&nic->drv_stats)
292 				[nicvf_drv_stats[stat].index];
293 
294 	nicvf_get_qset_stats(nic, stats, &data);
295 
296 	for (sqs = 0; sqs < nic->sqs_count; sqs++) {
297 		if (!nic->snicvf[sqs])
298 			continue;
299 		nicvf_get_qset_stats(nic->snicvf[sqs], stats, &data);
300 	}
301 
302 	for (stat = 0; stat < BGX_RX_STATS_COUNT; stat++)
303 		*(data++) = nic->bgx_stats.rx_stats[stat];
304 	for (stat = 0; stat < BGX_TX_STATS_COUNT; stat++)
305 		*(data++) = nic->bgx_stats.tx_stats[stat];
306 }
307 
nicvf_get_regs_len(struct net_device * dev)308 static int nicvf_get_regs_len(struct net_device *dev)
309 {
310 	return sizeof(u64) * NIC_VF_REG_COUNT;
311 }
312 
nicvf_get_regs(struct net_device * dev,struct ethtool_regs * regs,void * reg)313 static void nicvf_get_regs(struct net_device *dev,
314 			   struct ethtool_regs *regs, void *reg)
315 {
316 	struct nicvf *nic = netdev_priv(dev);
317 	u64 *p = (u64 *)reg;
318 	u64 reg_offset;
319 	int mbox, key, stat, q;
320 	int i = 0;
321 
322 	regs->version = 0;
323 	memset(p, 0, NIC_VF_REG_COUNT);
324 
325 	p[i++] = nicvf_reg_read(nic, NIC_VNIC_CFG);
326 	/* Mailbox registers */
327 	for (mbox = 0; mbox < NIC_PF_VF_MAILBOX_SIZE; mbox++)
328 		p[i++] = nicvf_reg_read(nic,
329 					NIC_VF_PF_MAILBOX_0_1 | (mbox << 3));
330 
331 	p[i++] = nicvf_reg_read(nic, NIC_VF_INT);
332 	p[i++] = nicvf_reg_read(nic, NIC_VF_INT_W1S);
333 	p[i++] = nicvf_reg_read(nic, NIC_VF_ENA_W1C);
334 	p[i++] = nicvf_reg_read(nic, NIC_VF_ENA_W1S);
335 	p[i++] = nicvf_reg_read(nic, NIC_VNIC_RSS_CFG);
336 
337 	for (key = 0; key < RSS_HASH_KEY_SIZE; key++)
338 		p[i++] = nicvf_reg_read(nic, NIC_VNIC_RSS_KEY_0_4 | (key << 3));
339 
340 	/* Tx/Rx statistics */
341 	for (stat = 0; stat < TX_STATS_ENUM_LAST; stat++)
342 		p[i++] = nicvf_reg_read(nic,
343 					NIC_VNIC_TX_STAT_0_4 | (stat << 3));
344 
345 	for (i = 0; i < RX_STATS_ENUM_LAST; i++)
346 		p[i++] = nicvf_reg_read(nic,
347 					NIC_VNIC_RX_STAT_0_13 | (stat << 3));
348 
349 	p[i++] = nicvf_reg_read(nic, NIC_QSET_RQ_GEN_CFG);
350 
351 	/* All completion queue's registers */
352 	for (q = 0; q < MAX_CMP_QUEUES_PER_QS; q++) {
353 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_CFG, q);
354 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_CFG2, q);
355 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_THRESH, q);
356 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_BASE, q);
357 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD, q);
358 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_TAIL, q);
359 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_DOOR, q);
360 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS, q);
361 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS2, q);
362 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_DEBUG, q);
363 	}
364 
365 	/* All receive queue's registers */
366 	for (q = 0; q < MAX_RCV_QUEUES_PER_QS; q++) {
367 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RQ_0_7_CFG, q);
368 		p[i++] = nicvf_queue_reg_read(nic,
369 						  NIC_QSET_RQ_0_7_STAT_0_1, q);
370 		reg_offset = NIC_QSET_RQ_0_7_STAT_0_1 | (1 << 3);
371 		p[i++] = nicvf_queue_reg_read(nic, reg_offset, q);
372 	}
373 
374 	for (q = 0; q < MAX_SND_QUEUES_PER_QS; q++) {
375 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_CFG, q);
376 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_THRESH, q);
377 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_BASE, q);
378 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_HEAD, q);
379 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_TAIL, q);
380 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_DOOR, q);
381 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_STATUS, q);
382 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_DEBUG, q);
383 		/* Padding, was NIC_QSET_SQ_0_7_CNM_CHG, which
384 		 * produces bus errors when read
385 		 */
386 		p[i++] = 0;
387 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_SQ_0_7_STAT_0_1, q);
388 		reg_offset = NIC_QSET_SQ_0_7_STAT_0_1 | (1 << 3);
389 		p[i++] = nicvf_queue_reg_read(nic, reg_offset, q);
390 	}
391 
392 	for (q = 0; q < MAX_RCV_BUF_DESC_RINGS_PER_QS; q++) {
393 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_CFG, q);
394 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_THRESH, q);
395 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_BASE, q);
396 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_HEAD, q);
397 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_TAIL, q);
398 		p[i++] = nicvf_queue_reg_read(nic, NIC_QSET_RBDR_0_1_DOOR, q);
399 		p[i++] = nicvf_queue_reg_read(nic,
400 					      NIC_QSET_RBDR_0_1_STATUS0, q);
401 		p[i++] = nicvf_queue_reg_read(nic,
402 					      NIC_QSET_RBDR_0_1_STATUS1, q);
403 		reg_offset = NIC_QSET_RBDR_0_1_PREFETCH_STATUS;
404 		p[i++] = nicvf_queue_reg_read(nic, reg_offset, q);
405 	}
406 }
407 
nicvf_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * cmd)408 static int nicvf_get_coalesce(struct net_device *netdev,
409 			      struct ethtool_coalesce *cmd)
410 {
411 	struct nicvf *nic = netdev_priv(netdev);
412 
413 	cmd->rx_coalesce_usecs = nic->cq_coalesce_usecs;
414 	return 0;
415 }
416 
nicvf_get_ringparam(struct net_device * netdev,struct ethtool_ringparam * ring)417 static void nicvf_get_ringparam(struct net_device *netdev,
418 				struct ethtool_ringparam *ring)
419 {
420 	struct nicvf *nic = netdev_priv(netdev);
421 	struct queue_set *qs = nic->qs;
422 
423 	ring->rx_max_pending = MAX_RCV_BUF_COUNT;
424 	ring->rx_pending = qs->rbdr_len;
425 	ring->tx_max_pending = MAX_SND_QUEUE_LEN;
426 	ring->tx_pending = qs->sq_len;
427 }
428 
nicvf_get_rss_hash_opts(struct nicvf * nic,struct ethtool_rxnfc * info)429 static int nicvf_get_rss_hash_opts(struct nicvf *nic,
430 				   struct ethtool_rxnfc *info)
431 {
432 	info->data = 0;
433 
434 	switch (info->flow_type) {
435 	case TCP_V4_FLOW:
436 	case TCP_V6_FLOW:
437 	case UDP_V4_FLOW:
438 	case UDP_V6_FLOW:
439 	case SCTP_V4_FLOW:
440 	case SCTP_V6_FLOW:
441 		info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
442 	case IPV4_FLOW:
443 	case IPV6_FLOW:
444 		info->data |= RXH_IP_SRC | RXH_IP_DST;
445 		break;
446 	default:
447 		return -EINVAL;
448 	}
449 
450 	return 0;
451 }
452 
nicvf_get_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info,u32 * rules)453 static int nicvf_get_rxnfc(struct net_device *dev,
454 			   struct ethtool_rxnfc *info, u32 *rules)
455 {
456 	struct nicvf *nic = netdev_priv(dev);
457 	int ret = -EOPNOTSUPP;
458 
459 	switch (info->cmd) {
460 	case ETHTOOL_GRXRINGS:
461 		info->data = nic->rx_queues;
462 		ret = 0;
463 		break;
464 	case ETHTOOL_GRXFH:
465 		return nicvf_get_rss_hash_opts(nic, info);
466 	default:
467 		break;
468 	}
469 	return ret;
470 }
471 
nicvf_set_rss_hash_opts(struct nicvf * nic,struct ethtool_rxnfc * info)472 static int nicvf_set_rss_hash_opts(struct nicvf *nic,
473 				   struct ethtool_rxnfc *info)
474 {
475 	struct nicvf_rss_info *rss = &nic->rss_info;
476 	u64 rss_cfg = nicvf_reg_read(nic, NIC_VNIC_RSS_CFG);
477 
478 	if (!rss->enable)
479 		netdev_err(nic->netdev,
480 			   "RSS is disabled, hash cannot be set\n");
481 
482 	netdev_info(nic->netdev, "Set RSS flow type = %d, data = %lld\n",
483 		    info->flow_type, info->data);
484 
485 	if (!(info->data & RXH_IP_SRC) || !(info->data & RXH_IP_DST))
486 		return -EINVAL;
487 
488 	switch (info->flow_type) {
489 	case TCP_V4_FLOW:
490 	case TCP_V6_FLOW:
491 		switch (info->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
492 		case 0:
493 			rss_cfg &= ~(1ULL << RSS_HASH_TCP);
494 			break;
495 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
496 			rss_cfg |= (1ULL << RSS_HASH_TCP);
497 			break;
498 		default:
499 			return -EINVAL;
500 		}
501 		break;
502 	case UDP_V4_FLOW:
503 	case UDP_V6_FLOW:
504 		switch (info->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
505 		case 0:
506 			rss_cfg &= ~(1ULL << RSS_HASH_UDP);
507 			break;
508 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
509 			rss_cfg |= (1ULL << RSS_HASH_UDP);
510 			break;
511 		default:
512 			return -EINVAL;
513 		}
514 		break;
515 	case SCTP_V4_FLOW:
516 	case SCTP_V6_FLOW:
517 		switch (info->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
518 		case 0:
519 			rss_cfg &= ~(1ULL << RSS_HASH_L4ETC);
520 			break;
521 		case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
522 			rss_cfg |= (1ULL << RSS_HASH_L4ETC);
523 			break;
524 		default:
525 			return -EINVAL;
526 		}
527 		break;
528 	case IPV4_FLOW:
529 	case IPV6_FLOW:
530 		rss_cfg = RSS_HASH_IP;
531 		break;
532 	default:
533 		return -EINVAL;
534 	}
535 
536 	nicvf_reg_write(nic, NIC_VNIC_RSS_CFG, rss_cfg);
537 	return 0;
538 }
539 
nicvf_set_rxnfc(struct net_device * dev,struct ethtool_rxnfc * info)540 static int nicvf_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
541 {
542 	struct nicvf *nic = netdev_priv(dev);
543 
544 	switch (info->cmd) {
545 	case ETHTOOL_SRXFH:
546 		return nicvf_set_rss_hash_opts(nic, info);
547 	default:
548 		break;
549 	}
550 	return -EOPNOTSUPP;
551 }
552 
nicvf_get_rxfh_key_size(struct net_device * netdev)553 static u32 nicvf_get_rxfh_key_size(struct net_device *netdev)
554 {
555 	return RSS_HASH_KEY_SIZE * sizeof(u64);
556 }
557 
nicvf_get_rxfh_indir_size(struct net_device * dev)558 static u32 nicvf_get_rxfh_indir_size(struct net_device *dev)
559 {
560 	struct nicvf *nic = netdev_priv(dev);
561 
562 	return nic->rss_info.rss_size;
563 }
564 
nicvf_get_rxfh(struct net_device * dev,u32 * indir,u8 * hkey,u8 * hfunc)565 static int nicvf_get_rxfh(struct net_device *dev, u32 *indir, u8 *hkey,
566 			  u8 *hfunc)
567 {
568 	struct nicvf *nic = netdev_priv(dev);
569 	struct nicvf_rss_info *rss = &nic->rss_info;
570 	int idx;
571 
572 	if (indir) {
573 		for (idx = 0; idx < rss->rss_size; idx++)
574 			indir[idx] = rss->ind_tbl[idx];
575 	}
576 
577 	if (hkey)
578 		memcpy(hkey, rss->key, RSS_HASH_KEY_SIZE * sizeof(u64));
579 
580 	if (hfunc)
581 		*hfunc = ETH_RSS_HASH_TOP;
582 
583 	return 0;
584 }
585 
nicvf_set_rxfh(struct net_device * dev,const u32 * indir,const u8 * hkey,u8 hfunc)586 static int nicvf_set_rxfh(struct net_device *dev, const u32 *indir,
587 			  const u8 *hkey, u8 hfunc)
588 {
589 	struct nicvf *nic = netdev_priv(dev);
590 	struct nicvf_rss_info *rss = &nic->rss_info;
591 	int idx;
592 
593 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
594 		return -EOPNOTSUPP;
595 
596 	if (!rss->enable) {
597 		netdev_err(nic->netdev,
598 			   "RSS is disabled, cannot change settings\n");
599 		return -EIO;
600 	}
601 
602 	if (indir) {
603 		for (idx = 0; idx < rss->rss_size; idx++)
604 			rss->ind_tbl[idx] = indir[idx];
605 	}
606 
607 	if (hkey) {
608 		memcpy(rss->key, hkey, RSS_HASH_KEY_SIZE * sizeof(u64));
609 		nicvf_set_rss_key(nic);
610 	}
611 
612 	nicvf_config_rss(nic);
613 	return 0;
614 }
615 
616 /* Get no of queues device supports and current queue count */
nicvf_get_channels(struct net_device * dev,struct ethtool_channels * channel)617 static void nicvf_get_channels(struct net_device *dev,
618 			       struct ethtool_channels *channel)
619 {
620 	struct nicvf *nic = netdev_priv(dev);
621 
622 	memset(channel, 0, sizeof(*channel));
623 
624 	channel->max_rx = nic->max_queues;
625 	channel->max_tx = nic->max_queues;
626 
627 	channel->rx_count = nic->rx_queues;
628 	channel->tx_count = nic->tx_queues;
629 }
630 
631 /* Set no of Tx, Rx queues to be used */
nicvf_set_channels(struct net_device * dev,struct ethtool_channels * channel)632 static int nicvf_set_channels(struct net_device *dev,
633 			      struct ethtool_channels *channel)
634 {
635 	struct nicvf *nic = netdev_priv(dev);
636 	int err = 0;
637 	bool if_up = netif_running(dev);
638 	int cqcount;
639 
640 	if (!channel->rx_count || !channel->tx_count)
641 		return -EINVAL;
642 	if (channel->rx_count > nic->max_queues)
643 		return -EINVAL;
644 	if (channel->tx_count > nic->max_queues)
645 		return -EINVAL;
646 
647 	if (if_up)
648 		nicvf_stop(dev);
649 
650 	cqcount = max(channel->rx_count, channel->tx_count);
651 
652 	if (cqcount > MAX_CMP_QUEUES_PER_QS) {
653 		nic->sqs_count = roundup(cqcount, MAX_CMP_QUEUES_PER_QS);
654 		nic->sqs_count = (nic->sqs_count / MAX_CMP_QUEUES_PER_QS) - 1;
655 	} else {
656 		nic->sqs_count = 0;
657 	}
658 
659 	nic->qs->rq_cnt = min_t(u32, channel->rx_count, MAX_RCV_QUEUES_PER_QS);
660 	nic->qs->sq_cnt = min_t(u32, channel->tx_count, MAX_SND_QUEUES_PER_QS);
661 	nic->qs->cq_cnt = max(nic->qs->rq_cnt, nic->qs->sq_cnt);
662 
663 	nic->rx_queues = channel->rx_count;
664 	nic->tx_queues = channel->tx_count;
665 	err = nicvf_set_real_num_queues(dev, nic->tx_queues, nic->rx_queues);
666 	if (err)
667 		return err;
668 
669 	if (if_up)
670 		nicvf_open(dev);
671 
672 	netdev_info(dev, "Setting num Tx rings to %d, Rx rings to %d success\n",
673 		    nic->tx_queues, nic->rx_queues);
674 
675 	return err;
676 }
677 
678 static const struct ethtool_ops nicvf_ethtool_ops = {
679 	.get_settings		= nicvf_get_settings,
680 	.get_link		= nicvf_get_link,
681 	.get_drvinfo		= nicvf_get_drvinfo,
682 	.get_msglevel		= nicvf_get_msglevel,
683 	.set_msglevel		= nicvf_set_msglevel,
684 	.get_strings		= nicvf_get_strings,
685 	.get_sset_count		= nicvf_get_sset_count,
686 	.get_ethtool_stats	= nicvf_get_ethtool_stats,
687 	.get_regs_len		= nicvf_get_regs_len,
688 	.get_regs		= nicvf_get_regs,
689 	.get_coalesce		= nicvf_get_coalesce,
690 	.get_ringparam		= nicvf_get_ringparam,
691 	.get_rxnfc		= nicvf_get_rxnfc,
692 	.set_rxnfc		= nicvf_set_rxnfc,
693 	.get_rxfh_key_size	= nicvf_get_rxfh_key_size,
694 	.get_rxfh_indir_size	= nicvf_get_rxfh_indir_size,
695 	.get_rxfh		= nicvf_get_rxfh,
696 	.set_rxfh		= nicvf_set_rxfh,
697 	.get_channels		= nicvf_get_channels,
698 	.set_channels		= nicvf_set_channels,
699 	.get_ts_info		= ethtool_op_get_ts_info,
700 };
701 
nicvf_set_ethtool_ops(struct net_device * netdev)702 void nicvf_set_ethtool_ops(struct net_device *netdev)
703 {
704 	netdev->ethtool_ops = &nicvf_ethtool_ops;
705 }
706