linux/drivers/net/ethernet/hisilicon/hns/hns_ae_adapt.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2014-2015 Hisilicon Limited.
   3 *
   4 * This program is free software; you can redistribute it and/or modify
   5 * it under the terms of the GNU General Public License as published by
   6 * the Free Software Foundation; either version 2 of the License, or
   7 * (at your option) any later version.
   8 */
   9
  10#include <linux/etherdevice.h>
  11#include <linux/netdevice.h>
  12#include <linux/spinlock.h>
  13
  14#include "hnae.h"
  15#include "hns_dsaf_mac.h"
  16#include "hns_dsaf_main.h"
  17#include "hns_dsaf_ppe.h"
  18#include "hns_dsaf_rcb.h"
  19
  20#define AE_NAME_PORT_ID_IDX 6
  21#define ETH_STATIC_REG   1
  22#define ETH_DUMP_REG     5
  23#define ETH_GSTRING_LEN 32
  24
  25static struct hns_mac_cb *hns_get_mac_cb(struct hnae_handle *handle)
  26{
  27        struct  hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  28
  29        return vf_cb->mac_cb;
  30}
  31
  32/**
  33 * hns_ae_map_eport_to_dport - translate enet port id to dsaf port id
  34 * @port_id: enet port id
  35 *: debug port 0-1, service port 2 -7 (dsaf mode only 2)
  36 * return: dsaf port id
  37 *: service ports 0 - 5, debug port 6-7
  38 **/
  39static int hns_ae_map_eport_to_dport(u32 port_id)
  40{
  41        int port_index;
  42
  43        if (port_id < DSAF_DEBUG_NW_NUM)
  44                port_index = port_id + DSAF_SERVICE_PORT_NUM_PER_DSAF;
  45        else
  46                port_index = port_id - DSAF_DEBUG_NW_NUM;
  47
  48        return port_index;
  49}
  50
  51static struct dsaf_device *hns_ae_get_dsaf_dev(struct hnae_ae_dev *dev)
  52{
  53        return container_of(dev, struct dsaf_device, ae_dev);
  54}
  55
  56static struct hns_ppe_cb *hns_get_ppe_cb(struct hnae_handle *handle)
  57{
  58        int ppe_index;
  59        int ppe_common_index;
  60        struct ppe_common_cb *ppe_comm;
  61        struct  hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
  62
  63        if (vf_cb->port_index < DSAF_SERVICE_PORT_NUM_PER_DSAF) {
  64                ppe_index = vf_cb->port_index;
  65                ppe_common_index = 0;
  66        } else {
  67                ppe_index = 0;
  68                ppe_common_index =
  69                        vf_cb->port_index - DSAF_SERVICE_PORT_NUM_PER_DSAF + 1;
  70        }
  71        ppe_comm = vf_cb->dsaf_dev->ppe_common[ppe_common_index];
  72        return &ppe_comm->ppe_cb[ppe_index];
  73}
  74
  75static int hns_ae_get_q_num_per_vf(
  76        struct dsaf_device *dsaf_dev, int port)
  77{
  78        int common_idx = hns_dsaf_get_comm_idx_by_port(port);
  79
  80        return dsaf_dev->rcb_common[common_idx]->max_q_per_vf;
  81}
  82
  83static int hns_ae_get_vf_num_per_port(
  84        struct dsaf_device *dsaf_dev, int port)
  85{
  86        int common_idx = hns_dsaf_get_comm_idx_by_port(port);
  87
  88        return dsaf_dev->rcb_common[common_idx]->max_vfn;
  89}
  90
  91static struct ring_pair_cb *hns_ae_get_base_ring_pair(
  92        struct dsaf_device *dsaf_dev, int port)
  93{
  94        int common_idx = hns_dsaf_get_comm_idx_by_port(port);
  95        struct rcb_common_cb *rcb_comm = dsaf_dev->rcb_common[common_idx];
  96        int q_num = rcb_comm->max_q_per_vf;
  97        int vf_num = rcb_comm->max_vfn;
  98
  99        if (common_idx == HNS_DSAF_COMM_SERVICE_NW_IDX)
 100                return &rcb_comm->ring_pair_cb[port * q_num * vf_num];
 101        else
 102                return &rcb_comm->ring_pair_cb[0];
 103}
 104
 105static struct ring_pair_cb *hns_ae_get_ring_pair(struct hnae_queue *q)
 106{
 107        return container_of(q, struct ring_pair_cb, q);
 108}
 109
 110struct hnae_handle *hns_ae_get_handle(struct hnae_ae_dev *dev,
 111                                      u32 port_id)
 112{
 113        int port_idx;
 114        int vfnum_per_port;
 115        int qnum_per_vf;
 116        int i;
 117        struct dsaf_device *dsaf_dev;
 118        struct hnae_handle *ae_handle;
 119        struct ring_pair_cb *ring_pair_cb;
 120        struct hnae_vf_cb *vf_cb;
 121
 122        dsaf_dev = hns_ae_get_dsaf_dev(dev);
 123        port_idx = hns_ae_map_eport_to_dport(port_id);
 124
 125        ring_pair_cb = hns_ae_get_base_ring_pair(dsaf_dev, port_idx);
 126        vfnum_per_port = hns_ae_get_vf_num_per_port(dsaf_dev, port_idx);
 127        qnum_per_vf = hns_ae_get_q_num_per_vf(dsaf_dev, port_idx);
 128
 129        vf_cb = kzalloc(sizeof(*vf_cb) +
 130                        qnum_per_vf * sizeof(struct hnae_queue *), GFP_KERNEL);
 131        if (unlikely(!vf_cb)) {
 132                dev_err(dsaf_dev->dev, "malloc vf_cb fail!\n");
 133                ae_handle = ERR_PTR(-ENOMEM);
 134                goto handle_err;
 135        }
 136        ae_handle = &vf_cb->ae_handle;
 137        /* ae_handle Init  */
 138        ae_handle->owner_dev = dsaf_dev->dev;
 139        ae_handle->dev = dev;
 140        ae_handle->q_num = qnum_per_vf;
 141
 142        /* find ring pair, and set vf id*/
 143        for (ae_handle->vf_id = 0;
 144                ae_handle->vf_id < vfnum_per_port; ae_handle->vf_id++) {
 145                if (!ring_pair_cb->used_by_vf)
 146                        break;
 147                ring_pair_cb += qnum_per_vf;
 148        }
 149        if (ae_handle->vf_id >= vfnum_per_port) {
 150                dev_err(dsaf_dev->dev, "malloc queue fail!\n");
 151                ae_handle = ERR_PTR(-EINVAL);
 152                goto vf_id_err;
 153        }
 154
 155        ae_handle->qs = (struct hnae_queue **)(&ae_handle->qs + 1);
 156        for (i = 0; i < qnum_per_vf; i++) {
 157                ae_handle->qs[i] = &ring_pair_cb->q;
 158                ae_handle->qs[i]->rx_ring.q = ae_handle->qs[i];
 159                ae_handle->qs[i]->tx_ring.q = ae_handle->qs[i];
 160
 161                ring_pair_cb->used_by_vf = 1;
 162                ring_pair_cb++;
 163        }
 164
 165        vf_cb->dsaf_dev = dsaf_dev;
 166        vf_cb->port_index = port_idx;
 167        vf_cb->mac_cb = &dsaf_dev->mac_cb[port_idx];
 168
 169        ae_handle->phy_if = vf_cb->mac_cb->phy_if;
 170        ae_handle->phy_node = vf_cb->mac_cb->phy_node;
 171        ae_handle->if_support = vf_cb->mac_cb->if_support;
 172        ae_handle->port_type = vf_cb->mac_cb->mac_type;
 173        ae_handle->dport_id = port_idx;
 174
 175        return ae_handle;
 176vf_id_err:
 177        kfree(vf_cb);
 178handle_err:
 179        return ae_handle;
 180}
 181
 182static void hns_ae_put_handle(struct hnae_handle *handle)
 183{
 184        struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
 185        int i;
 186
 187        vf_cb->mac_cb    = NULL;
 188
 189        kfree(vf_cb);
 190
 191        for (i = 0; i < handle->q_num; i++)
 192                hns_ae_get_ring_pair(handle->qs[i])->used_by_vf = 0;
 193}
 194
 195static void hns_ae_ring_enable_all(struct hnae_handle *handle, int val)
 196{
 197        int q_num = handle->q_num;
 198        int i;
 199
 200        for (i = 0; i < q_num; i++)
 201                hns_rcb_ring_enable_hw(handle->qs[i], val);
 202}
 203
 204static void hns_ae_init_queue(struct hnae_queue *q)
 205{
 206        struct ring_pair_cb *ring =
 207                container_of(q, struct ring_pair_cb, q);
 208
 209        hns_rcb_init_hw(ring);
 210}
 211
 212static void hns_ae_fini_queue(struct hnae_queue *q)
 213{
 214        struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(q->handle);
 215
 216        if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
 217                hns_rcb_reset_ring_hw(q);
 218}
 219
 220static int hns_ae_set_mac_address(struct hnae_handle *handle, void *p)
 221{
 222        int ret;
 223        struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
 224
 225        if (!p || !is_valid_ether_addr((const u8 *)p)) {
 226                dev_err(handle->owner_dev, "is not valid ether addr !\n");
 227                return -EADDRNOTAVAIL;
 228        }
 229
 230        ret = hns_mac_change_vf_addr(mac_cb, handle->vf_id, p);
 231        if (ret != 0) {
 232                dev_err(handle->owner_dev,
 233                        "set_mac_address fail, ret=%d!\n", ret);
 234                return ret;
 235        }
 236
 237        return 0;
 238}
 239
 240static int hns_ae_set_multicast_one(struct hnae_handle *handle, void *addr)
 241{
 242        int ret;
 243        char *mac_addr = (char *)addr;
 244        struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
 245
 246        assert(mac_cb);
 247
 248        if (mac_cb->mac_type != HNAE_PORT_SERVICE)
 249                return 0;
 250
 251        ret = hns_mac_set_multi(mac_cb, mac_cb->mac_id, mac_addr, true);
 252        if (ret) {
 253                dev_err(handle->owner_dev,
 254                        "mac add mul_mac:%pM port%d  fail, ret = %#x!\n",
 255                        mac_addr, mac_cb->mac_id, ret);
 256                return ret;
 257        }
 258
 259        ret = hns_mac_set_multi(mac_cb, DSAF_BASE_INNER_PORT_NUM,
 260                                mac_addr, true);
 261        if (ret)
 262                dev_err(handle->owner_dev,
 263                        "mac add mul_mac:%pM port%d  fail, ret = %#x!\n",
 264                        mac_addr, DSAF_BASE_INNER_PORT_NUM, ret);
 265
 266        return ret;
 267}
 268
 269static int hns_ae_set_mtu(struct hnae_handle *handle, int new_mtu)
 270{
 271        struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
 272
 273        return hns_mac_set_mtu(mac_cb, new_mtu);
 274}
 275
 276static void hns_ae_set_tso_stats(struct hnae_handle *handle, int enable)
 277{
 278        struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
 279
 280        hns_ppe_set_tso_enable(ppe_cb, enable);
 281}
 282
 283static int hns_ae_start(struct hnae_handle *handle)
 284{
 285        int ret;
 286        struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
 287
 288        ret = hns_mac_vm_config_bc_en(mac_cb, 0, true);
 289        if (ret)
 290                return ret;
 291
 292        hns_ae_ring_enable_all(handle, 1);
 293        msleep(100);
 294
 295        hns_mac_start(mac_cb);
 296
 297        return 0;
 298}
 299
 300void hns_ae_stop(struct hnae_handle *handle)
 301{
 302        struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
 303
 304        /* just clean tx fbd, neednot rx fbd*/
 305        hns_rcb_wait_fbd_clean(handle->qs, handle->q_num, RCB_INT_FLAG_TX);
 306
 307        msleep(20);
 308
 309        hns_mac_stop(mac_cb);
 310
 311        usleep_range(10000, 20000);
 312
 313        hns_ae_ring_enable_all(handle, 0);
 314
 315        (void)hns_mac_vm_config_bc_en(mac_cb, 0, false);
 316}
 317
 318static void hns_ae_reset(struct hnae_handle *handle)
 319{
 320        struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
 321
 322        if (vf_cb->mac_cb->mac_type == HNAE_PORT_DEBUG) {
 323                u8 ppe_common_index =
 324                        vf_cb->port_index - DSAF_SERVICE_PORT_NUM_PER_DSAF + 1;
 325
 326                hns_mac_reset(vf_cb->mac_cb);
 327                hns_ppe_reset_common(vf_cb->dsaf_dev, ppe_common_index);
 328        }
 329}
 330
 331void hns_ae_toggle_ring_irq(struct hnae_ring *ring, u32 mask)
 332{
 333        u32 flag;
 334
 335        if (is_tx_ring(ring))
 336                flag = RCB_INT_FLAG_TX;
 337        else
 338                flag = RCB_INT_FLAG_RX;
 339
 340        hns_rcb_int_ctrl_hw(ring->q, flag, mask);
 341}
 342
 343static void hns_aev2_toggle_ring_irq(struct hnae_ring *ring, u32 mask)
 344{
 345        u32 flag;
 346
 347        if (is_tx_ring(ring))
 348                flag = RCB_INT_FLAG_TX;
 349        else
 350                flag = RCB_INT_FLAG_RX;
 351
 352        hns_rcbv2_int_ctrl_hw(ring->q, flag, mask);
 353}
 354
 355static void hns_ae_toggle_queue_status(struct hnae_queue *queue, u32 val)
 356{
 357        struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(queue->dev);
 358
 359        if (AE_IS_VER1(dsaf_dev->dsaf_ver))
 360                hns_rcb_int_clr_hw(queue, RCB_INT_FLAG_TX | RCB_INT_FLAG_RX);
 361        else
 362                hns_rcbv2_int_clr_hw(queue, RCB_INT_FLAG_TX | RCB_INT_FLAG_RX);
 363
 364        hns_rcb_start(queue, val);
 365}
 366
 367static int hns_ae_get_link_status(struct hnae_handle *handle)
 368{
 369        u32 link_status;
 370        struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
 371
 372        hns_mac_get_link_status(mac_cb, &link_status);
 373
 374        return !!link_status;
 375}
 376
 377static int hns_ae_get_mac_info(struct hnae_handle *handle,
 378                               u8 *auto_neg, u16 *speed, u8 *duplex)
 379{
 380        struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
 381
 382        return hns_mac_get_port_info(mac_cb, auto_neg, speed, duplex);
 383}
 384
 385static void hns_ae_adjust_link(struct hnae_handle *handle, int speed,
 386                               int duplex)
 387{
 388        struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
 389
 390        hns_mac_adjust_link(mac_cb, speed, duplex);
 391}
 392
 393static void hns_ae_get_ring_bdnum_limit(struct hnae_queue *queue,
 394                                        u32 *uplimit)
 395{
 396        *uplimit = HNS_RCB_RING_MAX_PENDING_BD;
 397}
 398
 399static void hns_ae_get_pauseparam(struct hnae_handle *handle,
 400                                  u32 *auto_neg, u32 *rx_en, u32 *tx_en)
 401{
 402        assert(handle);
 403
 404        hns_mac_get_autoneg(hns_get_mac_cb(handle), auto_neg);
 405
 406        hns_mac_get_pauseparam(hns_get_mac_cb(handle), rx_en, tx_en);
 407}
 408
 409static int hns_ae_set_autoneg(struct hnae_handle *handle, u8 enable)
 410{
 411        assert(handle);
 412
 413        return hns_mac_set_autoneg(hns_get_mac_cb(handle), enable);
 414}
 415
 416static void hns_ae_set_promisc_mode(struct hnae_handle *handle, u32 en)
 417{
 418        struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
 419
 420        hns_dsaf_set_promisc_mode(hns_ae_get_dsaf_dev(handle->dev), en);
 421        hns_mac_set_promisc(mac_cb, (u8)!!en);
 422}
 423
 424static int hns_ae_get_autoneg(struct hnae_handle *handle)
 425{
 426        u32     auto_neg;
 427
 428        assert(handle);
 429
 430        hns_mac_get_autoneg(hns_get_mac_cb(handle), &auto_neg);
 431
 432        return auto_neg;
 433}
 434
 435static int hns_ae_set_pauseparam(struct hnae_handle *handle,
 436                                 u32 autoneg, u32 rx_en, u32 tx_en)
 437{
 438        struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
 439        int ret;
 440
 441        ret = hns_mac_set_autoneg(mac_cb, autoneg);
 442        if (ret)
 443                return ret;
 444
 445        return hns_mac_set_pauseparam(mac_cb, rx_en, tx_en);
 446}
 447
 448static void hns_ae_get_coalesce_usecs(struct hnae_handle *handle,
 449                                      u32 *tx_usecs, u32 *rx_usecs)
 450{
 451        struct ring_pair_cb *ring_pair =
 452                container_of(handle->qs[0], struct ring_pair_cb, q);
 453
 454        *tx_usecs = hns_rcb_get_coalesce_usecs(ring_pair->rcb_common,
 455                                               ring_pair->port_id_in_comm);
 456        *rx_usecs = hns_rcb_get_coalesce_usecs(ring_pair->rcb_common,
 457                                               ring_pair->port_id_in_comm);
 458}
 459
 460static void hns_ae_get_rx_max_coalesced_frames(struct hnae_handle *handle,
 461                                               u32 *tx_frames, u32 *rx_frames)
 462{
 463        struct ring_pair_cb *ring_pair =
 464                container_of(handle->qs[0], struct ring_pair_cb, q);
 465
 466        *tx_frames = hns_rcb_get_coalesced_frames(ring_pair->rcb_common,
 467                                                  ring_pair->port_id_in_comm);
 468        *rx_frames = hns_rcb_get_coalesced_frames(ring_pair->rcb_common,
 469                                                  ring_pair->port_id_in_comm);
 470}
 471
 472static int hns_ae_set_coalesce_usecs(struct hnae_handle *handle,
 473                                     u32 timeout)
 474{
 475        struct ring_pair_cb *ring_pair =
 476                container_of(handle->qs[0], struct ring_pair_cb, q);
 477
 478        return hns_rcb_set_coalesce_usecs(
 479                ring_pair->rcb_common, ring_pair->port_id_in_comm, timeout);
 480}
 481
 482static int  hns_ae_set_coalesce_frames(struct hnae_handle *handle,
 483                                       u32 coalesce_frames)
 484{
 485        struct ring_pair_cb *ring_pair =
 486                container_of(handle->qs[0], struct ring_pair_cb, q);
 487
 488        return hns_rcb_set_coalesced_frames(
 489                ring_pair->rcb_common,
 490                ring_pair->port_id_in_comm, coalesce_frames);
 491}
 492
 493void hns_ae_update_stats(struct hnae_handle *handle,
 494                         struct net_device_stats *net_stats)
 495{
 496        int port;
 497        int idx;
 498        struct dsaf_device *dsaf_dev;
 499        struct hns_mac_cb *mac_cb;
 500        struct hns_ppe_cb *ppe_cb;
 501        struct hnae_queue *queue;
 502        struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
 503        u64 tx_bytes = 0, rx_bytes = 0, tx_packets = 0, rx_packets = 0;
 504        u64 rx_errors = 0, tx_errors = 0, tx_dropped = 0;
 505        u64 rx_missed_errors = 0;
 506
 507        dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
 508        if (!dsaf_dev)
 509                return;
 510        port = vf_cb->port_index;
 511        ppe_cb = hns_get_ppe_cb(handle);
 512        mac_cb = hns_get_mac_cb(handle);
 513
 514        for (idx = 0; idx < handle->q_num; idx++) {
 515                queue = handle->qs[idx];
 516                hns_rcb_update_stats(queue);
 517
 518                tx_bytes += queue->tx_ring.stats.tx_bytes;
 519                tx_packets += queue->tx_ring.stats.tx_pkts;
 520                rx_bytes += queue->rx_ring.stats.rx_bytes;
 521                rx_packets += queue->rx_ring.stats.rx_pkts;
 522
 523                rx_errors += queue->rx_ring.stats.err_pkt_len
 524                                + queue->rx_ring.stats.l2_err
 525                                + queue->rx_ring.stats.l3l4_csum_err;
 526        }
 527
 528        hns_ppe_update_stats(ppe_cb);
 529        rx_missed_errors = ppe_cb->hw_stats.rx_drop_no_buf;
 530        tx_errors += ppe_cb->hw_stats.tx_err_checksum
 531                + ppe_cb->hw_stats.tx_err_fifo_empty;
 532
 533        if (mac_cb->mac_type == HNAE_PORT_SERVICE) {
 534                hns_dsaf_update_stats(dsaf_dev, port);
 535                /* for port upline direction, i.e., rx. */
 536                rx_missed_errors += dsaf_dev->hw_stats[port].bp_drop;
 537                rx_missed_errors += dsaf_dev->hw_stats[port].pad_drop;
 538                rx_missed_errors += dsaf_dev->hw_stats[port].crc_false;
 539
 540                /* for port downline direction, i.e., tx. */
 541                port = port + DSAF_PPE_INODE_BASE;
 542                hns_dsaf_update_stats(dsaf_dev, port);
 543                tx_dropped += dsaf_dev->hw_stats[port].bp_drop;
 544                tx_dropped += dsaf_dev->hw_stats[port].pad_drop;
 545                tx_dropped += dsaf_dev->hw_stats[port].crc_false;
 546                tx_dropped += dsaf_dev->hw_stats[port].rslt_drop;
 547                tx_dropped += dsaf_dev->hw_stats[port].vlan_drop;
 548                tx_dropped += dsaf_dev->hw_stats[port].stp_drop;
 549        }
 550
 551        hns_mac_update_stats(mac_cb);
 552        rx_errors += mac_cb->hw_stats.rx_fifo_overrun_err;
 553
 554        tx_errors += mac_cb->hw_stats.tx_bad_pkts
 555                + mac_cb->hw_stats.tx_fragment_err
 556                + mac_cb->hw_stats.tx_jabber_err
 557                + mac_cb->hw_stats.tx_underrun_err
 558                + mac_cb->hw_stats.tx_crc_err;
 559
 560        net_stats->tx_bytes = tx_bytes;
 561        net_stats->tx_packets = tx_packets;
 562        net_stats->rx_bytes = rx_bytes;
 563        net_stats->rx_dropped = 0;
 564        net_stats->rx_packets = rx_packets;
 565        net_stats->rx_errors = rx_errors;
 566        net_stats->tx_errors = tx_errors;
 567        net_stats->tx_dropped = tx_dropped;
 568        net_stats->rx_missed_errors = rx_missed_errors;
 569        net_stats->rx_crc_errors = mac_cb->hw_stats.rx_fcs_err;
 570        net_stats->rx_frame_errors = mac_cb->hw_stats.rx_align_err;
 571        net_stats->rx_fifo_errors = mac_cb->hw_stats.rx_fifo_overrun_err;
 572        net_stats->rx_length_errors = mac_cb->hw_stats.rx_len_err;
 573        net_stats->multicast = mac_cb->hw_stats.rx_mc_pkts;
 574}
 575
 576void hns_ae_get_stats(struct hnae_handle *handle, u64 *data)
 577{
 578        int idx;
 579        struct hns_mac_cb *mac_cb;
 580        struct hns_ppe_cb *ppe_cb;
 581        u64 *p = data;
 582        struct  hnae_vf_cb *vf_cb;
 583
 584        if (!handle || !data) {
 585                pr_err("hns_ae_get_stats NULL handle or data pointer!\n");
 586                return;
 587        }
 588
 589        vf_cb = hns_ae_get_vf_cb(handle);
 590        mac_cb = hns_get_mac_cb(handle);
 591        ppe_cb = hns_get_ppe_cb(handle);
 592
 593        for (idx = 0; idx < handle->q_num; idx++) {
 594                hns_rcb_get_stats(handle->qs[idx], p);
 595                p += hns_rcb_get_ring_sset_count((int)ETH_SS_STATS);
 596        }
 597
 598        hns_ppe_get_stats(ppe_cb, p);
 599        p += hns_ppe_get_sset_count((int)ETH_SS_STATS);
 600
 601        hns_mac_get_stats(mac_cb, p);
 602        p += hns_mac_get_sset_count(mac_cb, (int)ETH_SS_STATS);
 603
 604        if (mac_cb->mac_type == HNAE_PORT_SERVICE)
 605                hns_dsaf_get_stats(vf_cb->dsaf_dev, p, vf_cb->port_index);
 606}
 607
 608void hns_ae_get_strings(struct hnae_handle *handle,
 609                        u32 stringset, u8 *data)
 610{
 611        int port;
 612        int idx;
 613        struct hns_mac_cb *mac_cb;
 614        struct hns_ppe_cb *ppe_cb;
 615        u8 *p = data;
 616        struct  hnae_vf_cb *vf_cb;
 617
 618        assert(handle);
 619
 620        vf_cb = hns_ae_get_vf_cb(handle);
 621        port = vf_cb->port_index;
 622        mac_cb = hns_get_mac_cb(handle);
 623        ppe_cb = hns_get_ppe_cb(handle);
 624
 625        for (idx = 0; idx < handle->q_num; idx++) {
 626                hns_rcb_get_strings(stringset, p, idx);
 627                p += ETH_GSTRING_LEN * hns_rcb_get_ring_sset_count(stringset);
 628        }
 629
 630        hns_ppe_get_strings(ppe_cb, stringset, p);
 631        p += ETH_GSTRING_LEN * hns_ppe_get_sset_count(stringset);
 632
 633        hns_mac_get_strings(mac_cb, stringset, p);
 634        p += ETH_GSTRING_LEN * hns_mac_get_sset_count(mac_cb, stringset);
 635
 636        if (mac_cb->mac_type == HNAE_PORT_SERVICE)
 637                hns_dsaf_get_strings(stringset, p, port);
 638}
 639
 640int hns_ae_get_sset_count(struct hnae_handle *handle, int stringset)
 641{
 642        u32 sset_count = 0;
 643        struct hns_mac_cb *mac_cb;
 644
 645        assert(handle);
 646
 647        mac_cb = hns_get_mac_cb(handle);
 648
 649        sset_count += hns_rcb_get_ring_sset_count(stringset) * handle->q_num;
 650        sset_count += hns_ppe_get_sset_count(stringset);
 651        sset_count += hns_mac_get_sset_count(mac_cb, stringset);
 652
 653        if (mac_cb->mac_type == HNAE_PORT_SERVICE)
 654                sset_count += hns_dsaf_get_sset_count(stringset);
 655
 656        return sset_count;
 657}
 658
 659static int hns_ae_config_loopback(struct hnae_handle *handle,
 660                                  enum hnae_loop loop, int en)
 661{
 662        int ret;
 663        struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
 664        struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
 665
 666        switch (loop) {
 667        case MAC_INTERNALLOOP_PHY:
 668                ret = 0;
 669                break;
 670        case MAC_INTERNALLOOP_SERDES:
 671                ret = hns_mac_config_sds_loopback(vf_cb->mac_cb, en);
 672                break;
 673        case MAC_INTERNALLOOP_MAC:
 674                ret = hns_mac_config_mac_loopback(vf_cb->mac_cb, loop, en);
 675                break;
 676        default:
 677                ret = -EINVAL;
 678        }
 679
 680        if (!ret)
 681                hns_dsaf_set_inner_lb(mac_cb->dsaf_dev, mac_cb->mac_id, en);
 682
 683        return ret;
 684}
 685
 686void hns_ae_update_led_status(struct hnae_handle *handle)
 687{
 688        struct hns_mac_cb *mac_cb;
 689
 690        assert(handle);
 691        mac_cb = hns_get_mac_cb(handle);
 692        if (!mac_cb->cpld_vaddr)
 693                return;
 694        hns_set_led_opt(mac_cb);
 695}
 696
 697int hns_ae_cpld_set_led_id(struct hnae_handle *handle,
 698                           enum hnae_led_state status)
 699{
 700        struct hns_mac_cb *mac_cb;
 701
 702        assert(handle);
 703
 704        mac_cb = hns_get_mac_cb(handle);
 705
 706        return hns_cpld_led_set_id(mac_cb, status);
 707}
 708
 709void hns_ae_get_regs(struct hnae_handle *handle, void *data)
 710{
 711        u32 *p = data;
 712        u32 rcb_com_idx;
 713        int i;
 714        struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
 715        struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
 716
 717        hns_ppe_get_regs(ppe_cb, p);
 718        p += hns_ppe_get_regs_count();
 719
 720        rcb_com_idx = hns_dsaf_get_comm_idx_by_port(vf_cb->port_index);
 721        hns_rcb_get_common_regs(vf_cb->dsaf_dev->rcb_common[rcb_com_idx], p);
 722        p += hns_rcb_get_common_regs_count();
 723
 724        for (i = 0; i < handle->q_num; i++) {
 725                hns_rcb_get_ring_regs(handle->qs[i], p);
 726                p += hns_rcb_get_ring_regs_count();
 727        }
 728
 729        hns_mac_get_regs(vf_cb->mac_cb, p);
 730        p += hns_mac_get_regs_count(vf_cb->mac_cb);
 731
 732        if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
 733                hns_dsaf_get_regs(vf_cb->dsaf_dev, vf_cb->port_index, p);
 734}
 735
 736int hns_ae_get_regs_len(struct hnae_handle *handle)
 737{
 738        u32 total_num;
 739        struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
 740
 741        total_num = hns_ppe_get_regs_count();
 742        total_num += hns_rcb_get_common_regs_count();
 743        total_num += hns_rcb_get_ring_regs_count() * handle->q_num;
 744        total_num += hns_mac_get_regs_count(vf_cb->mac_cb);
 745
 746        if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
 747                total_num += hns_dsaf_get_regs_count();
 748
 749        return total_num;
 750}
 751
 752static u32 hns_ae_get_rss_key_size(struct hnae_handle *handle)
 753{
 754        return HNS_PPEV2_RSS_KEY_SIZE;
 755}
 756
 757static u32 hns_ae_get_rss_indir_size(struct hnae_handle *handle)
 758{
 759        return HNS_PPEV2_RSS_IND_TBL_SIZE;
 760}
 761
 762static int hns_ae_get_rss(struct hnae_handle *handle, u32 *indir, u8 *key,
 763                          u8 *hfunc)
 764{
 765        struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
 766
 767        /* currently we support only one type of hash function i.e. Toep hash */
 768        if (hfunc)
 769                *hfunc = ETH_RSS_HASH_TOP;
 770
 771        /* get the RSS Key required by the user */
 772        if (key)
 773                memcpy(key, ppe_cb->rss_key, HNS_PPEV2_RSS_KEY_SIZE);
 774
 775        /* update the current hash->queue mappings from the shadow RSS table */
 776        memcpy(indir, ppe_cb->rss_indir_table,
 777               HNS_PPEV2_RSS_IND_TBL_SIZE * sizeof(*indir));
 778
 779        return 0;
 780}
 781
 782static int hns_ae_set_rss(struct hnae_handle *handle, const u32 *indir,
 783                          const u8 *key, const u8 hfunc)
 784{
 785        struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
 786
 787        /* set the RSS Hash Key if specififed by the user */
 788        if (key)
 789                hns_ppe_set_rss_key(ppe_cb, (u32 *)key);
 790
 791        /* update the shadow RSS table with user specified qids */
 792        memcpy(ppe_cb->rss_indir_table, indir,
 793               HNS_PPEV2_RSS_IND_TBL_SIZE * sizeof(*indir));
 794
 795        /* now update the hardware */
 796        hns_ppe_set_indir_table(ppe_cb, ppe_cb->rss_indir_table);
 797
 798        return 0;
 799}
 800
 801static struct hnae_ae_ops hns_dsaf_ops = {
 802        .get_handle = hns_ae_get_handle,
 803        .put_handle = hns_ae_put_handle,
 804        .init_queue = hns_ae_init_queue,
 805        .fini_queue = hns_ae_fini_queue,
 806        .start = hns_ae_start,
 807        .stop = hns_ae_stop,
 808        .reset = hns_ae_reset,
 809        .toggle_ring_irq = hns_ae_toggle_ring_irq,
 810        .toggle_queue_status = hns_ae_toggle_queue_status,
 811        .get_status = hns_ae_get_link_status,
 812        .get_info = hns_ae_get_mac_info,
 813        .adjust_link = hns_ae_adjust_link,
 814        .set_loopback = hns_ae_config_loopback,
 815        .get_ring_bdnum_limit = hns_ae_get_ring_bdnum_limit,
 816        .get_pauseparam = hns_ae_get_pauseparam,
 817        .set_autoneg = hns_ae_set_autoneg,
 818        .get_autoneg = hns_ae_get_autoneg,
 819        .set_pauseparam = hns_ae_set_pauseparam,
 820        .get_coalesce_usecs = hns_ae_get_coalesce_usecs,
 821        .get_rx_max_coalesced_frames = hns_ae_get_rx_max_coalesced_frames,
 822        .set_coalesce_usecs = hns_ae_set_coalesce_usecs,
 823        .set_coalesce_frames = hns_ae_set_coalesce_frames,
 824        .set_promisc_mode = hns_ae_set_promisc_mode,
 825        .set_mac_addr = hns_ae_set_mac_address,
 826        .set_mc_addr = hns_ae_set_multicast_one,
 827        .set_mtu = hns_ae_set_mtu,
 828        .update_stats = hns_ae_update_stats,
 829        .set_tso_stats = hns_ae_set_tso_stats,
 830        .get_stats = hns_ae_get_stats,
 831        .get_strings = hns_ae_get_strings,
 832        .get_sset_count = hns_ae_get_sset_count,
 833        .update_led_status = hns_ae_update_led_status,
 834        .set_led_id = hns_ae_cpld_set_led_id,
 835        .get_regs = hns_ae_get_regs,
 836        .get_regs_len = hns_ae_get_regs_len,
 837        .get_rss_key_size = hns_ae_get_rss_key_size,
 838        .get_rss_indir_size = hns_ae_get_rss_indir_size,
 839        .get_rss = hns_ae_get_rss,
 840        .set_rss = hns_ae_set_rss
 841};
 842
 843int hns_dsaf_ae_init(struct dsaf_device *dsaf_dev)
 844{
 845        struct hnae_ae_dev *ae_dev = &dsaf_dev->ae_dev;
 846        static atomic_t id = ATOMIC_INIT(-1);
 847
 848        switch (dsaf_dev->dsaf_ver) {
 849        case AE_VERSION_1:
 850                hns_dsaf_ops.toggle_ring_irq = hns_ae_toggle_ring_irq;
 851                break;
 852        case AE_VERSION_2:
 853                hns_dsaf_ops.toggle_ring_irq = hns_aev2_toggle_ring_irq;
 854                break;
 855        default:
 856                break;
 857        }
 858
 859        snprintf(ae_dev->name, AE_NAME_SIZE, "%s%d", DSAF_DEVICE_NAME,
 860                 (int)atomic_inc_return(&id));
 861        ae_dev->ops = &hns_dsaf_ops;
 862        ae_dev->dev = dsaf_dev->dev;
 863
 864        return hnae_ae_register(ae_dev, THIS_MODULE);
 865}
 866
 867void hns_dsaf_ae_uninit(struct dsaf_device *dsaf_dev)
 868{
 869        hnae_ae_unregister(&dsaf_dev->ae_dev);
 870}
 871