1 /**********************************************************************
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
7 * Copyright (c) 2003-2017 Cavium, Inc.
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more details.
17 ***********************************************************************/
18 #include <linux/pci.h>
19 #include <linux/if_vlan.h>
20 #include "liquidio_common.h"
21 #include "octeon_droq.h"
22 #include "octeon_iq.h"
23 #include "response_manager.h"
24 #include "octeon_device.h"
25 #include "octeon_nic.h"
26 #include "octeon_main.h"
27 #include "octeon_network.h"
28 #include "lio_vf_rep.h"
30 static int lio_vf_rep_open(struct net_device *ndev);
31 static int lio_vf_rep_stop(struct net_device *ndev);
32 static netdev_tx_t lio_vf_rep_pkt_xmit(struct sk_buff *skb,
33 struct net_device *ndev);
34 static void lio_vf_rep_tx_timeout(struct net_device *netdev);
35 static int lio_vf_rep_phys_port_name(struct net_device *dev,
36 char *buf, size_t len);
37 static void lio_vf_rep_get_stats64(struct net_device *dev,
38 struct rtnl_link_stats64 *stats64);
39 static int lio_vf_rep_change_mtu(struct net_device *ndev, int new_mtu);
40 static int lio_vf_get_port_parent_id(struct net_device *dev,
41 struct netdev_phys_item_id *ppid);
43 static const struct net_device_ops lio_vf_rep_ndev_ops = {
44 .ndo_open = lio_vf_rep_open,
45 .ndo_stop = lio_vf_rep_stop,
46 .ndo_start_xmit = lio_vf_rep_pkt_xmit,
47 .ndo_tx_timeout = lio_vf_rep_tx_timeout,
48 .ndo_get_phys_port_name = lio_vf_rep_phys_port_name,
49 .ndo_get_stats64 = lio_vf_rep_get_stats64,
50 .ndo_change_mtu = lio_vf_rep_change_mtu,
51 .ndo_get_port_parent_id = lio_vf_get_port_parent_id,
55 lio_vf_rep_send_soft_command(struct octeon_device *oct,
56 void *req, int req_size,
57 void *resp, int resp_size)
59 int tot_resp_size = sizeof(struct lio_vf_rep_resp) + resp_size;
60 struct octeon_soft_command *sc = NULL;
61 struct lio_vf_rep_resp *rep_resp;
65 sc = (struct octeon_soft_command *)
66 octeon_alloc_soft_command(oct, req_size,
71 init_completion(&sc->complete);
72 sc->sc_status = OCTEON_REQUEST_PENDING;
74 sc_req = (struct lio_vf_rep_req *)sc->virtdptr;
75 memcpy(sc_req, req, req_size);
77 rep_resp = (struct lio_vf_rep_resp *)sc->virtrptr;
78 memset(rep_resp, 0, tot_resp_size);
79 WRITE_ONCE(rep_resp->status, 1);
82 octeon_prepare_soft_command(oct, sc, OPCODE_NIC,
83 OPCODE_NIC_VF_REP_CMD, 0, 0, 0);
85 err = octeon_send_soft_command(oct, sc);
86 if (err == IQ_SEND_FAILED)
89 err = wait_for_sc_completion_timeout(oct, sc, 0);
93 err = READ_ONCE(rep_resp->status) ? -EBUSY : 0;
95 dev_err(&oct->pci_dev->dev, "VF rep send config failed\n");
97 memcpy(resp, (rep_resp + 1), resp_size);
99 WRITE_ONCE(sc->caller_is_done, true);
103 octeon_free_soft_command(oct, sc);
109 lio_vf_rep_open(struct net_device *ndev)
111 struct lio_vf_rep_desc *vf_rep = netdev_priv(ndev);
112 struct lio_vf_rep_req rep_cfg;
113 struct octeon_device *oct;
118 memset(&rep_cfg, 0, sizeof(rep_cfg));
119 rep_cfg.req_type = LIO_VF_REP_REQ_STATE;
120 rep_cfg.ifidx = vf_rep->ifidx;
121 rep_cfg.rep_state.state = LIO_VF_REP_STATE_UP;
123 ret = lio_vf_rep_send_soft_command(oct, &rep_cfg,
124 sizeof(rep_cfg), NULL, 0);
127 dev_err(&oct->pci_dev->dev,
128 "VF_REP open failed with err %d\n", ret);
132 atomic_set(&vf_rep->ifstate, (atomic_read(&vf_rep->ifstate) |
133 LIO_IFSTATE_RUNNING));
135 netif_carrier_on(ndev);
136 netif_start_queue(ndev);
142 lio_vf_rep_stop(struct net_device *ndev)
144 struct lio_vf_rep_desc *vf_rep = netdev_priv(ndev);
145 struct lio_vf_rep_req rep_cfg;
146 struct octeon_device *oct;
151 memset(&rep_cfg, 0, sizeof(rep_cfg));
152 rep_cfg.req_type = LIO_VF_REP_REQ_STATE;
153 rep_cfg.ifidx = vf_rep->ifidx;
154 rep_cfg.rep_state.state = LIO_VF_REP_STATE_DOWN;
156 ret = lio_vf_rep_send_soft_command(oct, &rep_cfg,
157 sizeof(rep_cfg), NULL, 0);
160 dev_err(&oct->pci_dev->dev,
161 "VF_REP dev stop failed with err %d\n", ret);
165 atomic_set(&vf_rep->ifstate, (atomic_read(&vf_rep->ifstate) &
166 ~LIO_IFSTATE_RUNNING));
168 netif_tx_disable(ndev);
169 netif_carrier_off(ndev);
175 lio_vf_rep_tx_timeout(struct net_device *ndev)
177 netif_trans_update(ndev);
179 netif_wake_queue(ndev);
183 lio_vf_rep_get_stats64(struct net_device *dev,
184 struct rtnl_link_stats64 *stats64)
186 struct lio_vf_rep_desc *vf_rep = netdev_priv(dev);
188 /* Swap tx and rx stats as VF rep is a switch port */
189 stats64->tx_packets = vf_rep->stats.rx_packets;
190 stats64->tx_bytes = vf_rep->stats.rx_bytes;
191 stats64->tx_dropped = vf_rep->stats.rx_dropped;
193 stats64->rx_packets = vf_rep->stats.tx_packets;
194 stats64->rx_bytes = vf_rep->stats.tx_bytes;
195 stats64->rx_dropped = vf_rep->stats.tx_dropped;
199 lio_vf_rep_change_mtu(struct net_device *ndev, int new_mtu)
201 struct lio_vf_rep_desc *vf_rep = netdev_priv(ndev);
202 struct lio_vf_rep_req rep_cfg;
203 struct octeon_device *oct;
208 memset(&rep_cfg, 0, sizeof(rep_cfg));
209 rep_cfg.req_type = LIO_VF_REP_REQ_MTU;
210 rep_cfg.ifidx = vf_rep->ifidx;
211 rep_cfg.rep_mtu.mtu = cpu_to_be32(new_mtu);
213 ret = lio_vf_rep_send_soft_command(oct, &rep_cfg,
214 sizeof(rep_cfg), NULL, 0);
216 dev_err(&oct->pci_dev->dev,
217 "Change MTU failed with err %d\n", ret);
227 lio_vf_rep_phys_port_name(struct net_device *dev,
228 char *buf, size_t len)
230 struct lio_vf_rep_desc *vf_rep = netdev_priv(dev);
231 struct octeon_device *oct = vf_rep->oct;
234 ret = snprintf(buf, len, "pf%dvf%d", oct->pf_num,
235 vf_rep->ifidx - oct->pf_num * 64 - 1);
242 static struct net_device *
243 lio_vf_rep_get_ndev(struct octeon_device *oct, int ifidx)
245 int vf_id, max_vfs = CN23XX_MAX_VFS_PER_PF + 1;
246 int vfid_mask = max_vfs - 1;
248 if (ifidx <= oct->pf_num * max_vfs ||
249 ifidx >= oct->pf_num * max_vfs + max_vfs)
252 /* ifidx 1-63 for PF0 VFs
253 * ifidx 65-127 for PF1 VFs
255 vf_id = (ifidx & vfid_mask) - 1;
257 return oct->vf_rep_list.ndev[vf_id];
261 lio_vf_rep_copy_packet(struct octeon_device *oct,
265 if (likely(len > MIN_SKB_SIZE)) {
266 struct octeon_skb_page_info *pg_info;
269 pg_info = ((struct octeon_skb_page_info *)(skb->cb));
271 va = page_address(pg_info->page) +
272 pg_info->page_offset;
273 memcpy(skb->data, va, MIN_SKB_SIZE);
274 skb_put(skb, MIN_SKB_SIZE);
277 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
279 pg_info->page_offset + MIN_SKB_SIZE,
283 struct octeon_skb_page_info *pg_info =
284 ((struct octeon_skb_page_info *)(skb->cb));
286 skb_copy_to_linear_data(skb, page_address(pg_info->page) +
287 pg_info->page_offset, len);
289 put_page(pg_info->page);
294 lio_vf_rep_pkt_recv(struct octeon_recv_info *recv_info, void *buf)
296 struct octeon_recv_pkt *recv_pkt = recv_info->recv_pkt;
297 struct lio_vf_rep_desc *vf_rep;
298 struct net_device *vf_ndev;
299 struct octeon_device *oct;
304 oct = lio_get_device(recv_pkt->octeon_id);
308 skb = recv_pkt->buffer_ptr[0];
312 vf_ndev = lio_vf_rep_get_ndev(oct, ifidx);
316 vf_rep = netdev_priv(vf_ndev);
317 if (!(atomic_read(&vf_rep->ifstate) & LIO_IFSTATE_RUNNING) ||
318 recv_pkt->buffer_count > 1)
323 /* Multiple buffers are not used for vf_rep packets.
324 * So just buffer_size[0] is valid.
326 lio_vf_rep_copy_packet(oct, skb, recv_pkt->buffer_size[0]);
328 skb_pull(skb, rh->r_dh.len * BYTES_PER_DHLEN_UNIT);
329 skb->protocol = eth_type_trans(skb, skb->dev);
330 skb->ip_summed = CHECKSUM_NONE;
334 octeon_free_recv_info(recv_info);
339 for (i = 0; i < recv_pkt->buffer_count; i++)
340 recv_buffer_free(recv_pkt->buffer_ptr[i]);
342 octeon_free_recv_info(recv_info);
348 lio_vf_rep_packet_sent_callback(struct octeon_device *oct,
349 u32 status, void *buf)
351 struct octeon_soft_command *sc = (struct octeon_soft_command *)buf;
352 struct sk_buff *skb = sc->ctxptr;
353 struct net_device *ndev = skb->dev;
356 dma_unmap_single(&oct->pci_dev->dev, sc->dmadptr,
357 sc->datasize, DMA_TO_DEVICE);
358 dev_kfree_skb_any(skb);
360 octeon_free_soft_command(oct, sc);
362 if (octnet_iq_is_full(oct, iq_no))
365 if (netif_queue_stopped(ndev))
366 netif_wake_queue(ndev);
370 lio_vf_rep_pkt_xmit(struct sk_buff *skb, struct net_device *ndev)
372 struct lio_vf_rep_desc *vf_rep = netdev_priv(ndev);
373 struct net_device *parent_ndev = vf_rep->parent_ndev;
374 struct octeon_device *oct = vf_rep->oct;
375 struct octeon_instr_pki_ih3 *pki_ih3;
376 struct octeon_soft_command *sc;
377 struct lio *parent_lio;
380 parent_lio = GET_LIO(parent_ndev);
382 if (!(atomic_read(&vf_rep->ifstate) & LIO_IFSTATE_RUNNING) ||
386 if (octnet_iq_is_full(vf_rep->oct, parent_lio->txq)) {
387 dev_err(&oct->pci_dev->dev, "VF rep: Device IQ full\n");
388 netif_stop_queue(ndev);
389 return NETDEV_TX_BUSY;
392 sc = (struct octeon_soft_command *)
393 octeon_alloc_soft_command(oct, 0, 16, 0);
395 dev_err(&oct->pci_dev->dev, "VF rep: Soft command alloc failed\n");
399 /* Multiple buffers are not used for vf_rep packets. */
400 if (skb_shinfo(skb)->nr_frags != 0) {
401 dev_err(&oct->pci_dev->dev, "VF rep: nr_frags != 0. Dropping packet\n");
402 octeon_free_soft_command(oct, sc);
406 sc->dmadptr = dma_map_single(&oct->pci_dev->dev,
407 skb->data, skb->len, DMA_TO_DEVICE);
408 if (dma_mapping_error(&oct->pci_dev->dev, sc->dmadptr)) {
409 dev_err(&oct->pci_dev->dev, "VF rep: DMA mapping failed\n");
410 octeon_free_soft_command(oct, sc);
414 sc->virtdptr = skb->data;
415 sc->datasize = skb->len;
417 sc->iq_no = parent_lio->txq;
419 octeon_prepare_soft_command(oct, sc, OPCODE_NIC, OPCODE_NIC_VF_REP_PKT,
420 vf_rep->ifidx, 0, 0);
421 pki_ih3 = (struct octeon_instr_pki_ih3 *)&sc->cmd.cmd3.pki_ih3;
422 pki_ih3->tagtype = ORDERED_TAG;
424 sc->callback = lio_vf_rep_packet_sent_callback;
425 sc->callback_arg = sc;
427 status = octeon_send_soft_command(oct, sc);
428 if (status == IQ_SEND_FAILED) {
429 dma_unmap_single(&oct->pci_dev->dev, sc->dmadptr,
430 sc->datasize, DMA_TO_DEVICE);
431 octeon_free_soft_command(oct, sc);
435 if (status == IQ_SEND_STOP)
436 netif_stop_queue(ndev);
438 netif_trans_update(ndev);
443 dev_kfree_skb_any(skb);
448 static int lio_vf_get_port_parent_id(struct net_device *dev,
449 struct netdev_phys_item_id *ppid)
451 struct lio_vf_rep_desc *vf_rep = netdev_priv(dev);
452 struct net_device *parent_ndev = vf_rep->parent_ndev;
453 struct lio *lio = GET_LIO(parent_ndev);
455 ppid->id_len = ETH_ALEN;
456 ether_addr_copy(ppid->id, (void *)&lio->linfo.hw_addr + 2);
462 lio_vf_rep_fetch_stats(struct work_struct *work)
464 struct cavium_wk *wk = (struct cavium_wk *)work;
465 struct lio_vf_rep_desc *vf_rep = wk->ctxptr;
466 struct lio_vf_rep_stats stats;
467 struct lio_vf_rep_req rep_cfg;
468 struct octeon_device *oct;
473 memset(&rep_cfg, 0, sizeof(rep_cfg));
474 rep_cfg.req_type = LIO_VF_REP_REQ_STATS;
475 rep_cfg.ifidx = vf_rep->ifidx;
477 ret = lio_vf_rep_send_soft_command(oct, &rep_cfg, sizeof(rep_cfg),
478 &stats, sizeof(stats));
481 octeon_swap_8B_data((u64 *)&stats, (sizeof(stats) >> 3));
482 memcpy(&vf_rep->stats, &stats, sizeof(stats));
485 schedule_delayed_work(&vf_rep->stats_wk.work,
486 msecs_to_jiffies(LIO_VF_REP_STATS_POLL_TIME_MS));
490 lio_vf_rep_create(struct octeon_device *oct)
492 struct lio_vf_rep_desc *vf_rep;
493 struct net_device *ndev;
496 if (oct->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
499 if (!oct->sriov_info.sriov_enabled)
502 num_vfs = oct->sriov_info.num_vfs_alloced;
504 oct->vf_rep_list.num_vfs = 0;
505 for (i = 0; i < num_vfs; i++) {
506 ndev = alloc_etherdev(sizeof(struct lio_vf_rep_desc));
509 dev_err(&oct->pci_dev->dev,
510 "VF rep device %d creation failed\n", i);
514 ndev->min_mtu = LIO_MIN_MTU_SIZE;
515 ndev->max_mtu = LIO_MAX_MTU_SIZE;
516 ndev->netdev_ops = &lio_vf_rep_ndev_ops;
518 vf_rep = netdev_priv(ndev);
519 memset(vf_rep, 0, sizeof(*vf_rep));
523 vf_rep->parent_ndev = oct->props[0].netdev;
524 vf_rep->ifidx = (oct->pf_num * 64) + i + 1;
526 eth_hw_addr_random(ndev);
528 if (register_netdev(ndev)) {
529 dev_err(&oct->pci_dev->dev, "VF rep nerdev registration failed\n");
535 netif_carrier_off(ndev);
537 INIT_DELAYED_WORK(&vf_rep->stats_wk.work,
538 lio_vf_rep_fetch_stats);
539 vf_rep->stats_wk.ctxptr = (void *)vf_rep;
540 schedule_delayed_work(&vf_rep->stats_wk.work,
542 (LIO_VF_REP_STATS_POLL_TIME_MS));
543 oct->vf_rep_list.num_vfs++;
544 oct->vf_rep_list.ndev[i] = ndev;
547 if (octeon_register_dispatch_fn(oct, OPCODE_NIC,
548 OPCODE_NIC_VF_REP_PKT,
549 lio_vf_rep_pkt_recv, oct)) {
550 dev_err(&oct->pci_dev->dev, "VF rep Dispatch func registration failed\n");
558 for (i = 0; i < oct->vf_rep_list.num_vfs; i++) {
559 ndev = oct->vf_rep_list.ndev[i];
560 oct->vf_rep_list.ndev[i] = NULL;
562 vf_rep = netdev_priv(ndev);
563 cancel_delayed_work_sync
564 (&vf_rep->stats_wk.work);
565 unregister_netdev(ndev);
570 oct->vf_rep_list.num_vfs = 0;
576 lio_vf_rep_destroy(struct octeon_device *oct)
578 struct lio_vf_rep_desc *vf_rep;
579 struct net_device *ndev;
582 if (oct->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
585 if (!oct->sriov_info.sriov_enabled)
588 for (i = 0; i < oct->vf_rep_list.num_vfs; i++) {
589 ndev = oct->vf_rep_list.ndev[i];
590 oct->vf_rep_list.ndev[i] = NULL;
592 vf_rep = netdev_priv(ndev);
593 cancel_delayed_work_sync
594 (&vf_rep->stats_wk.work);
595 netif_tx_disable(ndev);
596 netif_carrier_off(ndev);
598 unregister_netdev(ndev);
603 oct->vf_rep_list.num_vfs = 0;
607 lio_vf_rep_netdev_event(struct notifier_block *nb,
608 unsigned long event, void *ptr)
610 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
611 struct lio_vf_rep_desc *vf_rep;
612 struct lio_vf_rep_req rep_cfg;
613 struct octeon_device *oct;
617 case NETDEV_REGISTER:
618 case NETDEV_CHANGENAME:
625 if (ndev->netdev_ops != &lio_vf_rep_ndev_ops)
628 vf_rep = netdev_priv(ndev);
631 if (strlen(ndev->name) > LIO_IF_NAME_SIZE) {
632 dev_err(&oct->pci_dev->dev,
633 "Device name change sync failed as the size is > %d\n",
638 memset(&rep_cfg, 0, sizeof(rep_cfg));
639 rep_cfg.req_type = LIO_VF_REP_REQ_DEVNAME;
640 rep_cfg.ifidx = vf_rep->ifidx;
641 strncpy(rep_cfg.rep_name.name, ndev->name, LIO_IF_NAME_SIZE);
643 ret = lio_vf_rep_send_soft_command(oct, &rep_cfg,
644 sizeof(rep_cfg), NULL, 0);
646 dev_err(&oct->pci_dev->dev,
647 "vf_rep netdev name change failed with err %d\n", ret);
652 static struct notifier_block lio_vf_rep_netdev_notifier = {
653 .notifier_call = lio_vf_rep_netdev_event,
657 lio_vf_rep_modinit(void)
659 if (register_netdevice_notifier(&lio_vf_rep_netdev_notifier)) {
660 pr_err("netdev notifier registration failed\n");
668 lio_vf_rep_modexit(void)
670 if (unregister_netdevice_notifier(&lio_vf_rep_netdev_notifier))
671 pr_err("netdev notifier unregister failed\n");