blob: de518288973e7bdec3babc6698c545f891a901d3 [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,
32 PCI_VENDOR_ID_CAVIUM, 0xA11E) },
33 { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
34 PCI_DEVICE_ID_THUNDER_PASS1_NIC_VF,
35 PCI_VENDOR_ID_CAVIUM, 0xA11E) },
36 { 0, } /* end of table */
37};
38
39MODULE_AUTHOR("Sunil Goutham");
40MODULE_DESCRIPTION("Cavium Thunder NIC Virtual Function Driver");
41MODULE_LICENSE("GPL v2");
42MODULE_VERSION(DRV_VERSION);
43MODULE_DEVICE_TABLE(pci, nicvf_id_table);
44
45static int debug = 0x00;
46module_param(debug, int, 0644);
47MODULE_PARM_DESC(debug, "Debug message level bitmap");
48
49static int cpi_alg = CPI_ALG_NONE;
50module_param(cpi_alg, int, S_IRUGO);
51MODULE_PARM_DESC(cpi_alg,
52 "PFC algorithm (0=none, 1=VLAN, 2=VLAN16, 3=IP Diffserv)");
53
Sunil Goutham4863dea2015-05-26 19:20:15 -070054static inline void nicvf_set_rx_frame_cnt(struct nicvf *nic,
55 struct sk_buff *skb)
56{
57 if (skb->len <= 64)
58 nic->drv_stats.rx_frames_64++;
59 else if (skb->len <= 127)
60 nic->drv_stats.rx_frames_127++;
61 else if (skb->len <= 255)
62 nic->drv_stats.rx_frames_255++;
63 else if (skb->len <= 511)
64 nic->drv_stats.rx_frames_511++;
65 else if (skb->len <= 1023)
66 nic->drv_stats.rx_frames_1023++;
67 else if (skb->len <= 1518)
68 nic->drv_stats.rx_frames_1518++;
69 else
70 nic->drv_stats.rx_frames_jumbo++;
71}
72
73/* The Cavium ThunderX network controller can *only* be found in SoCs
74 * containing the ThunderX ARM64 CPU implementation. All accesses to the device
75 * registers on this platform are implicitly strongly ordered with respect
76 * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
77 * with no memory barriers in this driver. The readq()/writeq() functions add
78 * explicit ordering operation which in this case are redundant, and only
79 * add overhead.
80 */
81
82/* Register read/write APIs */
83void nicvf_reg_write(struct nicvf *nic, u64 offset, u64 val)
84{
85 writeq_relaxed(val, nic->reg_base + offset);
86}
87
88u64 nicvf_reg_read(struct nicvf *nic, u64 offset)
89{
90 return readq_relaxed(nic->reg_base + offset);
91}
92
93void nicvf_queue_reg_write(struct nicvf *nic, u64 offset,
94 u64 qidx, u64 val)
95{
96 void __iomem *addr = nic->reg_base + offset;
97
98 writeq_relaxed(val, addr + (qidx << NIC_Q_NUM_SHIFT));
99}
100
101u64 nicvf_queue_reg_read(struct nicvf *nic, u64 offset, u64 qidx)
102{
103 void __iomem *addr = nic->reg_base + offset;
104
105 return readq_relaxed(addr + (qidx << NIC_Q_NUM_SHIFT));
106}
107
108/* VF -> PF mailbox communication */
Aleksey Makarov2cd2a192015-06-02 11:00:20 -0700109static void nicvf_write_to_mbx(struct nicvf *nic, union nic_mbx *mbx)
110{
111 u64 *msg = (u64 *)mbx;
112
113 nicvf_reg_write(nic, NIC_VF_PF_MAILBOX_0_1 + 0, msg[0]);
114 nicvf_reg_write(nic, NIC_VF_PF_MAILBOX_0_1 + 8, msg[1]);
115}
116
Sunil Goutham4863dea2015-05-26 19:20:15 -0700117int nicvf_send_msg_to_pf(struct nicvf *nic, union nic_mbx *mbx)
118{
119 int timeout = NIC_MBOX_MSG_TIMEOUT;
120 int sleep = 10;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700121
122 nic->pf_acked = false;
123 nic->pf_nacked = false;
124
Aleksey Makarov2cd2a192015-06-02 11:00:20 -0700125 nicvf_write_to_mbx(nic, mbx);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700126
127 /* Wait for previous message to be acked, timeout 2sec */
128 while (!nic->pf_acked) {
129 if (nic->pf_nacked)
130 return -EINVAL;
131 msleep(sleep);
132 if (nic->pf_acked)
133 break;
134 timeout -= sleep;
135 if (!timeout) {
136 netdev_err(nic->netdev,
137 "PF didn't ack to mbox msg %d from VF%d\n",
138 (mbx->msg.msg & 0xFF), nic->vf_id);
139 return -EBUSY;
140 }
141 }
142 return 0;
143}
144
145/* Checks if VF is able to comminicate with PF
146* and also gets the VNIC number this VF is associated to.
147*/
148static int nicvf_check_pf_ready(struct nicvf *nic)
149{
Aleksey Makarov2cd2a192015-06-02 11:00:20 -0700150 union nic_mbx mbx = {};
151
152 mbx.msg.msg = NIC_MBOX_MSG_READY;
Sunil Goutham6051cba2015-08-30 12:29:11 +0300153 if (nicvf_send_msg_to_pf(nic, &mbx)) {
154 netdev_err(nic->netdev,
155 "PF didn't respond to READY msg\n");
156 return 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700157 }
Sunil Goutham6051cba2015-08-30 12:29:11 +0300158
Sunil Goutham4863dea2015-05-26 19:20:15 -0700159 return 1;
160}
161
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700162static void nicvf_read_bgx_stats(struct nicvf *nic, struct bgx_stats_msg *bgx)
163{
164 if (bgx->rx)
165 nic->bgx_stats.rx_stats[bgx->idx] = bgx->stats;
166 else
167 nic->bgx_stats.tx_stats[bgx->idx] = bgx->stats;
168}
169
Sunil Goutham4863dea2015-05-26 19:20:15 -0700170static void nicvf_handle_mbx_intr(struct nicvf *nic)
171{
172 union nic_mbx mbx = {};
173 u64 *mbx_data;
174 u64 mbx_addr;
175 int i;
176
177 mbx_addr = NIC_VF_PF_MAILBOX_0_1;
178 mbx_data = (u64 *)&mbx;
179
180 for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
181 *mbx_data = nicvf_reg_read(nic, mbx_addr);
182 mbx_data++;
183 mbx_addr += sizeof(u64);
184 }
185
186 netdev_dbg(nic->netdev, "Mbox message: msg: 0x%x\n", mbx.msg.msg);
187 switch (mbx.msg.msg) {
188 case NIC_MBOX_MSG_READY:
Sunil Goutham6051cba2015-08-30 12:29:11 +0300189 nic->pf_acked = true;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700190 nic->vf_id = mbx.nic_cfg.vf_id & 0x7F;
191 nic->tns_mode = mbx.nic_cfg.tns_mode & 0x7F;
192 nic->node = mbx.nic_cfg.node_id;
Pavel Fedinbd049a92015-06-23 17:51:06 +0300193 if (!nic->set_mac_pending)
194 ether_addr_copy(nic->netdev->dev_addr,
195 mbx.nic_cfg.mac_addr);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700196 nic->link_up = false;
197 nic->duplex = 0;
198 nic->speed = 0;
199 break;
200 case NIC_MBOX_MSG_ACK:
201 nic->pf_acked = true;
202 break;
203 case NIC_MBOX_MSG_NACK:
204 nic->pf_nacked = true;
205 break;
206 case NIC_MBOX_MSG_RSS_SIZE:
207 nic->rss_info.rss_size = mbx.rss_size.ind_tbl_size;
208 nic->pf_acked = true;
209 break;
210 case NIC_MBOX_MSG_BGX_STATS:
211 nicvf_read_bgx_stats(nic, &mbx.bgx_stats);
212 nic->pf_acked = true;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700213 break;
214 case NIC_MBOX_MSG_BGX_LINK_CHANGE:
215 nic->pf_acked = true;
216 nic->link_up = mbx.link_status.link_up;
217 nic->duplex = mbx.link_status.duplex;
218 nic->speed = mbx.link_status.speed;
219 if (nic->link_up) {
220 netdev_info(nic->netdev, "%s: Link is Up %d Mbps %s\n",
221 nic->netdev->name, nic->speed,
222 nic->duplex == DUPLEX_FULL ?
223 "Full duplex" : "Half duplex");
224 netif_carrier_on(nic->netdev);
Sunil Gouthamb49087d2015-07-29 16:49:44 +0300225 netif_tx_start_all_queues(nic->netdev);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700226 } else {
227 netdev_info(nic->netdev, "%s: Link is Down\n",
228 nic->netdev->name);
229 netif_carrier_off(nic->netdev);
230 netif_tx_stop_all_queues(nic->netdev);
231 }
232 break;
233 default:
234 netdev_err(nic->netdev,
235 "Invalid message from PF, msg 0x%x\n", mbx.msg.msg);
236 break;
237 }
238 nicvf_clear_intr(nic, NICVF_INTR_MBOX, 0);
239}
240
241static int nicvf_hw_set_mac_addr(struct nicvf *nic, struct net_device *netdev)
242{
243 union nic_mbx mbx = {};
Sunil Goutham4863dea2015-05-26 19:20:15 -0700244
245 mbx.mac.msg = NIC_MBOX_MSG_SET_MAC;
246 mbx.mac.vf_id = nic->vf_id;
Aleksey Makarove610cb32015-06-02 11:00:21 -0700247 ether_addr_copy(mbx.mac.mac_addr, netdev->dev_addr);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700248
249 return nicvf_send_msg_to_pf(nic, &mbx);
250}
251
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700252static void nicvf_config_cpi(struct nicvf *nic)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700253{
254 union nic_mbx mbx = {};
255
256 mbx.cpi_cfg.msg = NIC_MBOX_MSG_CPI_CFG;
257 mbx.cpi_cfg.vf_id = nic->vf_id;
258 mbx.cpi_cfg.cpi_alg = nic->cpi_alg;
259 mbx.cpi_cfg.rq_cnt = nic->qs->rq_cnt;
260
261 nicvf_send_msg_to_pf(nic, &mbx);
262}
263
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700264static void nicvf_get_rss_size(struct nicvf *nic)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700265{
266 union nic_mbx mbx = {};
267
268 mbx.rss_size.msg = NIC_MBOX_MSG_RSS_SIZE;
269 mbx.rss_size.vf_id = nic->vf_id;
270 nicvf_send_msg_to_pf(nic, &mbx);
271}
272
273void nicvf_config_rss(struct nicvf *nic)
274{
275 union nic_mbx mbx = {};
276 struct nicvf_rss_info *rss = &nic->rss_info;
277 int ind_tbl_len = rss->rss_size;
278 int i, nextq = 0;
279
280 mbx.rss_cfg.vf_id = nic->vf_id;
281 mbx.rss_cfg.hash_bits = rss->hash_bits;
282 while (ind_tbl_len) {
283 mbx.rss_cfg.tbl_offset = nextq;
284 mbx.rss_cfg.tbl_len = min(ind_tbl_len,
285 RSS_IND_TBL_LEN_PER_MBX_MSG);
286 mbx.rss_cfg.msg = mbx.rss_cfg.tbl_offset ?
287 NIC_MBOX_MSG_RSS_CFG_CONT : NIC_MBOX_MSG_RSS_CFG;
288
289 for (i = 0; i < mbx.rss_cfg.tbl_len; i++)
290 mbx.rss_cfg.ind_tbl[i] = rss->ind_tbl[nextq++];
291
292 nicvf_send_msg_to_pf(nic, &mbx);
293
294 ind_tbl_len -= mbx.rss_cfg.tbl_len;
295 }
296}
297
298void nicvf_set_rss_key(struct nicvf *nic)
299{
300 struct nicvf_rss_info *rss = &nic->rss_info;
301 u64 key_addr = NIC_VNIC_RSS_KEY_0_4;
302 int idx;
303
304 for (idx = 0; idx < RSS_HASH_KEY_SIZE; idx++) {
305 nicvf_reg_write(nic, key_addr, rss->key[idx]);
306 key_addr += sizeof(u64);
307 }
308}
309
310static int nicvf_rss_init(struct nicvf *nic)
311{
312 struct nicvf_rss_info *rss = &nic->rss_info;
313 int idx;
314
315 nicvf_get_rss_size(nic);
316
Sunil Goutham38bb5d42015-08-30 12:29:12 +0300317 if (cpi_alg != CPI_ALG_NONE) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700318 rss->enable = false;
319 rss->hash_bits = 0;
320 return 0;
321 }
322
323 rss->enable = true;
324
325 /* Using the HW reset value for now */
Aleksey Makarov4a4f87d2015-06-02 11:00:19 -0700326 rss->key[0] = 0xFEED0BADFEED0BADULL;
327 rss->key[1] = 0xFEED0BADFEED0BADULL;
328 rss->key[2] = 0xFEED0BADFEED0BADULL;
329 rss->key[3] = 0xFEED0BADFEED0BADULL;
330 rss->key[4] = 0xFEED0BADFEED0BADULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700331
332 nicvf_set_rss_key(nic);
333
334 rss->cfg = RSS_IP_HASH_ENA | RSS_TCP_HASH_ENA | RSS_UDP_HASH_ENA;
335 nicvf_reg_write(nic, NIC_VNIC_RSS_CFG, rss->cfg);
336
337 rss->hash_bits = ilog2(rounddown_pow_of_two(rss->rss_size));
338
339 for (idx = 0; idx < rss->rss_size; idx++)
340 rss->ind_tbl[idx] = ethtool_rxfh_indir_default(idx,
341 nic->qs->rq_cnt);
342 nicvf_config_rss(nic);
343 return 1;
344}
345
346int nicvf_set_real_num_queues(struct net_device *netdev,
347 int tx_queues, int rx_queues)
348{
349 int err = 0;
350
351 err = netif_set_real_num_tx_queues(netdev, tx_queues);
352 if (err) {
353 netdev_err(netdev,
354 "Failed to set no of Tx queues: %d\n", tx_queues);
355 return err;
356 }
357
358 err = netif_set_real_num_rx_queues(netdev, rx_queues);
359 if (err)
360 netdev_err(netdev,
361 "Failed to set no of Rx queues: %d\n", rx_queues);
362 return err;
363}
364
365static int nicvf_init_resources(struct nicvf *nic)
366{
367 int err;
Aleksey Makarov2cd2a192015-06-02 11:00:20 -0700368 union nic_mbx mbx = {};
369
370 mbx.msg.msg = NIC_MBOX_MSG_CFG_DONE;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700371
372 /* Enable Qset */
373 nicvf_qset_config(nic, true);
374
375 /* Initialize queues and HW for data transfer */
376 err = nicvf_config_data_transfer(nic, true);
377 if (err) {
378 netdev_err(nic->netdev,
379 "Failed to alloc/config VF's QSet resources\n");
380 return err;
381 }
382
383 /* Send VF config done msg to PF */
Aleksey Makarov2cd2a192015-06-02 11:00:20 -0700384 nicvf_write_to_mbx(nic, &mbx);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700385
386 return 0;
387}
388
389static void nicvf_snd_pkt_handler(struct net_device *netdev,
390 struct cmp_queue *cq,
391 struct cqe_send_t *cqe_tx, int cqe_type)
392{
393 struct sk_buff *skb = NULL;
394 struct nicvf *nic = netdev_priv(netdev);
395 struct snd_queue *sq;
396 struct sq_hdr_subdesc *hdr;
397
398 sq = &nic->qs->sq[cqe_tx->sq_idx];
399
400 hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, cqe_tx->sqe_ptr);
401 if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER)
402 return;
403
404 netdev_dbg(nic->netdev,
405 "%s Qset #%d SQ #%d SQ ptr #%d subdesc count %d\n",
406 __func__, cqe_tx->sq_qs, cqe_tx->sq_idx,
407 cqe_tx->sqe_ptr, hdr->subdesc_cnt);
408
409 nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
410 nicvf_check_cqe_tx_errs(nic, cq, cqe_tx);
411 skb = (struct sk_buff *)sq->skbuff[cqe_tx->sqe_ptr];
412 /* For TSO offloaded packets only one head SKB needs to be freed */
413 if (skb) {
414 prefetch(skb);
415 dev_consume_skb_any(skb);
Sunil Goutham143ceb02015-07-29 16:49:37 +0300416 sq->skbuff[cqe_tx->sqe_ptr] = (u64)NULL;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700417 }
418}
419
Sunil Goutham38bb5d42015-08-30 12:29:12 +0300420static inline void nicvf_set_rxhash(struct net_device *netdev,
421 struct cqe_rx_t *cqe_rx,
422 struct sk_buff *skb)
423{
424 u8 hash_type;
425 u32 hash;
426
427 if (!(netdev->features & NETIF_F_RXHASH))
428 return;
429
430 switch (cqe_rx->rss_alg) {
431 case RSS_ALG_TCP_IP:
432 case RSS_ALG_UDP_IP:
433 hash_type = PKT_HASH_TYPE_L4;
434 hash = cqe_rx->rss_tag;
435 break;
436 case RSS_ALG_IP:
437 hash_type = PKT_HASH_TYPE_L3;
438 hash = cqe_rx->rss_tag;
439 break;
440 default:
441 hash_type = PKT_HASH_TYPE_NONE;
442 hash = 0;
443 }
444
445 skb_set_hash(skb, hash, hash_type);
446}
447
Sunil Goutham4863dea2015-05-26 19:20:15 -0700448static void nicvf_rcv_pkt_handler(struct net_device *netdev,
449 struct napi_struct *napi,
450 struct cmp_queue *cq,
451 struct cqe_rx_t *cqe_rx, int cqe_type)
452{
453 struct sk_buff *skb;
454 struct nicvf *nic = netdev_priv(netdev);
455 int err = 0;
456
457 /* Check for errors */
458 err = nicvf_check_cqe_rx_errs(nic, cq, cqe_rx);
459 if (err && !cqe_rx->rb_cnt)
460 return;
461
462 skb = nicvf_get_rcv_skb(nic, cqe_rx);
463 if (!skb) {
464 netdev_dbg(nic->netdev, "Packet not received\n");
465 return;
466 }
467
468 if (netif_msg_pktdata(nic)) {
469 netdev_info(nic->netdev, "%s: skb 0x%p, len=%d\n", netdev->name,
470 skb, skb->len);
471 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
472 skb->data, skb->len, true);
473 }
474
Sunil Gouthama2dc5de2015-08-30 12:29:10 +0300475 /* If error packet, drop it here */
476 if (err) {
477 dev_kfree_skb_any(skb);
478 return;
479 }
480
Sunil Goutham4863dea2015-05-26 19:20:15 -0700481 nicvf_set_rx_frame_cnt(nic, skb);
482
Sunil Goutham38bb5d42015-08-30 12:29:12 +0300483 nicvf_set_rxhash(netdev, cqe_rx, skb);
484
Sunil Goutham4863dea2015-05-26 19:20:15 -0700485 skb_record_rx_queue(skb, cqe_rx->rq_idx);
486 if (netdev->hw_features & NETIF_F_RXCSUM) {
487 /* HW by default verifies TCP/UDP/SCTP checksums */
488 skb->ip_summed = CHECKSUM_UNNECESSARY;
489 } else {
490 skb_checksum_none_assert(skb);
491 }
492
493 skb->protocol = eth_type_trans(skb, netdev);
494
Sunil Gouthamaa2e2592015-08-30 12:29:13 +0300495 /* Check for stripped VLAN */
496 if (cqe_rx->vlan_found && cqe_rx->vlan_stripped)
497 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
498 ntohs((__force __be16)cqe_rx->vlan_tci));
499
Sunil Goutham4863dea2015-05-26 19:20:15 -0700500 if (napi && (netdev->features & NETIF_F_GRO))
501 napi_gro_receive(napi, skb);
502 else
503 netif_receive_skb(skb);
504}
505
506static int nicvf_cq_intr_handler(struct net_device *netdev, u8 cq_idx,
507 struct napi_struct *napi, int budget)
508{
Sunil Goutham74840b82015-07-29 16:49:42 +0300509 int processed_cqe, work_done = 0, tx_done = 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700510 int cqe_count, cqe_head;
511 struct nicvf *nic = netdev_priv(netdev);
512 struct queue_set *qs = nic->qs;
513 struct cmp_queue *cq = &qs->cq[cq_idx];
514 struct cqe_rx_t *cq_desc;
Sunil Goutham74840b82015-07-29 16:49:42 +0300515 struct netdev_queue *txq;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700516
517 spin_lock_bh(&cq->lock);
518loop:
519 processed_cqe = 0;
520 /* Get no of valid CQ entries to process */
521 cqe_count = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS, cq_idx);
522 cqe_count &= CQ_CQE_COUNT;
523 if (!cqe_count)
524 goto done;
525
526 /* Get head of the valid CQ entries */
527 cqe_head = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD, cq_idx) >> 9;
528 cqe_head &= 0xFFFF;
529
Sunil Goutham74840b82015-07-29 16:49:42 +0300530 netdev_dbg(nic->netdev, "%s CQ%d cqe_count %d cqe_head %d\n",
531 __func__, cq_idx, cqe_count, cqe_head);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700532 while (processed_cqe < cqe_count) {
533 /* Get the CQ descriptor */
534 cq_desc = (struct cqe_rx_t *)GET_CQ_DESC(cq, cqe_head);
535 cqe_head++;
536 cqe_head &= (cq->dmem.q_len - 1);
537 /* Initiate prefetch for next descriptor */
538 prefetch((struct cqe_rx_t *)GET_CQ_DESC(cq, cqe_head));
539
540 if ((work_done >= budget) && napi &&
541 (cq_desc->cqe_type != CQE_TYPE_SEND)) {
542 break;
543 }
544
Sunil Goutham74840b82015-07-29 16:49:42 +0300545 netdev_dbg(nic->netdev, "CQ%d cq_desc->cqe_type %d\n",
546 cq_idx, cq_desc->cqe_type);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700547 switch (cq_desc->cqe_type) {
548 case CQE_TYPE_RX:
549 nicvf_rcv_pkt_handler(netdev, napi, cq,
550 cq_desc, CQE_TYPE_RX);
551 work_done++;
552 break;
553 case CQE_TYPE_SEND:
554 nicvf_snd_pkt_handler(netdev, cq,
555 (void *)cq_desc, CQE_TYPE_SEND);
Sunil Goutham74840b82015-07-29 16:49:42 +0300556 tx_done++;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700557 break;
558 case CQE_TYPE_INVALID:
559 case CQE_TYPE_RX_SPLIT:
560 case CQE_TYPE_RX_TCP:
561 case CQE_TYPE_SEND_PTP:
562 /* Ignore for now */
563 break;
564 }
565 processed_cqe++;
566 }
Sunil Goutham74840b82015-07-29 16:49:42 +0300567 netdev_dbg(nic->netdev,
568 "%s CQ%d processed_cqe %d work_done %d budget %d\n",
569 __func__, cq_idx, processed_cqe, work_done, budget);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700570
571 /* Ring doorbell to inform H/W to reuse processed CQEs */
572 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_DOOR,
573 cq_idx, processed_cqe);
574
575 if ((work_done < budget) && napi)
576 goto loop;
577
578done:
Sunil Goutham74840b82015-07-29 16:49:42 +0300579 /* Wakeup TXQ if its stopped earlier due to SQ full */
580 if (tx_done) {
581 txq = netdev_get_tx_queue(netdev, cq_idx);
582 if (netif_tx_queue_stopped(txq)) {
Sunil Gouthamb49087d2015-07-29 16:49:44 +0300583 netif_tx_start_queue(txq);
Sunil Goutham74840b82015-07-29 16:49:42 +0300584 nic->drv_stats.txq_wake++;
585 if (netif_msg_tx_err(nic))
586 netdev_warn(netdev,
587 "%s: Transmit queue wakeup SQ%d\n",
588 netdev->name, cq_idx);
589 }
590 }
591
Sunil Goutham4863dea2015-05-26 19:20:15 -0700592 spin_unlock_bh(&cq->lock);
593 return work_done;
594}
595
596static int nicvf_poll(struct napi_struct *napi, int budget)
597{
598 u64 cq_head;
599 int work_done = 0;
600 struct net_device *netdev = napi->dev;
601 struct nicvf *nic = netdev_priv(netdev);
602 struct nicvf_cq_poll *cq;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700603
604 cq = container_of(napi, struct nicvf_cq_poll, napi);
605 work_done = nicvf_cq_intr_handler(netdev, cq->cq_idx, napi, budget);
606
Sunil Goutham4863dea2015-05-26 19:20:15 -0700607 if (work_done < budget) {
608 /* Slow packet rate, exit polling */
609 napi_complete(napi);
610 /* Re-enable interrupts */
611 cq_head = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD,
612 cq->cq_idx);
613 nicvf_clear_intr(nic, NICVF_INTR_CQ, cq->cq_idx);
614 nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_HEAD,
615 cq->cq_idx, cq_head);
616 nicvf_enable_intr(nic, NICVF_INTR_CQ, cq->cq_idx);
617 }
618 return work_done;
619}
620
621/* Qset error interrupt handler
622 *
623 * As of now only CQ errors are handled
624 */
Aleksey Makarovfd7ec062015-06-02 11:00:23 -0700625static void nicvf_handle_qs_err(unsigned long data)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700626{
627 struct nicvf *nic = (struct nicvf *)data;
628 struct queue_set *qs = nic->qs;
629 int qidx;
630 u64 status;
631
632 netif_tx_disable(nic->netdev);
633
634 /* Check if it is CQ err */
635 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
636 status = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS,
637 qidx);
638 if (!(status & CQ_ERR_MASK))
639 continue;
640 /* Process already queued CQEs and reconfig CQ */
641 nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
642 nicvf_sq_disable(nic, qidx);
643 nicvf_cq_intr_handler(nic->netdev, qidx, NULL, 0);
644 nicvf_cmp_queue_config(nic, qs, qidx, true);
645 nicvf_sq_free_used_descs(nic->netdev, &qs->sq[qidx], qidx);
646 nicvf_sq_enable(nic, &qs->sq[qidx], qidx);
647
648 nicvf_enable_intr(nic, NICVF_INTR_CQ, qidx);
649 }
650
651 netif_tx_start_all_queues(nic->netdev);
652 /* Re-enable Qset error interrupt */
653 nicvf_enable_intr(nic, NICVF_INTR_QS_ERR, 0);
654}
655
656static irqreturn_t nicvf_misc_intr_handler(int irq, void *nicvf_irq)
657{
658 struct nicvf *nic = (struct nicvf *)nicvf_irq;
659 u64 intr;
660
661 intr = nicvf_reg_read(nic, NIC_VF_INT);
662 /* Check for spurious interrupt */
663 if (!(intr & NICVF_INTR_MBOX_MASK))
664 return IRQ_HANDLED;
665
666 nicvf_handle_mbx_intr(nic);
667
668 return IRQ_HANDLED;
669}
670
671static irqreturn_t nicvf_intr_handler(int irq, void *nicvf_irq)
672{
673 u64 qidx, intr, clear_intr = 0;
674 u64 cq_intr, rbdr_intr, qs_err_intr;
675 struct nicvf *nic = (struct nicvf *)nicvf_irq;
676 struct queue_set *qs = nic->qs;
677 struct nicvf_cq_poll *cq_poll = NULL;
678
679 intr = nicvf_reg_read(nic, NIC_VF_INT);
680 if (netif_msg_intr(nic))
681 netdev_info(nic->netdev, "%s: interrupt status 0x%llx\n",
682 nic->netdev->name, intr);
683
684 qs_err_intr = intr & NICVF_INTR_QS_ERR_MASK;
685 if (qs_err_intr) {
686 /* Disable Qset err interrupt and schedule softirq */
687 nicvf_disable_intr(nic, NICVF_INTR_QS_ERR, 0);
688 tasklet_hi_schedule(&nic->qs_err_task);
689 clear_intr |= qs_err_intr;
690 }
691
692 /* Disable interrupts and start polling */
693 cq_intr = (intr & NICVF_INTR_CQ_MASK) >> NICVF_INTR_CQ_SHIFT;
694 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
695 if (!(cq_intr & (1 << qidx)))
696 continue;
697 if (!nicvf_is_intr_enabled(nic, NICVF_INTR_CQ, qidx))
698 continue;
699
700 nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
701 clear_intr |= ((1 << qidx) << NICVF_INTR_CQ_SHIFT);
702
703 cq_poll = nic->napi[qidx];
704 /* Schedule NAPI */
705 if (cq_poll)
706 napi_schedule(&cq_poll->napi);
707 }
708
709 /* Handle RBDR interrupts */
710 rbdr_intr = (intr & NICVF_INTR_RBDR_MASK) >> NICVF_INTR_RBDR_SHIFT;
711 if (rbdr_intr) {
712 /* Disable RBDR interrupt and schedule softirq */
713 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
714 if (!nicvf_is_intr_enabled(nic, NICVF_INTR_RBDR, qidx))
715 continue;
716 nicvf_disable_intr(nic, NICVF_INTR_RBDR, qidx);
717 tasklet_hi_schedule(&nic->rbdr_task);
718 clear_intr |= ((1 << qidx) << NICVF_INTR_RBDR_SHIFT);
719 }
720 }
721
722 /* Clear interrupts */
723 nicvf_reg_write(nic, NIC_VF_INT, clear_intr);
724 return IRQ_HANDLED;
725}
726
727static int nicvf_enable_msix(struct nicvf *nic)
728{
729 int ret, vec;
730
731 nic->num_vec = NIC_VF_MSIX_VECTORS;
732
733 for (vec = 0; vec < nic->num_vec; vec++)
734 nic->msix_entries[vec].entry = vec;
735
736 ret = pci_enable_msix(nic->pdev, nic->msix_entries, nic->num_vec);
737 if (ret) {
738 netdev_err(nic->netdev,
739 "Req for #%d msix vectors failed\n", nic->num_vec);
740 return 0;
741 }
742 nic->msix_enabled = 1;
743 return 1;
744}
745
746static void nicvf_disable_msix(struct nicvf *nic)
747{
748 if (nic->msix_enabled) {
749 pci_disable_msix(nic->pdev);
750 nic->msix_enabled = 0;
751 nic->num_vec = 0;
752 }
753}
754
755static int nicvf_register_interrupts(struct nicvf *nic)
756{
757 int irq, free, ret = 0;
758 int vector;
759
760 for_each_cq_irq(irq)
761 sprintf(nic->irq_name[irq], "NICVF%d CQ%d",
762 nic->vf_id, irq);
763
764 for_each_sq_irq(irq)
765 sprintf(nic->irq_name[irq], "NICVF%d SQ%d",
766 nic->vf_id, irq - NICVF_INTR_ID_SQ);
767
768 for_each_rbdr_irq(irq)
769 sprintf(nic->irq_name[irq], "NICVF%d RBDR%d",
770 nic->vf_id, irq - NICVF_INTR_ID_RBDR);
771
772 /* Register all interrupts except mailbox */
773 for (irq = 0; irq < NICVF_INTR_ID_SQ; irq++) {
774 vector = nic->msix_entries[irq].vector;
775 ret = request_irq(vector, nicvf_intr_handler,
776 0, nic->irq_name[irq], nic);
777 if (ret)
778 break;
779 nic->irq_allocated[irq] = true;
780 }
781
782 for (irq = NICVF_INTR_ID_SQ; irq < NICVF_INTR_ID_MISC; irq++) {
783 vector = nic->msix_entries[irq].vector;
784 ret = request_irq(vector, nicvf_intr_handler,
785 0, nic->irq_name[irq], nic);
786 if (ret)
787 break;
788 nic->irq_allocated[irq] = true;
789 }
790
791 sprintf(nic->irq_name[NICVF_INTR_ID_QS_ERR],
792 "NICVF%d Qset error", nic->vf_id);
793 if (!ret) {
794 vector = nic->msix_entries[NICVF_INTR_ID_QS_ERR].vector;
795 irq = NICVF_INTR_ID_QS_ERR;
796 ret = request_irq(vector, nicvf_intr_handler,
797 0, nic->irq_name[irq], nic);
798 if (!ret)
799 nic->irq_allocated[irq] = true;
800 }
801
802 if (ret) {
803 netdev_err(nic->netdev, "Request irq failed\n");
804 for (free = 0; free < irq; free++)
805 free_irq(nic->msix_entries[free].vector, nic);
806 return ret;
807 }
808
809 return 0;
810}
811
812static void nicvf_unregister_interrupts(struct nicvf *nic)
813{
814 int irq;
815
816 /* Free registered interrupts */
817 for (irq = 0; irq < nic->num_vec; irq++) {
818 if (nic->irq_allocated[irq])
819 free_irq(nic->msix_entries[irq].vector, nic);
820 nic->irq_allocated[irq] = false;
821 }
822
823 /* Disable MSI-X */
824 nicvf_disable_msix(nic);
825}
826
827/* Initialize MSIX vectors and register MISC interrupt.
828 * Send READY message to PF to check if its alive
829 */
830static int nicvf_register_misc_interrupt(struct nicvf *nic)
831{
832 int ret = 0;
833 int irq = NICVF_INTR_ID_MISC;
834
835 /* Return if mailbox interrupt is already registered */
836 if (nic->msix_enabled)
837 return 0;
838
839 /* Enable MSI-X */
840 if (!nicvf_enable_msix(nic))
841 return 1;
842
843 sprintf(nic->irq_name[irq], "%s Mbox", "NICVF");
844 /* Register Misc interrupt */
845 ret = request_irq(nic->msix_entries[irq].vector,
846 nicvf_misc_intr_handler, 0, nic->irq_name[irq], nic);
847
848 if (ret)
849 return ret;
850 nic->irq_allocated[irq] = true;
851
852 /* Enable mailbox interrupt */
853 nicvf_enable_intr(nic, NICVF_INTR_MBOX, 0);
854
855 /* Check if VF is able to communicate with PF */
856 if (!nicvf_check_pf_ready(nic)) {
857 nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
858 nicvf_unregister_interrupts(nic);
859 return 1;
860 }
861
862 return 0;
863}
864
865static netdev_tx_t nicvf_xmit(struct sk_buff *skb, struct net_device *netdev)
866{
867 struct nicvf *nic = netdev_priv(netdev);
868 int qid = skb_get_queue_mapping(skb);
869 struct netdev_queue *txq = netdev_get_tx_queue(netdev, qid);
870
871 /* Check for minimum packet length */
872 if (skb->len <= ETH_HLEN) {
873 dev_kfree_skb(skb);
874 return NETDEV_TX_OK;
875 }
876
Sunil Gouthamb49087d2015-07-29 16:49:44 +0300877 if (!netif_tx_queue_stopped(txq) && !nicvf_sq_append_skb(nic, skb)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700878 netif_tx_stop_queue(txq);
Sunil Goutham74840b82015-07-29 16:49:42 +0300879 nic->drv_stats.txq_stop++;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700880 if (netif_msg_tx_err(nic))
881 netdev_warn(netdev,
882 "%s: Transmit ring full, stopping SQ%d\n",
883 netdev->name, qid);
884
885 return NETDEV_TX_BUSY;
886 }
887
888 return NETDEV_TX_OK;
889}
890
891int nicvf_stop(struct net_device *netdev)
892{
893 int irq, qidx;
894 struct nicvf *nic = netdev_priv(netdev);
895 struct queue_set *qs = nic->qs;
896 struct nicvf_cq_poll *cq_poll = NULL;
897 union nic_mbx mbx = {};
898
899 mbx.msg.msg = NIC_MBOX_MSG_SHUTDOWN;
900 nicvf_send_msg_to_pf(nic, &mbx);
901
902 netif_carrier_off(netdev);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700903
904 /* Disable RBDR & QS error interrupts */
905 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
906 nicvf_disable_intr(nic, NICVF_INTR_RBDR, qidx);
907 nicvf_clear_intr(nic, NICVF_INTR_RBDR, qidx);
908 }
909 nicvf_disable_intr(nic, NICVF_INTR_QS_ERR, 0);
910 nicvf_clear_intr(nic, NICVF_INTR_QS_ERR, 0);
911
912 /* Wait for pending IRQ handlers to finish */
913 for (irq = 0; irq < nic->num_vec; irq++)
914 synchronize_irq(nic->msix_entries[irq].vector);
915
916 tasklet_kill(&nic->rbdr_task);
917 tasklet_kill(&nic->qs_err_task);
918 if (nic->rb_work_scheduled)
919 cancel_delayed_work_sync(&nic->rbdr_work);
920
921 for (qidx = 0; qidx < nic->qs->cq_cnt; qidx++) {
922 cq_poll = nic->napi[qidx];
923 if (!cq_poll)
924 continue;
925 nic->napi[qidx] = NULL;
926 napi_synchronize(&cq_poll->napi);
927 /* CQ intr is enabled while napi_complete,
928 * so disable it now
929 */
930 nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
931 nicvf_clear_intr(nic, NICVF_INTR_CQ, qidx);
932 napi_disable(&cq_poll->napi);
933 netif_napi_del(&cq_poll->napi);
934 kfree(cq_poll);
935 }
936
Sunil Gouthamb49087d2015-07-29 16:49:44 +0300937 netif_tx_disable(netdev);
938
Sunil Goutham4863dea2015-05-26 19:20:15 -0700939 /* Free resources */
940 nicvf_config_data_transfer(nic, false);
941
942 /* Disable HW Qset */
943 nicvf_qset_config(nic, false);
944
945 /* disable mailbox interrupt */
946 nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
947
948 nicvf_unregister_interrupts(nic);
949
950 return 0;
951}
952
953int nicvf_open(struct net_device *netdev)
954{
955 int err, qidx;
956 struct nicvf *nic = netdev_priv(netdev);
957 struct queue_set *qs = nic->qs;
958 struct nicvf_cq_poll *cq_poll = NULL;
959
960 nic->mtu = netdev->mtu;
961
962 netif_carrier_off(netdev);
963
964 err = nicvf_register_misc_interrupt(nic);
965 if (err)
966 return err;
967
968 /* Register NAPI handler for processing CQEs */
969 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
970 cq_poll = kzalloc(sizeof(*cq_poll), GFP_KERNEL);
971 if (!cq_poll) {
972 err = -ENOMEM;
973 goto napi_del;
974 }
975 cq_poll->cq_idx = qidx;
976 netif_napi_add(netdev, &cq_poll->napi, nicvf_poll,
977 NAPI_POLL_WEIGHT);
978 napi_enable(&cq_poll->napi);
979 nic->napi[qidx] = cq_poll;
980 }
981
982 /* Check if we got MAC address from PF or else generate a radom MAC */
983 if (is_zero_ether_addr(netdev->dev_addr)) {
984 eth_hw_addr_random(netdev);
985 nicvf_hw_set_mac_addr(nic, netdev);
986 }
987
Pavel Fedinbd049a92015-06-23 17:51:06 +0300988 if (nic->set_mac_pending) {
989 nic->set_mac_pending = false;
990 nicvf_hw_set_mac_addr(nic, netdev);
991 }
992
Sunil Goutham4863dea2015-05-26 19:20:15 -0700993 /* Init tasklet for handling Qset err interrupt */
994 tasklet_init(&nic->qs_err_task, nicvf_handle_qs_err,
995 (unsigned long)nic);
996
997 /* Init RBDR tasklet which will refill RBDR */
998 tasklet_init(&nic->rbdr_task, nicvf_rbdr_task,
999 (unsigned long)nic);
1000 INIT_DELAYED_WORK(&nic->rbdr_work, nicvf_rbdr_work);
1001
1002 /* Configure CPI alorithm */
1003 nic->cpi_alg = cpi_alg;
1004 nicvf_config_cpi(nic);
1005
1006 /* Configure receive side scaling */
1007 nicvf_rss_init(nic);
1008
1009 err = nicvf_register_interrupts(nic);
1010 if (err)
1011 goto cleanup;
1012
1013 /* Initialize the queues */
1014 err = nicvf_init_resources(nic);
1015 if (err)
1016 goto cleanup;
1017
1018 /* Make sure queue initialization is written */
1019 wmb();
1020
1021 nicvf_reg_write(nic, NIC_VF_INT, -1);
1022 /* Enable Qset err interrupt */
1023 nicvf_enable_intr(nic, NICVF_INTR_QS_ERR, 0);
1024
1025 /* Enable completion queue interrupt */
1026 for (qidx = 0; qidx < qs->cq_cnt; qidx++)
1027 nicvf_enable_intr(nic, NICVF_INTR_CQ, qidx);
1028
1029 /* Enable RBDR threshold interrupt */
1030 for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
1031 nicvf_enable_intr(nic, NICVF_INTR_RBDR, qidx);
1032
Sunil Goutham74840b82015-07-29 16:49:42 +03001033 nic->drv_stats.txq_stop = 0;
1034 nic->drv_stats.txq_wake = 0;
1035
Sunil Goutham4863dea2015-05-26 19:20:15 -07001036 netif_carrier_on(netdev);
1037 netif_tx_start_all_queues(netdev);
1038
1039 return 0;
1040cleanup:
1041 nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
1042 nicvf_unregister_interrupts(nic);
1043napi_del:
1044 for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
1045 cq_poll = nic->napi[qidx];
1046 if (!cq_poll)
1047 continue;
1048 napi_disable(&cq_poll->napi);
1049 netif_napi_del(&cq_poll->napi);
1050 kfree(cq_poll);
1051 nic->napi[qidx] = NULL;
1052 }
1053 return err;
1054}
1055
1056static int nicvf_update_hw_max_frs(struct nicvf *nic, int mtu)
1057{
1058 union nic_mbx mbx = {};
1059
1060 mbx.frs.msg = NIC_MBOX_MSG_SET_MAX_FRS;
1061 mbx.frs.max_frs = mtu;
1062 mbx.frs.vf_id = nic->vf_id;
1063
1064 return nicvf_send_msg_to_pf(nic, &mbx);
1065}
1066
1067static int nicvf_change_mtu(struct net_device *netdev, int new_mtu)
1068{
1069 struct nicvf *nic = netdev_priv(netdev);
1070
1071 if (new_mtu > NIC_HW_MAX_FRS)
1072 return -EINVAL;
1073
1074 if (new_mtu < NIC_HW_MIN_FRS)
1075 return -EINVAL;
1076
1077 if (nicvf_update_hw_max_frs(nic, new_mtu))
1078 return -EINVAL;
1079 netdev->mtu = new_mtu;
1080 nic->mtu = new_mtu;
1081
1082 return 0;
1083}
1084
1085static int nicvf_set_mac_address(struct net_device *netdev, void *p)
1086{
1087 struct sockaddr *addr = p;
1088 struct nicvf *nic = netdev_priv(netdev);
1089
1090 if (!is_valid_ether_addr(addr->sa_data))
1091 return -EADDRNOTAVAIL;
1092
1093 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1094
Pavel Fedinbd049a92015-06-23 17:51:06 +03001095 if (nic->msix_enabled) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001096 if (nicvf_hw_set_mac_addr(nic, netdev))
1097 return -EBUSY;
Pavel Fedinbd049a92015-06-23 17:51:06 +03001098 } else {
1099 nic->set_mac_pending = true;
1100 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001101
1102 return 0;
1103}
1104
Sunil Goutham4863dea2015-05-26 19:20:15 -07001105void nicvf_update_lmac_stats(struct nicvf *nic)
1106{
1107 int stat = 0;
1108 union nic_mbx mbx = {};
Sunil Goutham4863dea2015-05-26 19:20:15 -07001109
1110 if (!netif_running(nic->netdev))
1111 return;
1112
1113 mbx.bgx_stats.msg = NIC_MBOX_MSG_BGX_STATS;
1114 mbx.bgx_stats.vf_id = nic->vf_id;
1115 /* Rx stats */
1116 mbx.bgx_stats.rx = 1;
1117 while (stat < BGX_RX_STATS_COUNT) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001118 mbx.bgx_stats.idx = stat;
Sunil Goutham6051cba2015-08-30 12:29:11 +03001119 if (nicvf_send_msg_to_pf(nic, &mbx))
1120 return;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001121 stat++;
1122 }
1123
1124 stat = 0;
1125
1126 /* Tx stats */
1127 mbx.bgx_stats.rx = 0;
1128 while (stat < BGX_TX_STATS_COUNT) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001129 mbx.bgx_stats.idx = stat;
Sunil Goutham6051cba2015-08-30 12:29:11 +03001130 if (nicvf_send_msg_to_pf(nic, &mbx))
1131 return;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001132 stat++;
1133 }
1134}
1135
1136void nicvf_update_stats(struct nicvf *nic)
1137{
1138 int qidx;
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001139 struct nicvf_hw_stats *stats = &nic->hw_stats;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001140 struct nicvf_drv_stats *drv_stats = &nic->drv_stats;
1141 struct queue_set *qs = nic->qs;
1142
1143#define GET_RX_STATS(reg) \
1144 nicvf_reg_read(nic, NIC_VNIC_RX_STAT_0_13 | (reg << 3))
1145#define GET_TX_STATS(reg) \
1146 nicvf_reg_read(nic, NIC_VNIC_TX_STAT_0_4 | (reg << 3))
1147
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001148 stats->rx_bytes = GET_RX_STATS(RX_OCTS);
1149 stats->rx_ucast_frames = GET_RX_STATS(RX_UCAST);
1150 stats->rx_bcast_frames = GET_RX_STATS(RX_BCAST);
1151 stats->rx_mcast_frames = GET_RX_STATS(RX_MCAST);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001152 stats->rx_fcs_errors = GET_RX_STATS(RX_FCS);
1153 stats->rx_l2_errors = GET_RX_STATS(RX_L2ERR);
1154 stats->rx_drop_red = GET_RX_STATS(RX_RED);
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001155 stats->rx_drop_red_bytes = GET_RX_STATS(RX_RED_OCTS);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001156 stats->rx_drop_overrun = GET_RX_STATS(RX_ORUN);
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001157 stats->rx_drop_overrun_bytes = GET_RX_STATS(RX_ORUN_OCTS);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001158 stats->rx_drop_bcast = GET_RX_STATS(RX_DRP_BCAST);
1159 stats->rx_drop_mcast = GET_RX_STATS(RX_DRP_MCAST);
1160 stats->rx_drop_l3_bcast = GET_RX_STATS(RX_DRP_L3BCAST);
1161 stats->rx_drop_l3_mcast = GET_RX_STATS(RX_DRP_L3MCAST);
1162
1163 stats->tx_bytes_ok = GET_TX_STATS(TX_OCTS);
1164 stats->tx_ucast_frames_ok = GET_TX_STATS(TX_UCAST);
1165 stats->tx_bcast_frames_ok = GET_TX_STATS(TX_BCAST);
1166 stats->tx_mcast_frames_ok = GET_TX_STATS(TX_MCAST);
1167 stats->tx_drops = GET_TX_STATS(TX_DROP);
1168
Sunil Goutham4863dea2015-05-26 19:20:15 -07001169 drv_stats->tx_frames_ok = stats->tx_ucast_frames_ok +
1170 stats->tx_bcast_frames_ok +
1171 stats->tx_mcast_frames_ok;
1172 drv_stats->rx_drops = stats->rx_drop_red +
1173 stats->rx_drop_overrun;
1174 drv_stats->tx_drops = stats->tx_drops;
1175
1176 /* Update RQ and SQ stats */
1177 for (qidx = 0; qidx < qs->rq_cnt; qidx++)
1178 nicvf_update_rq_stats(nic, qidx);
1179 for (qidx = 0; qidx < qs->sq_cnt; qidx++)
1180 nicvf_update_sq_stats(nic, qidx);
1181}
1182
Aleksey Makarovfd7ec062015-06-02 11:00:23 -07001183static struct rtnl_link_stats64 *nicvf_get_stats64(struct net_device *netdev,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001184 struct rtnl_link_stats64 *stats)
1185{
1186 struct nicvf *nic = netdev_priv(netdev);
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001187 struct nicvf_hw_stats *hw_stats = &nic->hw_stats;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001188 struct nicvf_drv_stats *drv_stats = &nic->drv_stats;
1189
1190 nicvf_update_stats(nic);
1191
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001192 stats->rx_bytes = hw_stats->rx_bytes;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001193 stats->rx_packets = drv_stats->rx_frames_ok;
1194 stats->rx_dropped = drv_stats->rx_drops;
Sunil Gouthama2dc5de2015-08-30 12:29:10 +03001195 stats->multicast = hw_stats->rx_mcast_frames;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001196
1197 stats->tx_bytes = hw_stats->tx_bytes_ok;
1198 stats->tx_packets = drv_stats->tx_frames_ok;
1199 stats->tx_dropped = drv_stats->tx_drops;
1200
1201 return stats;
1202}
1203
1204static void nicvf_tx_timeout(struct net_device *dev)
1205{
1206 struct nicvf *nic = netdev_priv(dev);
1207
1208 if (netif_msg_tx_err(nic))
1209 netdev_warn(dev, "%s: Transmit timed out, resetting\n",
1210 dev->name);
1211
1212 schedule_work(&nic->reset_task);
1213}
1214
1215static void nicvf_reset_task(struct work_struct *work)
1216{
1217 struct nicvf *nic;
1218
1219 nic = container_of(work, struct nicvf, reset_task);
1220
1221 if (!netif_running(nic->netdev))
1222 return;
1223
1224 nicvf_stop(nic->netdev);
1225 nicvf_open(nic->netdev);
1226 nic->netdev->trans_start = jiffies;
1227}
1228
Sunil Gouthamaa2e2592015-08-30 12:29:13 +03001229static int nicvf_set_features(struct net_device *netdev,
1230 netdev_features_t features)
1231{
1232 struct nicvf *nic = netdev_priv(netdev);
1233 netdev_features_t changed = features ^ netdev->features;
1234
1235 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
1236 nicvf_config_vlan_stripping(nic, features);
1237
1238 return 0;
1239}
1240
Sunil Goutham4863dea2015-05-26 19:20:15 -07001241static const struct net_device_ops nicvf_netdev_ops = {
1242 .ndo_open = nicvf_open,
1243 .ndo_stop = nicvf_stop,
1244 .ndo_start_xmit = nicvf_xmit,
1245 .ndo_change_mtu = nicvf_change_mtu,
1246 .ndo_set_mac_address = nicvf_set_mac_address,
1247 .ndo_get_stats64 = nicvf_get_stats64,
1248 .ndo_tx_timeout = nicvf_tx_timeout,
Sunil Gouthamaa2e2592015-08-30 12:29:13 +03001249 .ndo_set_features = nicvf_set_features,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001250};
1251
1252static int nicvf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1253{
1254 struct device *dev = &pdev->dev;
1255 struct net_device *netdev;
1256 struct nicvf *nic;
1257 struct queue_set *qs;
1258 int err;
1259
1260 err = pci_enable_device(pdev);
1261 if (err) {
1262 dev_err(dev, "Failed to enable PCI device\n");
1263 return err;
1264 }
1265
1266 err = pci_request_regions(pdev, DRV_NAME);
1267 if (err) {
1268 dev_err(dev, "PCI request regions failed 0x%x\n", err);
1269 goto err_disable_device;
1270 }
1271
1272 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(48));
1273 if (err) {
1274 dev_err(dev, "Unable to get usable DMA configuration\n");
1275 goto err_release_regions;
1276 }
1277
1278 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48));
1279 if (err) {
1280 dev_err(dev, "unable to get 48-bit DMA for consistent allocations\n");
1281 goto err_release_regions;
1282 }
1283
1284 netdev = alloc_etherdev_mqs(sizeof(struct nicvf),
1285 MAX_RCV_QUEUES_PER_QS,
1286 MAX_SND_QUEUES_PER_QS);
1287 if (!netdev) {
1288 err = -ENOMEM;
1289 goto err_release_regions;
1290 }
1291
1292 pci_set_drvdata(pdev, netdev);
1293
1294 SET_NETDEV_DEV(netdev, &pdev->dev);
1295
1296 nic = netdev_priv(netdev);
1297 nic->netdev = netdev;
1298 nic->pdev = pdev;
1299
1300 /* MAP VF's configuration registers */
1301 nic->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
1302 if (!nic->reg_base) {
1303 dev_err(dev, "Cannot map config register space, aborting\n");
1304 err = -ENOMEM;
1305 goto err_free_netdev;
1306 }
1307
1308 err = nicvf_set_qset_resources(nic);
1309 if (err)
1310 goto err_free_netdev;
1311
1312 qs = nic->qs;
1313
1314 err = nicvf_set_real_num_queues(netdev, qs->sq_cnt, qs->rq_cnt);
1315 if (err)
1316 goto err_free_netdev;
1317
1318 /* Check if PF is alive and get MAC address for this VF */
1319 err = nicvf_register_misc_interrupt(nic);
1320 if (err)
1321 goto err_free_netdev;
1322
Sunil Gouthamaa2e2592015-08-30 12:29:13 +03001323 netdev->hw_features = (NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
1324 NETIF_F_TSO | NETIF_F_GRO |
1325 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_RXHASH);
Sunil Goutham38bb5d42015-08-30 12:29:12 +03001326
Sunil Gouthamaa2e2592015-08-30 12:29:13 +03001327 netdev->features |= netdev->hw_features;
1328
1329 netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001330
1331 netdev->netdev_ops = &nicvf_netdev_ops;
Sunil Goutham3d7a8aa2015-07-29 16:49:43 +03001332 netdev->watchdog_timeo = NICVF_TX_TIMEOUT;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001333
1334 INIT_WORK(&nic->reset_task, nicvf_reset_task);
1335
1336 err = register_netdev(netdev);
1337 if (err) {
1338 dev_err(dev, "Failed to register netdevice\n");
1339 goto err_unregister_interrupts;
1340 }
1341
1342 nic->msg_enable = debug;
1343
1344 nicvf_set_ethtool_ops(netdev);
1345
1346 return 0;
1347
1348err_unregister_interrupts:
1349 nicvf_unregister_interrupts(nic);
1350err_free_netdev:
1351 pci_set_drvdata(pdev, NULL);
1352 free_netdev(netdev);
1353err_release_regions:
1354 pci_release_regions(pdev);
1355err_disable_device:
1356 pci_disable_device(pdev);
1357 return err;
1358}
1359
1360static void nicvf_remove(struct pci_dev *pdev)
1361{
1362 struct net_device *netdev = pci_get_drvdata(pdev);
1363 struct nicvf *nic = netdev_priv(netdev);
1364
1365 unregister_netdev(netdev);
1366 nicvf_unregister_interrupts(nic);
1367 pci_set_drvdata(pdev, NULL);
1368 free_netdev(netdev);
1369 pci_release_regions(pdev);
1370 pci_disable_device(pdev);
1371}
1372
Sunil Goutham4adf4352015-07-29 16:49:45 +03001373static void nicvf_shutdown(struct pci_dev *pdev)
1374{
1375 nicvf_remove(pdev);
1376}
1377
Sunil Goutham4863dea2015-05-26 19:20:15 -07001378static struct pci_driver nicvf_driver = {
1379 .name = DRV_NAME,
1380 .id_table = nicvf_id_table,
1381 .probe = nicvf_probe,
1382 .remove = nicvf_remove,
Sunil Goutham4adf4352015-07-29 16:49:45 +03001383 .shutdown = nicvf_shutdown,
Sunil Goutham4863dea2015-05-26 19:20:15 -07001384};
1385
1386static int __init nicvf_init_module(void)
1387{
1388 pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
1389
1390 return pci_register_driver(&nicvf_driver);
1391}
1392
1393static void __exit nicvf_cleanup_module(void)
1394{
1395 pci_unregister_driver(&nicvf_driver);
1396}
1397
1398module_init(nicvf_init_module);
1399module_exit(nicvf_cleanup_module);