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