blob: c6c23033e6ebb28178359cfcdf0f3deea20aad88 [file] [log] [blame]
Sunil Goutham4863dea2015-05-26 19:20:15 -07001/*
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#include <linux/module.h>
10#include <linux/interrupt.h>
11#include <linux/pci.h>
12#include <linux/netdevice.h>
Sunil Gouthamaa2e2592015-08-30 12:29:13 +030013#include <linux/if_vlan.h>
Sunil Goutham4863dea2015-05-26 19:20:15 -070014#include <linux/etherdevice.h>
15#include <linux/ethtool.h>
16#include <linux/log2.h>
17#include <linux/prefetch.h>
18#include <linux/irq.h>
19
20#include "nic_reg.h"
21#include "nic.h"
22#include "nicvf_queues.h"
23#include "thunder_bgx.h"
24
25#define DRV_NAME "thunder-nicvf"
26#define DRV_VERSION "1.0"
27
28/* Supported devices */
29static const struct pci_device_id nicvf_id_table[] = {
30 { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
31 PCI_DEVICE_ID_THUNDER_NIC_VF,
Sunil Gouthamf7ff0ae2016-08-12 16:51:25 +053032 PCI_VENDOR_ID_CAVIUM,
33 PCI_SUBSYS_DEVID_88XX_NIC_VF) },
Sunil Goutham4863dea2015-05-26 19:20:15 -070034 { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
35 PCI_DEVICE_ID_THUNDER_PASS1_NIC_VF,
Sunil Gouthamf7ff0ae2016-08-12 16:51:25 +053036 PCI_VENDOR_ID_CAVIUM,
37 PCI_SUBSYS_DEVID_88XX_PASS1_NIC_VF) },
38 { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
39 PCI_DEVICE_ID_THUNDER_NIC_VF,
40 PCI_VENDOR_ID_CAVIUM,
41 PCI_SUBSYS_DEVID_81XX_NIC_VF) },
42 { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
43 PCI_DEVICE_ID_THUNDER_NIC_VF,
44 PCI_VENDOR_ID_CAVIUM,
45 PCI_SUBSYS_DEVID_83XX_NIC_VF) },
Sunil Goutham4863dea2015-05-26 19:20:15 -070046 { 0, } /* end of table */
47};
48
49MODULE_AUTHOR("Sunil Goutham");
50MODULE_DESCRIPTION("Cavium Thunder NIC Virtual Function Driver");
51MODULE_LICENSE("GPL v2");
52MODULE_VERSION(DRV_VERSION);
53MODULE_DEVICE_TABLE(pci, nicvf_id_table);
54
55static int debug = 0x00;
56module_param(debug, int, 0644);
57MODULE_PARM_DESC(debug, "Debug message level bitmap");
58
59static int cpi_alg = CPI_ALG_NONE;
60module_param(cpi_alg, int, S_IRUGO);
61MODULE_PARM_DESC(cpi_alg,
62 "PFC algorithm (0=none, 1=VLAN, 2=VLAN16, 3=IP Diffserv)");
63
Sunil Goutham92dc8762015-08-30 12:29:15 +030064static inline u8 nicvf_netdev_qidx(struct nicvf *nic, u8 qidx)
65{
66 if (nic->sqs_mode)
67 return qidx + ((nic->sqs_id + 1) * MAX_CMP_QUEUES_PER_QS);
68 else
69 return qidx;
70}
71
Sunil Goutham4863dea2015-05-26 19:20:15 -070072/* The Cavium ThunderX network controller can *only* be found in SoCs
73 * containing the ThunderX ARM64 CPU implementation. All accesses to the device
74 * registers on this platform are implicitly strongly ordered with respect
75 * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
76 * with no memory barriers in this driver. The readq()/writeq() functions add
77 * explicit ordering operation which in this case are redundant, and only
78 * add overhead.
79 */
80
81/* Register read/write APIs */
82void nicvf_reg_write(struct nicvf *nic, u64 offset, u64 val)
83{
84 writeq_relaxed(val, nic->reg_base + offset);
85}
86
87u64 nicvf_reg_read(struct nicvf *nic, u64 offset)
88{
89 return readq_relaxed(nic->reg_base + offset);
90}
91
92void nicvf_queue_reg_write(struct nicvf *nic, u64 offset,
93 u64 qidx, u64 val)
94{
95 void __iomem *addr = nic->reg_base + offset;
96
97 writeq_relaxed(val, addr + (qidx << NIC_Q_NUM_SHIFT));
98}
99
100u64 nicvf_queue_reg_read(struct nicvf *nic, u64 offset, u64 qidx)
101{
102 void __iomem *addr = nic->reg_base + offset;
103
104 return readq_relaxed(addr + (qidx << NIC_Q_NUM_SHIFT));
105}
106
107/* VF -> PF mailbox communication */
Aleksey Makarov2cd2a192015-06-02 11:00:20 -0700108static void nicvf_write_to_mbx(struct nicvf *nic, union nic_mbx *mbx)
109{
110 u64 *msg = (u64 *)mbx;
111
112 nicvf_reg_write(nic, NIC_VF_PF_MAILBOX_0_1 + 0, msg[0]);
113 nicvf_reg_write(nic, NIC_VF_PF_MAILBOX_0_1 + 8, msg[1]);
114}
115
Sunil Goutham4863dea2015-05-26 19:20:15 -0700116int nicvf_send_msg_to_pf(struct nicvf *nic, union nic_mbx *mbx)
117{
118 int timeout = NIC_MBOX_MSG_TIMEOUT;
119 int sleep = 10;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700120
121 nic->pf_acked = false;
122 nic->pf_nacked = false;
123
Aleksey Makarov2cd2a192015-06-02 11:00:20 -0700124 nicvf_write_to_mbx(nic, mbx);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700125
126 /* Wait for previous message to be acked, timeout 2sec */
127 while (!nic->pf_acked) {
Radoslaw Biernackiecae29c2016-08-12 16:51:38 +0530128 if (nic->pf_nacked) {
129 netdev_err(nic->netdev,
130 "PF NACK to mbox msg 0x%02x from VF%d\n",
131 (mbx->msg.msg & 0xFF), nic->vf_id);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700132 return -EINVAL;
Radoslaw Biernackiecae29c2016-08-12 16:51:38 +0530133 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700134 msleep(sleep);
135 if (nic->pf_acked)
136 break;
137 timeout -= sleep;
138 if (!timeout) {
139 netdev_err(nic->netdev,
Radoslaw Biernackiecae29c2016-08-12 16:51:38 +0530140 "PF didn't ACK to mbox msg 0x%02x from VF%d\n",
Sunil Goutham4863dea2015-05-26 19:20:15 -0700141 (mbx->msg.msg & 0xFF), nic->vf_id);
142 return -EBUSY;
143 }
144 }
145 return 0;
146}
147
148/* Checks if VF is able to comminicate with PF
149* and also gets the VNIC number this VF is associated to.
150*/
151static int nicvf_check_pf_ready(struct nicvf *nic)
152{
Aleksey Makarov2cd2a192015-06-02 11:00:20 -0700153 union nic_mbx mbx = {};
154
155 mbx.msg.msg = NIC_MBOX_MSG_READY;
Sunil Goutham6051cba2015-08-30 12:29:11 +0300156 if (nicvf_send_msg_to_pf(nic, &mbx)) {
157 netdev_err(nic->netdev,
158 "PF didn't respond to READY msg\n");
159 return 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700160 }
Sunil Goutham6051cba2015-08-30 12:29:11 +0300161
Sunil Goutham4863dea2015-05-26 19:20:15 -0700162 return 1;
163}
164
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700165static void nicvf_read_bgx_stats(struct nicvf *nic, struct bgx_stats_msg *bgx)
166{
167 if (bgx->rx)
168 nic->bgx_stats.rx_stats[bgx->idx] = bgx->stats;
169 else
170 nic->bgx_stats.tx_stats[bgx->idx] = bgx->stats;
171}
172
Sunil Goutham4863dea2015-05-26 19:20:15 -0700173static void nicvf_handle_mbx_intr(struct nicvf *nic)
174{
175 union nic_mbx mbx = {};
176 u64 *mbx_data;
177 u64 mbx_addr;
178 int i;
179
180 mbx_addr = NIC_VF_PF_MAILBOX_0_1;
181 mbx_data = (u64 *)&mbx;
182
183 for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
184 *mbx_data = nicvf_reg_read(nic, mbx_addr);
185 mbx_data++;
186 mbx_addr += sizeof(u64);
187 }
188
189 netdev_dbg(nic->netdev, "Mbox message: msg: 0x%x\n", mbx.msg.msg);
190 switch (mbx.msg.msg) {
191 case NIC_MBOX_MSG_READY:
Sunil Goutham6051cba2015-08-30 12:29:11 +0300192 nic->pf_acked = true;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700193 nic->vf_id = mbx.nic_cfg.vf_id & 0x7F;
194 nic->tns_mode = mbx.nic_cfg.tns_mode & 0x7F;
195 nic->node = mbx.nic_cfg.node_id;
Pavel Fedinbd049a92015-06-23 17:51:06 +0300196 if (!nic->set_mac_pending)
197 ether_addr_copy(nic->netdev->dev_addr,
198 mbx.nic_cfg.mac_addr);
Sunil Goutham92dc8762015-08-30 12:29:15 +0300199 nic->sqs_mode = mbx.nic_cfg.sqs_mode;
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300200 nic->loopback_supported = mbx.nic_cfg.loopback_supported;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700201 nic->link_up = false;
202 nic->duplex = 0;
203 nic->speed = 0;
204 break;
205 case NIC_MBOX_MSG_ACK:
206 nic->pf_acked = true;
207 break;
208 case NIC_MBOX_MSG_NACK:
209 nic->pf_nacked = true;
210 break;
211 case NIC_MBOX_MSG_RSS_SIZE:
212 nic->rss_info.rss_size = mbx.rss_size.ind_tbl_size;
213 nic->pf_acked = true;
214 break;
215 case NIC_MBOX_MSG_BGX_STATS:
216 nicvf_read_bgx_stats(nic, &mbx.bgx_stats);
217 nic->pf_acked = true;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700218 break;
219 case NIC_MBOX_MSG_BGX_LINK_CHANGE:
220 nic->pf_acked = true;
221 nic->link_up = mbx.link_status.link_up;
222 nic->duplex = mbx.link_status.duplex;
223 nic->speed = mbx.link_status.speed;
Thanneeru Srinivasulu1cc70252016-11-24 14:48:01 +0530224 nic->mac_type = mbx.link_status.mac_type;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700225 if (nic->link_up) {
226 netdev_info(nic->netdev, "%s: Link is Up %d Mbps %s\n",
227 nic->netdev->name, nic->speed,
228 nic->duplex == DUPLEX_FULL ?
229 "Full duplex" : "Half duplex");
230 netif_carrier_on(nic->netdev);
Sunil Gouthamb49087d2015-07-29 16:49:44 +0300231 netif_tx_start_all_queues(nic->netdev);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700232 } else {
233 netdev_info(nic->netdev, "%s: Link is Down\n",
234 nic->netdev->name);
235 netif_carrier_off(nic->netdev);
236 netif_tx_stop_all_queues(nic->netdev);
237 }
238 break;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300239 case NIC_MBOX_MSG_ALLOC_SQS:
240 nic->sqs_count = mbx.sqs_alloc.qs_count;
241 nic->pf_acked = true;
242 break;
243 case NIC_MBOX_MSG_SNICVF_PTR:
244 /* Primary VF: make note of secondary VF's pointer
245 * to be used while packet transmission.
246 */
247 nic->snicvf[mbx.nicvf.sqs_id] =
248 (struct nicvf *)mbx.nicvf.nicvf;
249 nic->pf_acked = true;
250 break;
251 case NIC_MBOX_MSG_PNICVF_PTR:
252 /* Secondary VF/Qset: make note of primary VF's pointer
253 * to be used while packet reception, to handover packet
254 * to primary VF's netdev.
255 */
256 nic->pnicvf = (struct nicvf *)mbx.nicvf.nicvf;
257 nic->pf_acked = true;
258 break;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700259 default:
260 netdev_err(nic->netdev,
261 "Invalid message from PF, msg 0x%x\n", mbx.msg.msg);
262 break;
263 }
264 nicvf_clear_intr(nic, NICVF_INTR_MBOX, 0);
265}
266
267static int nicvf_hw_set_mac_addr(struct nicvf *nic, struct net_device *netdev)
268{
269 union nic_mbx mbx = {};
Sunil Goutham4863dea2015-05-26 19:20:15 -0700270
271 mbx.mac.msg = NIC_MBOX_MSG_SET_MAC;
272 mbx.mac.vf_id = nic->vf_id;
Aleksey Makarove610cb32015-06-02 11:00:21 -0700273 ether_addr_copy(mbx.mac.mac_addr, netdev->dev_addr);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700274
275 return nicvf_send_msg_to_pf(nic, &mbx);
276}
277
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700278static void nicvf_config_cpi(struct nicvf *nic)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700279{
280 union nic_mbx mbx = {};
281
282 mbx.cpi_cfg.msg = NIC_MBOX_MSG_CPI_CFG;
283 mbx.cpi_cfg.vf_id = nic->vf_id;
284 mbx.cpi_cfg.cpi_alg = nic->cpi_alg;
285 mbx.cpi_cfg.rq_cnt = nic->qs->rq_cnt;
286
287 nicvf_send_msg_to_pf(nic, &mbx);
288}
289
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700290static void nicvf_get_rss_size(struct nicvf *nic)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700291{
292 union nic_mbx mbx = {};
293
294 mbx.rss_size.msg = NIC_MBOX_MSG_RSS_SIZE;
295 mbx.rss_size.vf_id = nic->vf_id;
296 nicvf_send_msg_to_pf(nic, &mbx);
297}
298
299void nicvf_config_rss(struct nicvf *nic)
300{
301 union nic_mbx mbx = {};
302 struct nicvf_rss_info *rss = &nic->rss_info;
303 int ind_tbl_len = rss->rss_size;
304 int i, nextq = 0;
305
306 mbx.rss_cfg.vf_id = nic->vf_id;
307 mbx.rss_cfg.hash_bits = rss->hash_bits;
308 while (ind_tbl_len) {
309 mbx.rss_cfg.tbl_offset = nextq;
310 mbx.rss_cfg.tbl_len = min(ind_tbl_len,
311 RSS_IND_TBL_LEN_PER_MBX_MSG);
312 mbx.rss_cfg.msg = mbx.rss_cfg.tbl_offset ?
313 NIC_MBOX_MSG_RSS_CFG_CONT : NIC_MBOX_MSG_RSS_CFG;
314
315 for (i = 0; i < mbx.rss_cfg.tbl_len; i++)
316 mbx.rss_cfg.ind_tbl[i] = rss->ind_tbl[nextq++];
317
318 nicvf_send_msg_to_pf(nic, &mbx);
319
320 ind_tbl_len -= mbx.rss_cfg.tbl_len;
321 }
322}
323
324void nicvf_set_rss_key(struct nicvf *nic)
325{
326 struct nicvf_rss_info *rss = &nic->rss_info;
327 u64 key_addr = NIC_VNIC_RSS_KEY_0_4;
328 int idx;
329
330 for (idx = 0; idx < RSS_HASH_KEY_SIZE; idx++) {
331 nicvf_reg_write(nic, key_addr, rss->key[idx]);
332 key_addr += sizeof(u64);
333 }
334}
335
336static int nicvf_rss_init(struct nicvf *nic)
337{
338 struct nicvf_rss_info *rss = &nic->rss_info;
339 int idx;
340
341 nicvf_get_rss_size(nic);
342
Sunil Goutham38bb5d42015-08-30 12:29:12 +0300343 if (cpi_alg != CPI_ALG_NONE) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700344 rss->enable = false;
345 rss->hash_bits = 0;
346 return 0;
347 }
348
349 rss->enable = true;
350
Sunil Goutham0052c922016-08-12 16:51:43 +0530351 netdev_rss_key_fill(rss->key, RSS_HASH_KEY_SIZE * sizeof(u64));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700352 nicvf_set_rss_key(nic);
353
354 rss->cfg = RSS_IP_HASH_ENA | RSS_TCP_HASH_ENA | RSS_UDP_HASH_ENA;
355 nicvf_reg_write(nic, NIC_VNIC_RSS_CFG, rss->cfg);
356
357 rss->hash_bits = ilog2(rounddown_pow_of_two(rss->rss_size));
358
359 for (idx = 0; idx < rss->rss_size; idx++)
360 rss->ind_tbl[idx] = ethtool_rxfh_indir_default(idx,
Sunil Goutham92dc8762015-08-30 12:29:15 +0300361 nic->rx_queues);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700362 nicvf_config_rss(nic);
363 return 1;
364}
365
Sunil Goutham92dc8762015-08-30 12:29:15 +0300366/* Request PF to allocate additional Qsets */
367static void nicvf_request_sqs(struct nicvf *nic)
368{
369 union nic_mbx mbx = {};
370 int sqs;
371 int sqs_count = nic->sqs_count;
372 int rx_queues = 0, tx_queues = 0;
373
374 /* Only primary VF should request */
375 if (nic->sqs_mode || !nic->sqs_count)
376 return;
377
378 mbx.sqs_alloc.msg = NIC_MBOX_MSG_ALLOC_SQS;
379 mbx.sqs_alloc.vf_id = nic->vf_id;
380 mbx.sqs_alloc.qs_count = nic->sqs_count;
381 if (nicvf_send_msg_to_pf(nic, &mbx)) {
382 /* No response from PF */
383 nic->sqs_count = 0;
384 return;
385 }
386
387 /* Return if no Secondary Qsets available */
388 if (!nic->sqs_count)
389 return;
390
391 if (nic->rx_queues > MAX_RCV_QUEUES_PER_QS)
392 rx_queues = nic->rx_queues - MAX_RCV_QUEUES_PER_QS;
393 if (nic->tx_queues > MAX_SND_QUEUES_PER_QS)
394 tx_queues = nic->tx_queues - MAX_SND_QUEUES_PER_QS;
395
396 /* Set no of Rx/Tx queues in each of the SQsets */
397 for (sqs = 0; sqs < nic->sqs_count; sqs++) {
398 mbx.nicvf.msg = NIC_MBOX_MSG_SNICVF_PTR;
399 mbx.nicvf.vf_id = nic->vf_id;
400 mbx.nicvf.sqs_id = sqs;
401 nicvf_send_msg_to_pf(nic, &mbx);
402
403 nic->snicvf[sqs]->sqs_id = sqs;
404 if (rx_queues > MAX_RCV_QUEUES_PER_QS) {
405 nic->snicvf[sqs]->qs->rq_cnt = MAX_RCV_QUEUES_PER_QS;
406 rx_queues -= MAX_RCV_QUEUES_PER_QS;
407 } else {
408 nic->snicvf[sqs]->qs->rq_cnt = rx_queues;
409 rx_queues = 0;
410 }
411
412 if (tx_queues > MAX_SND_QUEUES_PER_QS) {
413 nic->snicvf[sqs]->qs->sq_cnt = MAX_SND_QUEUES_PER_QS;
414 tx_queues -= MAX_SND_QUEUES_PER_QS;
415 } else {
416 nic->snicvf[sqs]->qs->sq_cnt = tx_queues;
417 tx_queues = 0;
418 }
419
420 nic->snicvf[sqs]->qs->cq_cnt =
421 max(nic->snicvf[sqs]->qs->rq_cnt, nic->snicvf[sqs]->qs->sq_cnt);
422
423 /* Initialize secondary Qset's queues and its interrupts */
424 nicvf_open(nic->snicvf[sqs]->netdev);
425 }
426
427 /* Update stack with actual Rx/Tx queue count allocated */
428 if (sqs_count != nic->sqs_count)
429 nicvf_set_real_num_queues(nic->netdev,
430 nic->tx_queues, nic->rx_queues);
431}
432
433/* Send this Qset's nicvf pointer to PF.
434 * PF inturn sends primary VF's nicvf struct to secondary Qsets/VFs
435 * so that packets received by these Qsets can use primary VF's netdev
436 */
437static void nicvf_send_vf_struct(struct nicvf *nic)
438{
439 union nic_mbx mbx = {};
440
441 mbx.nicvf.msg = NIC_MBOX_MSG_NICVF_PTR;
442 mbx.nicvf.sqs_mode = nic->sqs_mode;
443 mbx.nicvf.nicvf = (u64)nic;
444 nicvf_send_msg_to_pf(nic, &mbx);
445}
446
447static void nicvf_get_primary_vf_struct(struct nicvf *nic)
448{
449 union nic_mbx mbx = {};
450
451 mbx.nicvf.msg = NIC_MBOX_MSG_PNICVF_PTR;
452 nicvf_send_msg_to_pf(nic, &mbx);
453}
454
Sunil Goutham4863dea2015-05-26 19:20:15 -0700455int nicvf_set_real_num_queues(struct net_device *netdev,
456 int tx_queues, int rx_queues)
457{
458 int err = 0;
459
460 err = netif_set_real_num_tx_queues(netdev, tx_queues);
461 if (err) {
462 netdev_err(netdev,
463 "Failed to set no of Tx queues: %d\n", tx_queues);
464 return err;
465 }
466
467 err = netif_set_real_num_rx_queues(netdev, rx_queues);
468 if (err)
469 netdev_err(netdev,
470 "Failed to set no of Rx queues: %d\n", rx_queues);
471 return err;
472}
473
474static int nicvf_init_resources(struct nicvf *nic)
475{
476 int err;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700477
478 /* Enable Qset */
479 nicvf_qset_config(nic, true);
480
481 /* Initialize queues and HW for data transfer */
482 err = nicvf_config_data_transfer(nic, true);
483 if (err) {
484 netdev_err(nic->netdev,
485 "Failed to alloc/config VF's QSet resources\n");
486 return err;
487 }
488
Sunil Goutham4863dea2015-05-26 19:20:15 -0700489 return 0;
490}
491
492static void nicvf_snd_pkt_handler(struct net_device *netdev,
Sunil Gouthamc43548d2016-08-12 16:51:41 +0530493 struct cqe_send_t *cqe_tx,
Sunil Goutham2c204c22016-09-23 14:42:28 +0530494 int cqe_type, int budget,
495 unsigned int *tx_pkts, unsigned int *tx_bytes)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700496{
497 struct sk_buff *skb = NULL;
498 struct nicvf *nic = netdev_priv(netdev);
499 struct snd_queue *sq;
500 struct sq_hdr_subdesc *hdr;
Sunil Goutham7ceb8a12016-08-30 11:36:27 +0530501 struct sq_hdr_subdesc *tso_sqe;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700502
503 sq = &nic->qs->sq[cqe_tx->sq_idx];
504
505 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, cqe_tx->sqe_ptr);
506 if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER)
507 return;
508
509 netdev_dbg(nic->netdev,
510 "%s Qset #%d SQ #%d SQ ptr #%d subdesc count %d\n",
511 __func__, cqe_tx->sq_qs, cqe_tx->sq_idx,
512 cqe_tx->sqe_ptr, hdr->subdesc_cnt);
513
Sunil Goutham964cb692016-11-15 17:38:16 +0530514 nicvf_check_cqe_tx_errs(nic, cqe_tx);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700515 skb = (struct sk_buff *)sq->skbuff[cqe_tx->sqe_ptr];
Sunil Goutham4863dea2015-05-26 19:20:15 -0700516 if (skb) {
Sunil Goutham7ceb8a12016-08-30 11:36:27 +0530517 /* Check for dummy descriptor used for HW TSO offload on 88xx */
518 if (hdr->dont_send) {
519 /* Get actual TSO descriptors and free them */
520 tso_sqe =
521 (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, hdr->rsvd2);
522 nicvf_put_sq_desc(sq, tso_sqe->subdesc_cnt + 1);
523 }
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530524 nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700525 prefetch(skb);
Sunil Goutham2c204c22016-09-23 14:42:28 +0530526 (*tx_pkts)++;
527 *tx_bytes += skb->len;
Sunil Gouthamc43548d2016-08-12 16:51:41 +0530528 napi_consume_skb(skb, budget);
Sunil Goutham143ceb02015-07-29 16:49:37 +0300529 sq->skbuff[cqe_tx->sqe_ptr] = (u64)NULL;
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530530 } else {
Sunil Goutham7ceb8a12016-08-30 11:36:27 +0530531 /* In case of SW TSO on 88xx, only last segment will have
532 * a SKB attached, so just free SQEs here.
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530533 */
534 if (!nic->hw_tso)
535 nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700536 }
537}
538
Sunil Goutham38bb5d42015-08-30 12:29:12 +0300539static inline void nicvf_set_rxhash(struct net_device *netdev,
540 struct cqe_rx_t *cqe_rx,
541 struct sk_buff *skb)
542{
543 u8 hash_type;
544 u32 hash;
545
546 if (!(netdev->features & NETIF_F_RXHASH))
547 return;
548
549 switch (cqe_rx->rss_alg) {
550 case RSS_ALG_TCP_IP:
551 case RSS_ALG_UDP_IP:
552 hash_type = PKT_HASH_TYPE_L4;
553 hash = cqe_rx->rss_tag;
554 break;
555 case RSS_ALG_IP:
556 hash_type = PKT_HASH_TYPE_L3;
557 hash = cqe_rx->rss_tag;
558 break;
559 default:
560 hash_type = PKT_HASH_TYPE_NONE;
561 hash = 0;
562 }
563
564 skb_set_hash(skb, hash, hash_type);
565}
566
Sunil Goutham4863dea2015-05-26 19:20:15 -0700567static void nicvf_rcv_pkt_handler(struct net_device *netdev,
568 struct napi_struct *napi,
Sunil Gouthamad2eceb2016-02-16 16:29:51 +0530569 struct cqe_rx_t *cqe_rx)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700570{
571 struct sk_buff *skb;
572 struct nicvf *nic = netdev_priv(netdev);
573 int err = 0;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300574 int rq_idx;
575
576 rq_idx = nicvf_netdev_qidx(nic, cqe_rx->rq_idx);
577
578 if (nic->sqs_mode) {
579 /* Use primary VF's 'nicvf' struct */
580 nic = nic->pnicvf;
581 netdev = nic->netdev;
582 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700583
584 /* Check for errors */
Sunil Gouthamad2eceb2016-02-16 16:29:51 +0530585 err = nicvf_check_cqe_rx_errs(nic, cqe_rx);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700586 if (err && !cqe_rx->rb_cnt)
587 return;
588
589 skb = nicvf_get_rcv_skb(nic, cqe_rx);
590 if (!skb) {
591 netdev_dbg(nic->netdev, "Packet not received\n");
592 return;
593 }
594
595 if (netif_msg_pktdata(nic)) {
596 netdev_info(nic->netdev, "%s: skb 0x%p, len=%d\n", netdev->name,
597 skb, skb->len);
598 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
599 skb->data, skb->len, true);
600 }
601
Sunil Gouthama2dc5de2015-08-30 12:29:10 +0300602 /* If error packet, drop it here */
603 if (err) {
604 dev_kfree_skb_any(skb);
605 return;
606 }
607
Sunil Goutham38bb5d42015-08-30 12:29:12 +0300608 nicvf_set_rxhash(netdev, cqe_rx, skb);
609
Sunil Goutham92dc8762015-08-30 12:29:15 +0300610 skb_record_rx_queue(skb, rq_idx);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700611 if (netdev->hw_features & NETIF_F_RXCSUM) {
612 /* HW by default verifies TCP/UDP/SCTP checksums */
613 skb->ip_summed = CHECKSUM_UNNECESSARY;
614 } else {
615 skb_checksum_none_assert(skb);
616 }
617
618 skb->protocol = eth_type_trans(skb, netdev);
619
Sunil Gouthamaa2e2592015-08-30 12:29:13 +0300620 /* Check for stripped VLAN */
621 if (cqe_rx->vlan_found && cqe_rx->vlan_stripped)
622 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
623 ntohs((__force __be16)cqe_rx->vlan_tci));
624
Sunil Goutham4863dea2015-05-26 19:20:15 -0700625 if (napi && (netdev->features & NETIF_F_GRO))
626 napi_gro_receive(napi, skb);
627 else
628 netif_receive_skb(skb);
629}
630
631static int nicvf_cq_intr_handler(struct net_device *netdev, u8 cq_idx,
632 struct napi_struct *napi, int budget)
633{
Sunil Goutham74840b82015-07-29 16:49:42 +0300634 int processed_cqe, work_done = 0, tx_done = 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700635 int cqe_count, cqe_head;
636 struct nicvf *nic = netdev_priv(netdev);
637 struct queue_set *qs = nic->qs;
638 struct cmp_queue *cq = &qs->cq[cq_idx];
639 struct cqe_rx_t *cq_desc;
Sunil Goutham74840b82015-07-29 16:49:42 +0300640 struct netdev_queue *txq;
Sunil Goutham2c204c22016-09-23 14:42:28 +0530641 unsigned int tx_pkts = 0, tx_bytes = 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700642
643 spin_lock_bh(&cq->lock);
644loop:
645 processed_cqe = 0;
646 /* Get no of valid CQ entries to process */
647 cqe_count = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS, cq_idx);
648 cqe_count &= CQ_CQE_COUNT;
649 if (!cqe_count)
650 goto done;
651
652 /* Get head of the valid CQ entries */
653 cqe_head = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD, cq_idx) >> 9;
654 cqe_head &= 0xFFFF;
655
Sunil Goutham74840b82015-07-29 16:49:42 +0300656 netdev_dbg(nic->netdev, "%s CQ%d cqe_count %d cqe_head %d\n",
657 __func__, cq_idx, cqe_count, cqe_head);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700658 while (processed_cqe < cqe_count) {
659 /* Get the CQ descriptor */
660 cq_desc = (struct cqe_rx_t *)GET_CQ_DESC(cq, cqe_head);
661 cqe_head++;
662 cqe_head &= (cq->dmem.q_len - 1);
663 /* Initiate prefetch for next descriptor */
664 prefetch((struct cqe_rx_t *)GET_CQ_DESC(cq, cqe_head));
665
666 if ((work_done >= budget) && napi &&
667 (cq_desc->cqe_type != CQE_TYPE_SEND)) {
668 break;
669 }
670
Sunil Goutham74840b82015-07-29 16:49:42 +0300671 netdev_dbg(nic->netdev, "CQ%d cq_desc->cqe_type %d\n",
672 cq_idx, cq_desc->cqe_type);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700673 switch (cq_desc->cqe_type) {
674 case CQE_TYPE_RX:
Sunil Gouthamad2eceb2016-02-16 16:29:51 +0530675 nicvf_rcv_pkt_handler(netdev, napi, cq_desc);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700676 work_done++;
677 break;
678 case CQE_TYPE_SEND:
Sunil Goutham964cb692016-11-15 17:38:16 +0530679 nicvf_snd_pkt_handler(netdev,
Sunil Gouthamc43548d2016-08-12 16:51:41 +0530680 (void *)cq_desc, CQE_TYPE_SEND,
Sunil Goutham2c204c22016-09-23 14:42:28 +0530681 budget, &tx_pkts, &tx_bytes);
Sunil Goutham74840b82015-07-29 16:49:42 +0300682 tx_done++;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700683 break;
684 case CQE_TYPE_INVALID:
685 case CQE_TYPE_RX_SPLIT:
686 case CQE_TYPE_RX_TCP:
687 case CQE_TYPE_SEND_PTP:
688 /* Ignore for now */
689 break;
690 }
691 processed_cqe++;
692 }
Sunil Goutham74840b82015-07-29 16:49:42 +0300693 netdev_dbg(nic->netdev,
694 "%s CQ%d processed_cqe %d work_done %d budget %d\n",
695 __func__, cq_idx, processed_cqe, work_done, budget);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700696
697 /* Ring doorbell to inform H/W to reuse processed CQEs */
698 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_DOOR,
699 cq_idx, processed_cqe);
700
701 if ((work_done < budget) && napi)
702 goto loop;
703
704done:
Sunil Goutham74840b82015-07-29 16:49:42 +0300705 /* Wakeup TXQ if its stopped earlier due to SQ full */
706 if (tx_done) {
Sunil Goutham92dc8762015-08-30 12:29:15 +0300707 netdev = nic->pnicvf->netdev;
708 txq = netdev_get_tx_queue(netdev,
709 nicvf_netdev_qidx(nic, cq_idx));
Sunil Goutham2c204c22016-09-23 14:42:28 +0530710 if (tx_pkts)
711 netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
712
Sunil Goutham92dc8762015-08-30 12:29:15 +0300713 nic = nic->pnicvf;
714 if (netif_tx_queue_stopped(txq) && netif_carrier_ok(netdev)) {
Sunil Gouthamb49087d2015-07-29 16:49:44 +0300715 netif_tx_start_queue(txq);
Sunil Goutham964cb692016-11-15 17:38:16 +0530716 this_cpu_inc(nic->drv_stats->txq_wake);
Sunil Goutham74840b82015-07-29 16:49:42 +0300717 if (netif_msg_tx_err(nic))
718 netdev_warn(netdev,
719 "%s: Transmit queue wakeup SQ%d\n",
720 netdev->name, cq_idx);
721 }
722 }
723
Sunil Goutham4863dea2015-05-26 19:20:15 -0700724 spin_unlock_bh(&cq->lock);
725 return work_done;
726}
727
728static int nicvf_poll(struct napi_struct *napi, int budget)
729{
730 u64 cq_head;
731 int work_done = 0;
732 struct net_device *netdev = napi->dev;
733 struct nicvf *nic = netdev_priv(netdev);
734 struct nicvf_cq_poll *cq;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700735
736 cq = container_of(napi, struct nicvf_cq_poll, napi);
737 work_done = nicvf_cq_intr_handler(netdev, cq->cq_idx, napi, budget);
738
Sunil Goutham4863dea2015-05-26 19:20:15 -0700739 if (work_done < budget) {
740 /* Slow packet rate, exit polling */
741 napi_complete(napi);
742 /* Re-enable interrupts */
743 cq_head = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD,
744 cq->cq_idx);
745 nicvf_clear_intr(nic, NICVF_INTR_CQ, cq->cq_idx);
746 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_HEAD,
747 cq->cq_idx, cq_head);
748 nicvf_enable_intr(nic, NICVF_INTR_CQ, cq->cq_idx);
749 }
750 return work_done;
751}
752
753/* Qset error interrupt handler
754 *
755 * As of now only CQ errors are handled
756 */
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700757static void nicvf_handle_qs_err(unsigned long data)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700758{
759 struct nicvf *nic = (struct nicvf *)data;
760 struct queue_set *qs = nic->qs;
761 int qidx;
762 u64 status;
763
764 netif_tx_disable(nic->netdev);
765
766 /* Check if it is CQ err */
767 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
768 status = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS,
769 qidx);
770 if (!(status & CQ_ERR_MASK))
771 continue;
772 /* Process already queued CQEs and reconfig CQ */
773 nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
774 nicvf_sq_disable(nic, qidx);
775 nicvf_cq_intr_handler(nic->netdev, qidx, NULL, 0);
776 nicvf_cmp_queue_config(nic, qs, qidx, true);
777 nicvf_sq_free_used_descs(nic->netdev, &qs->sq[qidx], qidx);
778 nicvf_sq_enable(nic, &qs->sq[qidx], qidx);
779
780 nicvf_enable_intr(nic, NICVF_INTR_CQ, qidx);
781 }
782
783 netif_tx_start_all_queues(nic->netdev);
784 /* Re-enable Qset error interrupt */
785 nicvf_enable_intr(nic, NICVF_INTR_QS_ERR, 0);
786}
787
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300788static void nicvf_dump_intr_status(struct nicvf *nic)
789{
790 if (netif_msg_intr(nic))
791 netdev_info(nic->netdev, "%s: interrupt status 0x%llx\n",
792 nic->netdev->name, nicvf_reg_read(nic, NIC_VF_INT));
793}
794
Sunil Goutham4863dea2015-05-26 19:20:15 -0700795static irqreturn_t nicvf_misc_intr_handler(int irq, void *nicvf_irq)
796{
797 struct nicvf *nic = (struct nicvf *)nicvf_irq;
798 u64 intr;
799
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300800 nicvf_dump_intr_status(nic);
801
Sunil Goutham4863dea2015-05-26 19:20:15 -0700802 intr = nicvf_reg_read(nic, NIC_VF_INT);
803 /* Check for spurious interrupt */
804 if (!(intr & NICVF_INTR_MBOX_MASK))
805 return IRQ_HANDLED;
806
807 nicvf_handle_mbx_intr(nic);
808
809 return IRQ_HANDLED;
810}
811
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300812static irqreturn_t nicvf_intr_handler(int irq, void *cq_irq)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700813{
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300814 struct nicvf_cq_poll *cq_poll = (struct nicvf_cq_poll *)cq_irq;
815 struct nicvf *nic = cq_poll->nicvf;
816 int qidx = cq_poll->cq_idx;
817
818 nicvf_dump_intr_status(nic);
819
820 /* Disable interrupts */
821 nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
822
823 /* Schedule NAPI */
Sunil Gouthamef0a4d82016-02-11 21:50:22 +0530824 napi_schedule_irqoff(&cq_poll->napi);
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300825
826 /* Clear interrupt */
827 nicvf_clear_intr(nic, NICVF_INTR_CQ, qidx);
828
829 return IRQ_HANDLED;
830}
831
832static irqreturn_t nicvf_rbdr_intr_handler(int irq, void *nicvf_irq)
833{
Sunil Goutham4863dea2015-05-26 19:20:15 -0700834 struct nicvf *nic = (struct nicvf *)nicvf_irq;
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300835 u8 qidx;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700836
Sunil Goutham4863dea2015-05-26 19:20:15 -0700837
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300838 nicvf_dump_intr_status(nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700839
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300840 /* Disable RBDR interrupt and schedule softirq */
841 for (qidx = 0; qidx < nic->qs->rbdr_cnt; qidx++) {
842 if (!nicvf_is_intr_enabled(nic, NICVF_INTR_RBDR, qidx))
Sunil Goutham4863dea2015-05-26 19:20:15 -0700843 continue;
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300844 nicvf_disable_intr(nic, NICVF_INTR_RBDR, qidx);
845 tasklet_hi_schedule(&nic->rbdr_task);
846 /* Clear interrupt */
847 nicvf_clear_intr(nic, NICVF_INTR_RBDR, qidx);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700848 }
849
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300850 return IRQ_HANDLED;
851}
Sunil Goutham4863dea2015-05-26 19:20:15 -0700852
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300853static irqreturn_t nicvf_qs_err_intr_handler(int irq, void *nicvf_irq)
854{
855 struct nicvf *nic = (struct nicvf *)nicvf_irq;
856
857 nicvf_dump_intr_status(nic);
858
859 /* Disable Qset err interrupt and schedule softirq */
860 nicvf_disable_intr(nic, NICVF_INTR_QS_ERR, 0);
861 tasklet_hi_schedule(&nic->qs_err_task);
862 nicvf_clear_intr(nic, NICVF_INTR_QS_ERR, 0);
863
Sunil Goutham4863dea2015-05-26 19:20:15 -0700864 return IRQ_HANDLED;
865}
866
867static int nicvf_enable_msix(struct nicvf *nic)
868{
869 int ret, vec;
870
871 nic->num_vec = NIC_VF_MSIX_VECTORS;
872
873 for (vec = 0; vec < nic->num_vec; vec++)
874 nic->msix_entries[vec].entry = vec;
875
876 ret = pci_enable_msix(nic->pdev, nic->msix_entries, nic->num_vec);
877 if (ret) {
878 netdev_err(nic->netdev,
879 "Req for #%d msix vectors failed\n", nic->num_vec);
880 return 0;
881 }
882 nic->msix_enabled = 1;
883 return 1;
884}
885
886static void nicvf_disable_msix(struct nicvf *nic)
887{
888 if (nic->msix_enabled) {
889 pci_disable_msix(nic->pdev);
890 nic->msix_enabled = 0;
891 nic->num_vec = 0;
892 }
893}
894
Sunil Gouthamfb4b7d92016-02-11 21:50:23 +0530895static void nicvf_set_irq_affinity(struct nicvf *nic)
896{
897 int vec, cpu;
898 int irqnum;
899
900 for (vec = 0; vec < nic->num_vec; vec++) {
901 if (!nic->irq_allocated[vec])
902 continue;
903
904 if (!zalloc_cpumask_var(&nic->affinity_mask[vec], GFP_KERNEL))
905 return;
906 /* CQ interrupts */
907 if (vec < NICVF_INTR_ID_SQ)
908 /* Leave CPU0 for RBDR and other interrupts */
909 cpu = nicvf_netdev_qidx(nic, vec) + 1;
910 else
911 cpu = 0;
912
913 cpumask_set_cpu(cpumask_local_spread(cpu, nic->node),
914 nic->affinity_mask[vec]);
915 irqnum = nic->msix_entries[vec].vector;
916 irq_set_affinity_hint(irqnum, nic->affinity_mask[vec]);
917 }
918}
919
Sunil Goutham4863dea2015-05-26 19:20:15 -0700920static int nicvf_register_interrupts(struct nicvf *nic)
921{
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300922 int irq, ret = 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700923 int vector;
924
925 for_each_cq_irq(irq)
Sunil Gouthame4126212016-08-12 16:51:36 +0530926 sprintf(nic->irq_name[irq], "%s-rxtx-%d",
927 nic->pnicvf->netdev->name,
928 nicvf_netdev_qidx(nic, irq));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700929
930 for_each_sq_irq(irq)
Sunil Gouthame4126212016-08-12 16:51:36 +0530931 sprintf(nic->irq_name[irq], "%s-sq-%d",
932 nic->pnicvf->netdev->name,
933 nicvf_netdev_qidx(nic, irq - NICVF_INTR_ID_SQ));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700934
935 for_each_rbdr_irq(irq)
Sunil Gouthame4126212016-08-12 16:51:36 +0530936 sprintf(nic->irq_name[irq], "%s-rbdr-%d",
937 nic->pnicvf->netdev->name,
938 nic->sqs_mode ? (nic->sqs_id + 1) : 0);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700939
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300940 /* Register CQ interrupts */
941 for (irq = 0; irq < nic->qs->cq_cnt; irq++) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700942 vector = nic->msix_entries[irq].vector;
943 ret = request_irq(vector, nicvf_intr_handler,
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300944 0, nic->irq_name[irq], nic->napi[irq]);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700945 if (ret)
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300946 goto err;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700947 nic->irq_allocated[irq] = true;
948 }
949
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300950 /* Register RBDR interrupt */
951 for (irq = NICVF_INTR_ID_RBDR;
952 irq < (NICVF_INTR_ID_RBDR + nic->qs->rbdr_cnt); irq++) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700953 vector = nic->msix_entries[irq].vector;
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300954 ret = request_irq(vector, nicvf_rbdr_intr_handler,
Sunil Goutham4863dea2015-05-26 19:20:15 -0700955 0, nic->irq_name[irq], nic);
956 if (ret)
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300957 goto err;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700958 nic->irq_allocated[irq] = true;
959 }
960
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300961 /* Register QS error interrupt */
Sunil Gouthame4126212016-08-12 16:51:36 +0530962 sprintf(nic->irq_name[NICVF_INTR_ID_QS_ERR], "%s-qset-err-%d",
963 nic->pnicvf->netdev->name,
964 nic->sqs_mode ? (nic->sqs_id + 1) : 0);
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300965 irq = NICVF_INTR_ID_QS_ERR;
966 ret = request_irq(nic->msix_entries[irq].vector,
967 nicvf_qs_err_intr_handler,
968 0, nic->irq_name[irq], nic);
Sunil Gouthamfb4b7d92016-02-11 21:50:23 +0530969 if (ret)
970 goto err;
971
972 nic->irq_allocated[irq] = true;
973
974 /* Set IRQ affinities */
975 nicvf_set_irq_affinity(nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700976
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300977err:
978 if (ret)
979 netdev_err(nic->netdev, "request_irq failed, vector %d\n", irq);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700980
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300981 return ret;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700982}
983
984static void nicvf_unregister_interrupts(struct nicvf *nic)
985{
986 int irq;
987
988 /* Free registered interrupts */
989 for (irq = 0; irq < nic->num_vec; irq++) {
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300990 if (!nic->irq_allocated[irq])
991 continue;
992
Sunil Gouthamfb4b7d92016-02-11 21:50:23 +0530993 irq_set_affinity_hint(nic->msix_entries[irq].vector, NULL);
994 free_cpumask_var(nic->affinity_mask[irq]);
995
Sunil Goutham39ad6ee2015-08-30 12:29:14 +0300996 if (irq < NICVF_INTR_ID_SQ)
997 free_irq(nic->msix_entries[irq].vector, nic->napi[irq]);
998 else
Sunil Goutham4863dea2015-05-26 19:20:15 -0700999 free_irq(nic->msix_entries[irq].vector, nic);
Sunil Goutham39ad6ee2015-08-30 12:29:14 +03001000
Sunil Goutham4863dea2015-05-26 19:20:15 -07001001 nic->irq_allocated[irq] = false;
1002 }
1003
1004 /* Disable MSI-X */
1005 nicvf_disable_msix(nic);
1006}
1007
1008/* Initialize MSIX vectors and register MISC interrupt.
1009 * Send READY message to PF to check if its alive
1010 */
1011static int nicvf_register_misc_interrupt(struct nicvf *nic)
1012{
1013 int ret = 0;
1014 int irq = NICVF_INTR_ID_MISC;
1015
1016 /* Return if mailbox interrupt is already registered */
1017 if (nic->msix_enabled)
1018 return 0;
1019
1020 /* Enable MSI-X */
1021 if (!nicvf_enable_msix(nic))
1022 return 1;
1023
1024 sprintf(nic->irq_name[irq], "%s Mbox", "NICVF");
1025 /* Register Misc interrupt */
1026 ret = request_irq(nic->msix_entries[irq].vector,
1027 nicvf_misc_intr_handler, 0, nic->irq_name[irq], nic);
1028
1029 if (ret)
1030 return ret;
1031 nic->irq_allocated[irq] = true;
1032
1033 /* Enable mailbox interrupt */
1034 nicvf_enable_intr(nic, NICVF_INTR_MBOX, 0);
1035
1036 /* Check if VF is able to communicate with PF */
1037 if (!nicvf_check_pf_ready(nic)) {
1038 nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
1039 nicvf_unregister_interrupts(nic);
1040 return 1;
1041 }
1042
1043 return 0;
1044}
1045
1046static netdev_tx_t nicvf_xmit(struct sk_buff *skb, struct net_device *netdev)
1047{
1048 struct nicvf *nic = netdev_priv(netdev);
1049 int qid = skb_get_queue_mapping(skb);
1050 struct netdev_queue *txq = netdev_get_tx_queue(netdev, qid);
1051
1052 /* Check for minimum packet length */
1053 if (skb->len <= ETH_HLEN) {
1054 dev_kfree_skb(skb);
1055 return NETDEV_TX_OK;
1056 }
1057
Sunil Gouthamb49087d2015-07-29 16:49:44 +03001058 if (!netif_tx_queue_stopped(txq) && !nicvf_sq_append_skb(nic, skb)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001059 netif_tx_stop_queue(txq);
Sunil Goutham964cb692016-11-15 17:38:16 +05301060 this_cpu_inc(nic->drv_stats->txq_stop);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001061 if (netif_msg_tx_err(nic))
1062 netdev_warn(netdev,
1063 "%s: Transmit ring full, stopping SQ%d\n",
1064 netdev->name, qid);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001065 return NETDEV_TX_BUSY;
1066 }
1067
1068 return NETDEV_TX_OK;
1069}
1070
Sunil Goutham39ad6ee2015-08-30 12:29:14 +03001071static inline void nicvf_free_cq_poll(struct nicvf *nic)
1072{
1073 struct nicvf_cq_poll *cq_poll;
1074 int qidx;
1075
1076 for (qidx = 0; qidx < nic->qs->cq_cnt; qidx++) {
1077 cq_poll = nic->napi[qidx];
1078 if (!cq_poll)
1079 continue;
1080 nic->napi[qidx] = NULL;
1081 kfree(cq_poll);
1082 }
1083}
1084
Sunil Goutham4863dea2015-05-26 19:20:15 -07001085int nicvf_stop(struct net_device *netdev)
1086{
1087 int irq, qidx;
1088 struct nicvf *nic = netdev_priv(netdev);
1089 struct queue_set *qs = nic->qs;
1090 struct nicvf_cq_poll *cq_poll = NULL;
1091 union nic_mbx mbx = {};
1092
1093 mbx.msg.msg = NIC_MBOX_MSG_SHUTDOWN;
1094 nicvf_send_msg_to_pf(nic, &mbx);
1095
1096 netif_carrier_off(netdev);
Sunil Goutham92dc8762015-08-30 12:29:15 +03001097 netif_tx_stop_all_queues(nic->netdev);
Sunil Goutham0b72a9a2015-12-02 15:36:16 +05301098 nic->link_up = false;
Sunil Goutham92dc8762015-08-30 12:29:15 +03001099
1100 /* Teardown secondary qsets first */
1101 if (!nic->sqs_mode) {
1102 for (qidx = 0; qidx < nic->sqs_count; qidx++) {
1103 if (!nic->snicvf[qidx])
1104 continue;
1105 nicvf_stop(nic->snicvf[qidx]->netdev);
1106 nic->snicvf[qidx] = NULL;
1107 }
1108 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001109
1110 /* Disable RBDR & QS error interrupts */
1111 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
1112 nicvf_disable_intr(nic, NICVF_INTR_RBDR, qidx);
1113 nicvf_clear_intr(nic, NICVF_INTR_RBDR, qidx);
1114 }
1115 nicvf_disable_intr(nic, NICVF_INTR_QS_ERR, 0);
1116 nicvf_clear_intr(nic, NICVF_INTR_QS_ERR, 0);
1117
1118 /* Wait for pending IRQ handlers to finish */
1119 for (irq = 0; irq < nic->num_vec; irq++)
1120 synchronize_irq(nic->msix_entries[irq].vector);
1121
1122 tasklet_kill(&nic->rbdr_task);
1123 tasklet_kill(&nic->qs_err_task);
1124 if (nic->rb_work_scheduled)
1125 cancel_delayed_work_sync(&nic->rbdr_work);
1126
1127 for (qidx = 0; qidx < nic->qs->cq_cnt; qidx++) {
1128 cq_poll = nic->napi[qidx];
1129 if (!cq_poll)
1130 continue;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001131 napi_synchronize(&cq_poll->napi);
1132 /* CQ intr is enabled while napi_complete,
1133 * so disable it now
1134 */
1135 nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
1136 nicvf_clear_intr(nic, NICVF_INTR_CQ, qidx);
1137 napi_disable(&cq_poll->napi);
1138 netif_napi_del(&cq_poll->napi);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001139 }
1140
Sunil Gouthamb49087d2015-07-29 16:49:44 +03001141 netif_tx_disable(netdev);
1142
Sunil Goutham2c204c22016-09-23 14:42:28 +05301143 for (qidx = 0; qidx < netdev->num_tx_queues; qidx++)
1144 netdev_tx_reset_queue(netdev_get_tx_queue(netdev, qidx));
1145
Sunil Goutham4863dea2015-05-26 19:20:15 -07001146 /* Free resources */
1147 nicvf_config_data_transfer(nic, false);
1148
1149 /* Disable HW Qset */
1150 nicvf_qset_config(nic, false);
1151
1152 /* disable mailbox interrupt */
1153 nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
1154
1155 nicvf_unregister_interrupts(nic);
1156
Sunil Goutham39ad6ee2015-08-30 12:29:14 +03001157 nicvf_free_cq_poll(nic);
1158
Sunil Goutham92dc8762015-08-30 12:29:15 +03001159 /* Clear multiqset info */
1160 nic->pnicvf = nic;
Sunil Goutham92dc8762015-08-30 12:29:15 +03001161
Sunil Goutham4863dea2015-05-26 19:20:15 -07001162 return 0;
1163}
1164
Sunil Goutham712c3182016-11-15 17:37:36 +05301165static int nicvf_update_hw_max_frs(struct nicvf *nic, int mtu)
1166{
1167 union nic_mbx mbx = {};
1168
1169 mbx.frs.msg = NIC_MBOX_MSG_SET_MAX_FRS;
1170 mbx.frs.max_frs = mtu;
1171 mbx.frs.vf_id = nic->vf_id;
1172
1173 return nicvf_send_msg_to_pf(nic, &mbx);
1174}
1175
Sunil Goutham4863dea2015-05-26 19:20:15 -07001176int nicvf_open(struct net_device *netdev)
1177{
Sunil Goutham964cb692016-11-15 17:38:16 +05301178 int cpu, err, qidx;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001179 struct nicvf *nic = netdev_priv(netdev);
1180 struct queue_set *qs = nic->qs;
1181 struct nicvf_cq_poll *cq_poll = NULL;
Sunil Gouthamc94acf82016-11-15 17:38:29 +05301182 union nic_mbx mbx = {};
Sunil Goutham4863dea2015-05-26 19:20:15 -07001183
1184 netif_carrier_off(netdev);
1185
1186 err = nicvf_register_misc_interrupt(nic);
1187 if (err)
1188 return err;
1189
1190 /* Register NAPI handler for processing CQEs */
1191 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
1192 cq_poll = kzalloc(sizeof(*cq_poll), GFP_KERNEL);
1193 if (!cq_poll) {
1194 err = -ENOMEM;
1195 goto napi_del;
1196 }
1197 cq_poll->cq_idx = qidx;
Sunil Goutham39ad6ee2015-08-30 12:29:14 +03001198 cq_poll->nicvf = nic;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001199 netif_napi_add(netdev, &cq_poll->napi, nicvf_poll,
1200 NAPI_POLL_WEIGHT);
1201 napi_enable(&cq_poll->napi);
1202 nic->napi[qidx] = cq_poll;
1203 }
1204
1205 /* Check if we got MAC address from PF or else generate a radom MAC */
Sunil Gouthama3a8ce42016-08-12 16:51:40 +05301206 if (!nic->sqs_mode && is_zero_ether_addr(netdev->dev_addr)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001207 eth_hw_addr_random(netdev);
1208 nicvf_hw_set_mac_addr(nic, netdev);
1209 }
1210
Pavel Fedinbd049a92015-06-23 17:51:06 +03001211 if (nic->set_mac_pending) {
1212 nic->set_mac_pending = false;
1213 nicvf_hw_set_mac_addr(nic, netdev);
1214 }
1215
Sunil Goutham4863dea2015-05-26 19:20:15 -07001216 /* Init tasklet for handling Qset err interrupt */
1217 tasklet_init(&nic->qs_err_task, nicvf_handle_qs_err,
1218 (unsigned long)nic);
1219
1220 /* Init RBDR tasklet which will refill RBDR */
1221 tasklet_init(&nic->rbdr_task, nicvf_rbdr_task,
1222 (unsigned long)nic);
1223 INIT_DELAYED_WORK(&nic->rbdr_work, nicvf_rbdr_work);
1224
1225 /* Configure CPI alorithm */
1226 nic->cpi_alg = cpi_alg;
Sunil Goutham92dc8762015-08-30 12:29:15 +03001227 if (!nic->sqs_mode)
1228 nicvf_config_cpi(nic);
1229
1230 nicvf_request_sqs(nic);
1231 if (nic->sqs_mode)
1232 nicvf_get_primary_vf_struct(nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001233
Sunil Goutham712c3182016-11-15 17:37:36 +05301234 /* Configure receive side scaling and MTU */
1235 if (!nic->sqs_mode) {
Sunil Goutham92dc8762015-08-30 12:29:15 +03001236 nicvf_rss_init(nic);
Sunil Goutham712c3182016-11-15 17:37:36 +05301237 if (nicvf_update_hw_max_frs(nic, netdev->mtu))
1238 goto cleanup;
Sunil Goutham964cb692016-11-15 17:38:16 +05301239
1240 /* Clear percpu stats */
1241 for_each_possible_cpu(cpu)
1242 memset(per_cpu_ptr(nic->drv_stats, cpu), 0,
1243 sizeof(struct nicvf_drv_stats));
Sunil Goutham712c3182016-11-15 17:37:36 +05301244 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001245
1246 err = nicvf_register_interrupts(nic);
1247 if (err)
1248 goto cleanup;
1249
1250 /* Initialize the queues */
1251 err = nicvf_init_resources(nic);
1252 if (err)
1253 goto cleanup;
1254
1255 /* Make sure queue initialization is written */
1256 wmb();
1257
1258 nicvf_reg_write(nic, NIC_VF_INT, -1);
1259 /* Enable Qset err interrupt */
1260 nicvf_enable_intr(nic, NICVF_INTR_QS_ERR, 0);
1261
1262 /* Enable completion queue interrupt */
1263 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
1264 nicvf_enable_intr(nic, NICVF_INTR_CQ, qidx);
1265
1266 /* Enable RBDR threshold interrupt */
1267 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
1268 nicvf_enable_intr(nic, NICVF_INTR_RBDR, qidx);
1269
Sunil Gouthamc94acf82016-11-15 17:38:29 +05301270 /* Send VF config done msg to PF */
1271 mbx.msg.msg = NIC_MBOX_MSG_CFG_DONE;
1272 nicvf_write_to_mbx(nic, &mbx);
Sunil Goutham74840b82015-07-29 16:49:42 +03001273
Sunil Goutham4863dea2015-05-26 19:20:15 -07001274 return 0;
1275cleanup:
1276 nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
1277 nicvf_unregister_interrupts(nic);
Sunil Goutham39ad6ee2015-08-30 12:29:14 +03001278 tasklet_kill(&nic->qs_err_task);
1279 tasklet_kill(&nic->rbdr_task);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001280napi_del:
1281 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
1282 cq_poll = nic->napi[qidx];
1283 if (!cq_poll)
1284 continue;
1285 napi_disable(&cq_poll->napi);
1286 netif_napi_del(&cq_poll->napi);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001287 }
Sunil Goutham39ad6ee2015-08-30 12:29:14 +03001288 nicvf_free_cq_poll(nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001289 return err;
1290}
1291
Sunil Goutham4863dea2015-05-26 19:20:15 -07001292static int nicvf_change_mtu(struct net_device *netdev, int new_mtu)
1293{
1294 struct nicvf *nic = netdev_priv(netdev);
David S. Millerf9aa9dc2016-11-22 11:29:28 -05001295 int orig_mtu = netdev->mtu;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001296
Sunil Goutham4863dea2015-05-26 19:20:15 -07001297 netdev->mtu = new_mtu;
Sunil Goutham712c3182016-11-15 17:37:36 +05301298
1299 if (!netif_running(netdev))
1300 return 0;
1301
David S. Millerf9aa9dc2016-11-22 11:29:28 -05001302 if (nicvf_update_hw_max_frs(nic, new_mtu)) {
1303 netdev->mtu = orig_mtu;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001304 return -EINVAL;
David S. Millerf9aa9dc2016-11-22 11:29:28 -05001305 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001306
1307 return 0;
1308}
1309
1310static int nicvf_set_mac_address(struct net_device *netdev, void *p)
1311{
1312 struct sockaddr *addr = p;
1313 struct nicvf *nic = netdev_priv(netdev);
1314
1315 if (!is_valid_ether_addr(addr->sa_data))
1316 return -EADDRNOTAVAIL;
1317
1318 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1319
Pavel Fedinbd049a92015-06-23 17:51:06 +03001320 if (nic->msix_enabled) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001321 if (nicvf_hw_set_mac_addr(nic, netdev))
1322 return -EBUSY;
Pavel Fedinbd049a92015-06-23 17:51:06 +03001323 } else {
1324 nic->set_mac_pending = true;
1325 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001326
1327 return 0;
1328}
1329
Sunil Goutham4863dea2015-05-26 19:20:15 -07001330void nicvf_update_lmac_stats(struct nicvf *nic)
1331{
1332 int stat = 0;
1333 union nic_mbx mbx = {};
Sunil Goutham4863dea2015-05-26 19:20:15 -07001334
1335 if (!netif_running(nic->netdev))
1336 return;
1337
1338 mbx.bgx_stats.msg = NIC_MBOX_MSG_BGX_STATS;
1339 mbx.bgx_stats.vf_id = nic->vf_id;
1340 /* Rx stats */
1341 mbx.bgx_stats.rx = 1;
1342 while (stat < BGX_RX_STATS_COUNT) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001343 mbx.bgx_stats.idx = stat;
Sunil Goutham6051cba2015-08-30 12:29:11 +03001344 if (nicvf_send_msg_to_pf(nic, &mbx))
1345 return;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001346 stat++;
1347 }
1348
1349 stat = 0;
1350
1351 /* Tx stats */
1352 mbx.bgx_stats.rx = 0;
1353 while (stat < BGX_TX_STATS_COUNT) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001354 mbx.bgx_stats.idx = stat;
Sunil Goutham6051cba2015-08-30 12:29:11 +03001355 if (nicvf_send_msg_to_pf(nic, &mbx))
1356 return;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001357 stat++;
1358 }
1359}
1360
1361void nicvf_update_stats(struct nicvf *nic)
1362{
Sunil Goutham964cb692016-11-15 17:38:16 +05301363 int qidx, cpu;
1364 u64 tmp_stats = 0;
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001365 struct nicvf_hw_stats *stats = &nic->hw_stats;
Sunil Goutham964cb692016-11-15 17:38:16 +05301366 struct nicvf_drv_stats *drv_stats;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001367 struct queue_set *qs = nic->qs;
1368
1369#define GET_RX_STATS(reg) \
1370 nicvf_reg_read(nic, NIC_VNIC_RX_STAT_0_13 | (reg << 3))
1371#define GET_TX_STATS(reg) \
1372 nicvf_reg_read(nic, NIC_VNIC_TX_STAT_0_4 | (reg << 3))
1373
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001374 stats->rx_bytes = GET_RX_STATS(RX_OCTS);
1375 stats->rx_ucast_frames = GET_RX_STATS(RX_UCAST);
1376 stats->rx_bcast_frames = GET_RX_STATS(RX_BCAST);
1377 stats->rx_mcast_frames = GET_RX_STATS(RX_MCAST);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001378 stats->rx_fcs_errors = GET_RX_STATS(RX_FCS);
1379 stats->rx_l2_errors = GET_RX_STATS(RX_L2ERR);
1380 stats->rx_drop_red = GET_RX_STATS(RX_RED);
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001381 stats->rx_drop_red_bytes = GET_RX_STATS(RX_RED_OCTS);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001382 stats->rx_drop_overrun = GET_RX_STATS(RX_ORUN);
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001383 stats->rx_drop_overrun_bytes = GET_RX_STATS(RX_ORUN_OCTS);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001384 stats->rx_drop_bcast = GET_RX_STATS(RX_DRP_BCAST);
1385 stats->rx_drop_mcast = GET_RX_STATS(RX_DRP_MCAST);
1386 stats->rx_drop_l3_bcast = GET_RX_STATS(RX_DRP_L3BCAST);
1387 stats->rx_drop_l3_mcast = GET_RX_STATS(RX_DRP_L3MCAST);
1388
Sunil Goutham964cb692016-11-15 17:38:16 +05301389 stats->tx_bytes = GET_TX_STATS(TX_OCTS);
1390 stats->tx_ucast_frames = GET_TX_STATS(TX_UCAST);
1391 stats->tx_bcast_frames = GET_TX_STATS(TX_BCAST);
1392 stats->tx_mcast_frames = GET_TX_STATS(TX_MCAST);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001393 stats->tx_drops = GET_TX_STATS(TX_DROP);
1394
Sunil Goutham964cb692016-11-15 17:38:16 +05301395 /* On T88 pass 2.0, the dummy SQE added for TSO notification
1396 * via CQE has 'dont_send' set. Hence HW drops the pkt pointed
1397 * pointed by dummy SQE and results in tx_drops counter being
1398 * incremented. Subtracting it from tx_tso counter will give
1399 * exact tx_drops counter.
1400 */
1401 if (nic->t88 && nic->hw_tso) {
1402 for_each_possible_cpu(cpu) {
1403 drv_stats = per_cpu_ptr(nic->drv_stats, cpu);
1404 tmp_stats += drv_stats->tx_tso;
1405 }
1406 stats->tx_drops = tmp_stats - stats->tx_drops;
1407 }
1408 stats->tx_frames = stats->tx_ucast_frames +
1409 stats->tx_bcast_frames +
1410 stats->tx_mcast_frames;
1411 stats->rx_frames = stats->rx_ucast_frames +
1412 stats->rx_bcast_frames +
1413 stats->rx_mcast_frames;
1414 stats->rx_drops = stats->rx_drop_red +
1415 stats->rx_drop_overrun;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001416
1417 /* Update RQ and SQ stats */
1418 for (qidx = 0; qidx < qs->rq_cnt; qidx++)
1419 nicvf_update_rq_stats(nic, qidx);
1420 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
1421 nicvf_update_sq_stats(nic, qidx);
1422}
1423
Aleksey Makarovfd7ec062015-06-02 11:00:23 -07001424static struct rtnl_link_stats64 *nicvf_get_stats64(struct net_device *netdev,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001425 struct rtnl_link_stats64 *stats)
1426{
1427 struct nicvf *nic = netdev_priv(netdev);
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001428 struct nicvf_hw_stats *hw_stats = &nic->hw_stats;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001429
1430 nicvf_update_stats(nic);
1431
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001432 stats->rx_bytes = hw_stats->rx_bytes;
Sunil Goutham964cb692016-11-15 17:38:16 +05301433 stats->rx_packets = hw_stats->rx_frames;
1434 stats->rx_dropped = hw_stats->rx_drops;
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001435 stats->multicast = hw_stats->rx_mcast_frames;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001436
Sunil Goutham964cb692016-11-15 17:38:16 +05301437 stats->tx_bytes = hw_stats->tx_bytes;
1438 stats->tx_packets = hw_stats->tx_frames;
1439 stats->tx_dropped = hw_stats->tx_drops;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001440
1441 return stats;
1442}
1443
1444static void nicvf_tx_timeout(struct net_device *dev)
1445{
1446 struct nicvf *nic = netdev_priv(dev);
1447
1448 if (netif_msg_tx_err(nic))
1449 netdev_warn(dev, "%s: Transmit timed out, resetting\n",
1450 dev->name);
1451
Sunil Goutham964cb692016-11-15 17:38:16 +05301452 this_cpu_inc(nic->drv_stats->tx_timeout);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001453 schedule_work(&nic->reset_task);
1454}
1455
1456static void nicvf_reset_task(struct work_struct *work)
1457{
1458 struct nicvf *nic;
1459
1460 nic = container_of(work, struct nicvf, reset_task);
1461
1462 if (!netif_running(nic->netdev))
1463 return;
1464
1465 nicvf_stop(nic->netdev);
1466 nicvf_open(nic->netdev);
Florian Westphal860e9532016-05-03 16:33:13 +02001467 netif_trans_update(nic->netdev);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001468}
1469
Sunil Gouthamd77a2382015-08-30 12:29:16 +03001470static int nicvf_config_loopback(struct nicvf *nic,
1471 netdev_features_t features)
1472{
1473 union nic_mbx mbx = {};
1474
1475 mbx.lbk.msg = NIC_MBOX_MSG_LOOPBACK;
1476 mbx.lbk.vf_id = nic->vf_id;
1477 mbx.lbk.enable = (features & NETIF_F_LOOPBACK) != 0;
1478
1479 return nicvf_send_msg_to_pf(nic, &mbx);
1480}
1481
1482static netdev_features_t nicvf_fix_features(struct net_device *netdev,
1483 netdev_features_t features)
1484{
1485 struct nicvf *nic = netdev_priv(netdev);
1486
1487 if ((features & NETIF_F_LOOPBACK) &&
1488 netif_running(netdev) && !nic->loopback_supported)
1489 features &= ~NETIF_F_LOOPBACK;
1490
1491 return features;
1492}
1493
Sunil Gouthamaa2e2592015-08-30 12:29:13 +03001494static int nicvf_set_features(struct net_device *netdev,
1495 netdev_features_t features)
1496{
1497 struct nicvf *nic = netdev_priv(netdev);
1498 netdev_features_t changed = features ^ netdev->features;
1499
1500 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
1501 nicvf_config_vlan_stripping(nic, features);
1502
Sunil Gouthamd77a2382015-08-30 12:29:16 +03001503 if ((changed & NETIF_F_LOOPBACK) && netif_running(netdev))
1504 return nicvf_config_loopback(nic, features);
1505
Sunil Gouthamaa2e2592015-08-30 12:29:13 +03001506 return 0;
1507}
1508
Sunil Goutham4863dea2015-05-26 19:20:15 -07001509static const struct net_device_ops nicvf_netdev_ops = {
1510 .ndo_open = nicvf_open,
1511 .ndo_stop = nicvf_stop,
1512 .ndo_start_xmit = nicvf_xmit,
1513 .ndo_change_mtu = nicvf_change_mtu,
1514 .ndo_set_mac_address = nicvf_set_mac_address,
1515 .ndo_get_stats64 = nicvf_get_stats64,
1516 .ndo_tx_timeout = nicvf_tx_timeout,
Sunil Gouthamd77a2382015-08-30 12:29:16 +03001517 .ndo_fix_features = nicvf_fix_features,
Sunil Gouthamaa2e2592015-08-30 12:29:13 +03001518 .ndo_set_features = nicvf_set_features,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001519};
1520
1521static int nicvf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1522{
1523 struct device *dev = &pdev->dev;
1524 struct net_device *netdev;
1525 struct nicvf *nic;
Sunil Goutham92dc8762015-08-30 12:29:15 +03001526 int err, qcount;
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301527 u16 sdevid;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001528
1529 err = pci_enable_device(pdev);
1530 if (err) {
1531 dev_err(dev, "Failed to enable PCI device\n");
1532 return err;
1533 }
1534
1535 err = pci_request_regions(pdev, DRV_NAME);
1536 if (err) {
1537 dev_err(dev, "PCI request regions failed 0x%x\n", err);
1538 goto err_disable_device;
1539 }
1540
1541 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(48));
1542 if (err) {
1543 dev_err(dev, "Unable to get usable DMA configuration\n");
1544 goto err_release_regions;
1545 }
1546
1547 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48));
1548 if (err) {
1549 dev_err(dev, "unable to get 48-bit DMA for consistent allocations\n");
1550 goto err_release_regions;
1551 }
1552
Sunil Goutham3a397eb2016-08-12 16:51:27 +05301553 qcount = netif_get_num_default_rss_queues();
Sunil Goutham92dc8762015-08-30 12:29:15 +03001554
1555 /* Restrict multiqset support only for host bound VFs */
1556 if (pdev->is_virtfn) {
1557 /* Set max number of queues per VF */
Sunil Goutham3a397eb2016-08-12 16:51:27 +05301558 qcount = min_t(int, num_online_cpus(),
1559 (MAX_SQS_PER_VF + 1) * MAX_CMP_QUEUES_PER_QS);
Sunil Goutham92dc8762015-08-30 12:29:15 +03001560 }
1561
1562 netdev = alloc_etherdev_mqs(sizeof(struct nicvf), qcount, qcount);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001563 if (!netdev) {
1564 err = -ENOMEM;
1565 goto err_release_regions;
1566 }
1567
1568 pci_set_drvdata(pdev, netdev);
1569
1570 SET_NETDEV_DEV(netdev, &pdev->dev);
1571
1572 nic = netdev_priv(netdev);
1573 nic->netdev = netdev;
1574 nic->pdev = pdev;
Sunil Goutham92dc8762015-08-30 12:29:15 +03001575 nic->pnicvf = nic;
1576 nic->max_queues = qcount;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001577
1578 /* MAP VF's configuration registers */
1579 nic->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
1580 if (!nic->reg_base) {
1581 dev_err(dev, "Cannot map config register space, aborting\n");
1582 err = -ENOMEM;
1583 goto err_free_netdev;
1584 }
1585
Sunil Goutham964cb692016-11-15 17:38:16 +05301586 nic->drv_stats = netdev_alloc_pcpu_stats(struct nicvf_drv_stats);
1587 if (!nic->drv_stats) {
1588 err = -ENOMEM;
1589 goto err_free_netdev;
1590 }
1591
Sunil Goutham4863dea2015-05-26 19:20:15 -07001592 err = nicvf_set_qset_resources(nic);
1593 if (err)
1594 goto err_free_netdev;
1595
Sunil Goutham4863dea2015-05-26 19:20:15 -07001596 /* Check if PF is alive and get MAC address for this VF */
1597 err = nicvf_register_misc_interrupt(nic);
1598 if (err)
1599 goto err_free_netdev;
1600
Sunil Goutham92dc8762015-08-30 12:29:15 +03001601 nicvf_send_vf_struct(nic);
1602
Sunil Goutham8d210d52016-02-16 16:29:50 +05301603 if (!pass1_silicon(nic->pdev))
1604 nic->hw_tso = true;
1605
Sunil Goutham7ceb8a12016-08-30 11:36:27 +05301606 pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
1607 if (sdevid == 0xA134)
1608 nic->t88 = true;
1609
Sunil Goutham92dc8762015-08-30 12:29:15 +03001610 /* Check if this VF is in QS only mode */
1611 if (nic->sqs_mode)
1612 return 0;
1613
1614 err = nicvf_set_real_num_queues(netdev, nic->tx_queues, nic->rx_queues);
1615 if (err)
1616 goto err_unregister_interrupts;
1617
Sunil Gouthamaa2e2592015-08-30 12:29:13 +03001618 netdev->hw_features = (NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
1619 NETIF_F_TSO | NETIF_F_GRO |
Sunil Goutham92dc8762015-08-30 12:29:15 +03001620 NETIF_F_HW_VLAN_CTAG_RX);
1621
1622 netdev->hw_features |= NETIF_F_RXHASH;
Sunil Goutham38bb5d42015-08-30 12:29:12 +03001623
Sunil Gouthamaa2e2592015-08-30 12:29:13 +03001624 netdev->features |= netdev->hw_features;
Sunil Gouthamd77a2382015-08-30 12:29:16 +03001625 netdev->hw_features |= NETIF_F_LOOPBACK;
Sunil Gouthamaa2e2592015-08-30 12:29:13 +03001626
1627 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001628
1629 netdev->netdev_ops = &nicvf_netdev_ops;
Sunil Goutham3d7a8aa2015-07-29 16:49:43 +03001630 netdev->watchdog_timeo = NICVF_TX_TIMEOUT;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001631
Jarod Wilson109cc162016-10-17 15:54:13 -04001632 /* MTU range: 64 - 9200 */
1633 netdev->min_mtu = NIC_HW_MIN_FRS;
1634 netdev->max_mtu = NIC_HW_MAX_FRS;
1635
Sunil Goutham4863dea2015-05-26 19:20:15 -07001636 INIT_WORK(&nic->reset_task, nicvf_reset_task);
1637
1638 err = register_netdev(netdev);
1639 if (err) {
1640 dev_err(dev, "Failed to register netdevice\n");
1641 goto err_unregister_interrupts;
1642 }
1643
1644 nic->msg_enable = debug;
1645
1646 nicvf_set_ethtool_ops(netdev);
1647
1648 return 0;
1649
1650err_unregister_interrupts:
1651 nicvf_unregister_interrupts(nic);
1652err_free_netdev:
1653 pci_set_drvdata(pdev, NULL);
Sunil Goutham964cb692016-11-15 17:38:16 +05301654 if (nic->drv_stats)
1655 free_percpu(nic->drv_stats);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001656 free_netdev(netdev);
1657err_release_regions:
1658 pci_release_regions(pdev);
1659err_disable_device:
1660 pci_disable_device(pdev);
1661 return err;
1662}
1663
1664static void nicvf_remove(struct pci_dev *pdev)
1665{
1666 struct net_device *netdev = pci_get_drvdata(pdev);
Pavel Fedin77501302015-11-16 17:51:34 +03001667 struct nicvf *nic;
1668 struct net_device *pnetdev;
1669
1670 if (!netdev)
1671 return;
1672
1673 nic = netdev_priv(netdev);
1674 pnetdev = nic->pnicvf->netdev;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001675
Sunil Goutham92dc8762015-08-30 12:29:15 +03001676 /* Check if this Qset is assigned to different VF.
1677 * If yes, clean primary and all secondary Qsets.
1678 */
1679 if (pnetdev && (pnetdev->reg_state == NETREG_REGISTERED))
1680 unregister_netdev(pnetdev);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001681 nicvf_unregister_interrupts(nic);
1682 pci_set_drvdata(pdev, NULL);
Sunil Goutham964cb692016-11-15 17:38:16 +05301683 if (nic->drv_stats)
1684 free_percpu(nic->drv_stats);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001685 free_netdev(netdev);
1686 pci_release_regions(pdev);
1687 pci_disable_device(pdev);
1688}
1689
Sunil Goutham4adf4352015-07-29 16:49:45 +03001690static void nicvf_shutdown(struct pci_dev *pdev)
1691{
1692 nicvf_remove(pdev);
1693}
1694
Sunil Goutham4863dea2015-05-26 19:20:15 -07001695static struct pci_driver nicvf_driver = {
1696 .name = DRV_NAME,
1697 .id_table = nicvf_id_table,
1698 .probe = nicvf_probe,
1699 .remove = nicvf_remove,
Sunil Goutham4adf4352015-07-29 16:49:45 +03001700 .shutdown = nicvf_shutdown,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001701};
1702
1703static int __init nicvf_init_module(void)
1704{
1705 pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
1706
1707 return pci_register_driver(&nicvf_driver);
1708}
1709
1710static void __exit nicvf_cleanup_module(void)
1711{
1712 pci_unregister_driver(&nicvf_driver);
1713}
1714
1715module_init(nicvf_init_module);
1716module_exit(nicvf_cleanup_module);