Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (c) 2014-2015 Hisilicon Limited.
0004  */
0005 
0006 #include <linux/etherdevice.h>
0007 #include <linux/netdevice.h>
0008 #include <linux/spinlock.h>
0009 
0010 #include "hnae.h"
0011 #include "hns_dsaf_mac.h"
0012 #include "hns_dsaf_main.h"
0013 #include "hns_dsaf_ppe.h"
0014 #include "hns_dsaf_rcb.h"
0015 
0016 static struct hns_mac_cb *hns_get_mac_cb(struct hnae_handle *handle)
0017 {
0018     struct  hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
0019 
0020     return vf_cb->mac_cb;
0021 }
0022 
0023 static struct dsaf_device *hns_ae_get_dsaf_dev(struct hnae_ae_dev *dev)
0024 {
0025     return container_of(dev, struct dsaf_device, ae_dev);
0026 }
0027 
0028 static struct hns_ppe_cb *hns_get_ppe_cb(struct hnae_handle *handle)
0029 {
0030     int ppe_index;
0031     struct ppe_common_cb *ppe_comm;
0032     struct  hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
0033 
0034     ppe_comm = vf_cb->dsaf_dev->ppe_common[0];
0035     ppe_index = vf_cb->port_index;
0036 
0037     return &ppe_comm->ppe_cb[ppe_index];
0038 }
0039 
0040 static int hns_ae_get_q_num_per_vf(
0041     struct dsaf_device *dsaf_dev, int port)
0042 {
0043     return dsaf_dev->rcb_common[0]->max_q_per_vf;
0044 }
0045 
0046 static int hns_ae_get_vf_num_per_port(
0047     struct dsaf_device *dsaf_dev, int port)
0048 {
0049     return dsaf_dev->rcb_common[0]->max_vfn;
0050 }
0051 
0052 static struct ring_pair_cb *hns_ae_get_base_ring_pair(
0053     struct dsaf_device *dsaf_dev, int port)
0054 {
0055     struct rcb_common_cb *rcb_comm = dsaf_dev->rcb_common[0];
0056     int q_num = rcb_comm->max_q_per_vf;
0057     int vf_num = rcb_comm->max_vfn;
0058 
0059     return &rcb_comm->ring_pair_cb[port * q_num * vf_num];
0060 }
0061 
0062 static struct ring_pair_cb *hns_ae_get_ring_pair(struct hnae_queue *q)
0063 {
0064     return container_of(q, struct ring_pair_cb, q);
0065 }
0066 
0067 static struct hnae_handle *hns_ae_get_handle(struct hnae_ae_dev *dev,
0068                          u32 port_id)
0069 {
0070     int vfnum_per_port;
0071     int qnum_per_vf;
0072     int i;
0073     struct dsaf_device *dsaf_dev;
0074     struct hnae_handle *ae_handle;
0075     struct ring_pair_cb *ring_pair_cb;
0076     struct hnae_vf_cb *vf_cb;
0077 
0078     dsaf_dev = hns_ae_get_dsaf_dev(dev);
0079 
0080     ring_pair_cb = hns_ae_get_base_ring_pair(dsaf_dev, port_id);
0081     vfnum_per_port = hns_ae_get_vf_num_per_port(dsaf_dev, port_id);
0082     qnum_per_vf = hns_ae_get_q_num_per_vf(dsaf_dev, port_id);
0083 
0084     vf_cb = kzalloc(struct_size(vf_cb, ae_handle.qs, qnum_per_vf),
0085             GFP_KERNEL);
0086     if (unlikely(!vf_cb)) {
0087         dev_err(dsaf_dev->dev, "malloc vf_cb fail!\n");
0088         ae_handle = ERR_PTR(-ENOMEM);
0089         goto handle_err;
0090     }
0091     ae_handle = &vf_cb->ae_handle;
0092     /* ae_handle Init  */
0093     ae_handle->owner_dev = dsaf_dev->dev;
0094     ae_handle->dev = dev;
0095     ae_handle->q_num = qnum_per_vf;
0096     ae_handle->coal_param = HNAE_LOWEST_LATENCY_COAL_PARAM;
0097 
0098     /* find ring pair, and set vf id*/
0099     for (ae_handle->vf_id = 0;
0100         ae_handle->vf_id < vfnum_per_port; ae_handle->vf_id++) {
0101         if (!ring_pair_cb->used_by_vf)
0102             break;
0103         ring_pair_cb += qnum_per_vf;
0104     }
0105     if (ae_handle->vf_id >= vfnum_per_port) {
0106         dev_err(dsaf_dev->dev, "malloc queue fail!\n");
0107         ae_handle = ERR_PTR(-EINVAL);
0108         goto vf_id_err;
0109     }
0110 
0111     for (i = 0; i < qnum_per_vf; i++) {
0112         ae_handle->qs[i] = &ring_pair_cb->q;
0113         ae_handle->qs[i]->rx_ring.q = ae_handle->qs[i];
0114         ae_handle->qs[i]->tx_ring.q = ae_handle->qs[i];
0115 
0116         ring_pair_cb->used_by_vf = 1;
0117         ring_pair_cb++;
0118     }
0119 
0120     vf_cb->dsaf_dev = dsaf_dev;
0121     vf_cb->port_index = port_id;
0122     vf_cb->mac_cb = dsaf_dev->mac_cb[port_id];
0123 
0124     ae_handle->phy_if = vf_cb->mac_cb->phy_if;
0125     ae_handle->phy_dev = vf_cb->mac_cb->phy_dev;
0126     ae_handle->if_support = vf_cb->mac_cb->if_support;
0127     ae_handle->port_type = vf_cb->mac_cb->mac_type;
0128     ae_handle->media_type = vf_cb->mac_cb->media_type;
0129     ae_handle->dport_id = port_id;
0130 
0131     return ae_handle;
0132 vf_id_err:
0133     kfree(vf_cb);
0134 handle_err:
0135     return ae_handle;
0136 }
0137 
0138 static void hns_ae_put_handle(struct hnae_handle *handle)
0139 {
0140     struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
0141     int i;
0142 
0143     for (i = 0; i < handle->q_num; i++)
0144         hns_ae_get_ring_pair(handle->qs[i])->used_by_vf = 0;
0145 
0146     kfree(vf_cb);
0147 }
0148 
0149 static int hns_ae_wait_flow_down(struct hnae_handle *handle)
0150 {
0151     struct dsaf_device *dsaf_dev;
0152     struct hns_ppe_cb *ppe_cb;
0153     struct hnae_vf_cb *vf_cb;
0154     int ret;
0155     int i;
0156 
0157     for (i = 0; i < handle->q_num; i++) {
0158         ret = hns_rcb_wait_tx_ring_clean(handle->qs[i]);
0159         if (ret)
0160             return ret;
0161     }
0162 
0163     ppe_cb = hns_get_ppe_cb(handle);
0164     ret = hns_ppe_wait_tx_fifo_clean(ppe_cb);
0165     if (ret)
0166         return ret;
0167 
0168     dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
0169     if (!dsaf_dev)
0170         return -EINVAL;
0171     ret = hns_dsaf_wait_pkt_clean(dsaf_dev, handle->dport_id);
0172     if (ret)
0173         return ret;
0174 
0175     vf_cb = hns_ae_get_vf_cb(handle);
0176     ret = hns_mac_wait_fifo_clean(vf_cb->mac_cb);
0177     if (ret)
0178         return ret;
0179 
0180     mdelay(10);
0181     return 0;
0182 }
0183 
0184 static void hns_ae_ring_enable_all(struct hnae_handle *handle, int val)
0185 {
0186     int q_num = handle->q_num;
0187     int i;
0188 
0189     for (i = 0; i < q_num; i++)
0190         hns_rcb_ring_enable_hw(handle->qs[i], val);
0191 }
0192 
0193 static void hns_ae_init_queue(struct hnae_queue *q)
0194 {
0195     struct ring_pair_cb *ring =
0196         container_of(q, struct ring_pair_cb, q);
0197 
0198     hns_rcb_init_hw(ring);
0199 }
0200 
0201 static void hns_ae_fini_queue(struct hnae_queue *q)
0202 {
0203     struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(q->handle);
0204 
0205     if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
0206         hns_rcb_reset_ring_hw(q);
0207 }
0208 
0209 static int hns_ae_set_mac_address(struct hnae_handle *handle, const void *p)
0210 {
0211     int ret;
0212     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0213 
0214     if (!p || !is_valid_ether_addr((const u8 *)p)) {
0215         dev_err(handle->owner_dev, "is not valid ether addr !\n");
0216         return -EADDRNOTAVAIL;
0217     }
0218 
0219     ret = hns_mac_change_vf_addr(mac_cb, handle->vf_id, p);
0220     if (ret != 0) {
0221         dev_err(handle->owner_dev,
0222             "set_mac_address fail, ret=%d!\n", ret);
0223         return ret;
0224     }
0225 
0226     return 0;
0227 }
0228 
0229 static int hns_ae_add_uc_address(struct hnae_handle *handle,
0230                  const unsigned char *addr)
0231 {
0232     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0233 
0234     if (mac_cb->mac_type != HNAE_PORT_SERVICE)
0235         return -ENOSPC;
0236 
0237     return hns_mac_add_uc_addr(mac_cb, handle->vf_id, addr);
0238 }
0239 
0240 static int hns_ae_rm_uc_address(struct hnae_handle *handle,
0241                 const unsigned char *addr)
0242 {
0243     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0244 
0245     if (mac_cb->mac_type != HNAE_PORT_SERVICE)
0246         return -ENOSPC;
0247 
0248     return hns_mac_rm_uc_addr(mac_cb, handle->vf_id, addr);
0249 }
0250 
0251 static int hns_ae_set_multicast_one(struct hnae_handle *handle, void *addr)
0252 {
0253     int ret;
0254     char *mac_addr = (char *)addr;
0255     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0256     u8 port_num;
0257 
0258     assert(mac_cb);
0259 
0260     if (mac_cb->mac_type != HNAE_PORT_SERVICE)
0261         return 0;
0262 
0263     ret = hns_mac_set_multi(mac_cb, mac_cb->mac_id, mac_addr, true);
0264     if (ret) {
0265         dev_err(handle->owner_dev,
0266             "mac add mul_mac:%pM port%d  fail, ret = %#x!\n",
0267             mac_addr, mac_cb->mac_id, ret);
0268         return ret;
0269     }
0270 
0271     ret = hns_mac_get_inner_port_num(mac_cb, handle->vf_id, &port_num);
0272     if (ret)
0273         return ret;
0274 
0275     ret = hns_mac_set_multi(mac_cb, port_num, mac_addr, true);
0276     if (ret)
0277         dev_err(handle->owner_dev,
0278             "mac add mul_mac:%pM port%d  fail, ret = %#x!\n",
0279             mac_addr, DSAF_BASE_INNER_PORT_NUM, ret);
0280 
0281     return ret;
0282 }
0283 
0284 static int hns_ae_clr_multicast(struct hnae_handle *handle)
0285 {
0286     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0287 
0288     if (mac_cb->mac_type != HNAE_PORT_SERVICE)
0289         return 0;
0290 
0291     return hns_mac_clr_multicast(mac_cb, handle->vf_id);
0292 }
0293 
0294 static int hns_ae_set_mtu(struct hnae_handle *handle, int new_mtu)
0295 {
0296     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0297     struct hnae_queue *q;
0298     u32 rx_buf_size;
0299     int i, ret;
0300 
0301     /* when buf_size is 2048, max mtu is 6K for rx ring max bd num is 3. */
0302     if (!AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver)) {
0303         if (new_mtu <= BD_SIZE_2048_MAX_MTU)
0304             rx_buf_size = 2048;
0305         else
0306             rx_buf_size = 4096;
0307     } else {
0308         rx_buf_size = mac_cb->dsaf_dev->buf_size;
0309     }
0310 
0311     ret = hns_mac_set_mtu(mac_cb, new_mtu, rx_buf_size);
0312 
0313     if (!ret) {
0314         /* reinit ring buf_size */
0315         for (i = 0; i < handle->q_num; i++) {
0316             q = handle->qs[i];
0317             q->rx_ring.buf_size = rx_buf_size;
0318             hns_rcb_set_rx_ring_bs(q, rx_buf_size);
0319         }
0320     }
0321 
0322     return ret;
0323 }
0324 
0325 static void hns_ae_set_tso_stats(struct hnae_handle *handle, int enable)
0326 {
0327     struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
0328 
0329     hns_ppe_set_tso_enable(ppe_cb, enable);
0330 }
0331 
0332 static int hns_ae_start(struct hnae_handle *handle)
0333 {
0334     int ret;
0335     int k;
0336     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0337 
0338     ret = hns_mac_vm_config_bc_en(mac_cb, 0, true);
0339     if (ret)
0340         return ret;
0341 
0342     for (k = 0; k < handle->q_num; k++) {
0343         if (AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver))
0344             hns_rcb_int_clr_hw(handle->qs[k],
0345                        RCB_INT_FLAG_TX | RCB_INT_FLAG_RX);
0346         else
0347             hns_rcbv2_int_clr_hw(handle->qs[k],
0348                          RCB_INT_FLAG_TX | RCB_INT_FLAG_RX);
0349     }
0350     hns_ae_ring_enable_all(handle, 1);
0351     msleep(100);
0352 
0353     hns_mac_start(mac_cb);
0354 
0355     return 0;
0356 }
0357 
0358 static void hns_ae_stop(struct hnae_handle *handle)
0359 {
0360     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0361 
0362     /* just clean tx fbd, neednot rx fbd*/
0363     hns_rcb_wait_fbd_clean(handle->qs, handle->q_num, RCB_INT_FLAG_TX);
0364 
0365     msleep(20);
0366 
0367     hns_mac_stop(mac_cb);
0368 
0369     usleep_range(10000, 20000);
0370 
0371     hns_ae_ring_enable_all(handle, 0);
0372 
0373     /* clean rx fbd. */
0374     hns_rcb_wait_fbd_clean(handle->qs, handle->q_num, RCB_INT_FLAG_RX);
0375 
0376     (void)hns_mac_vm_config_bc_en(mac_cb, 0, false);
0377 }
0378 
0379 static void hns_ae_reset(struct hnae_handle *handle)
0380 {
0381     struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
0382 
0383     if (vf_cb->mac_cb->mac_type == HNAE_PORT_DEBUG) {
0384         hns_mac_reset(vf_cb->mac_cb);
0385         hns_ppe_reset_common(vf_cb->dsaf_dev, 0);
0386     }
0387 }
0388 
0389 static void hns_ae_toggle_ring_irq(struct hnae_ring *ring, u32 mask)
0390 {
0391     u32 flag;
0392 
0393     if (is_tx_ring(ring))
0394         flag = RCB_INT_FLAG_TX;
0395     else
0396         flag = RCB_INT_FLAG_RX;
0397 
0398     hns_rcb_int_ctrl_hw(ring->q, flag, mask);
0399 }
0400 
0401 static void hns_aev2_toggle_ring_irq(struct hnae_ring *ring, u32 mask)
0402 {
0403     u32 flag;
0404 
0405     if (is_tx_ring(ring))
0406         flag = RCB_INT_FLAG_TX;
0407     else
0408         flag = RCB_INT_FLAG_RX;
0409 
0410     hns_rcbv2_int_ctrl_hw(ring->q, flag, mask);
0411 }
0412 
0413 static int hns_ae_get_link_status(struct hnae_handle *handle)
0414 {
0415     u32 link_status;
0416     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0417 
0418     hns_mac_get_link_status(mac_cb, &link_status);
0419 
0420     return !!link_status;
0421 }
0422 
0423 static int hns_ae_get_mac_info(struct hnae_handle *handle,
0424                    u8 *auto_neg, u16 *speed, u8 *duplex)
0425 {
0426     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0427 
0428     return hns_mac_get_port_info(mac_cb, auto_neg, speed, duplex);
0429 }
0430 
0431 static bool hns_ae_need_adjust_link(struct hnae_handle *handle, int speed,
0432                     int duplex)
0433 {
0434     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0435 
0436     return hns_mac_need_adjust_link(mac_cb, speed, duplex);
0437 }
0438 
0439 static void hns_ae_adjust_link(struct hnae_handle *handle, int speed,
0440                    int duplex)
0441 {
0442     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0443 
0444     switch (mac_cb->dsaf_dev->dsaf_ver) {
0445     case AE_VERSION_1:
0446         hns_mac_adjust_link(mac_cb, speed, duplex);
0447         break;
0448 
0449     case AE_VERSION_2:
0450         /* chip need to clear all pkt inside */
0451         hns_mac_disable(mac_cb, MAC_COMM_MODE_RX);
0452         if (hns_ae_wait_flow_down(handle)) {
0453             hns_mac_enable(mac_cb, MAC_COMM_MODE_RX);
0454             break;
0455         }
0456 
0457         hns_mac_adjust_link(mac_cb, speed, duplex);
0458         hns_mac_enable(mac_cb, MAC_COMM_MODE_RX);
0459         break;
0460 
0461     default:
0462         break;
0463     }
0464 }
0465 
0466 static void hns_ae_get_ring_bdnum_limit(struct hnae_queue *queue,
0467                     u32 *uplimit)
0468 {
0469     *uplimit = HNS_RCB_RING_MAX_PENDING_BD;
0470 }
0471 
0472 static void hns_ae_get_pauseparam(struct hnae_handle *handle,
0473                   u32 *auto_neg, u32 *rx_en, u32 *tx_en)
0474 {
0475     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0476     struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
0477 
0478     hns_mac_get_autoneg(mac_cb, auto_neg);
0479 
0480     hns_mac_get_pauseparam(mac_cb, rx_en, tx_en);
0481 
0482     /* Service port's pause feature is provided by DSAF, not mac */
0483     if (handle->port_type == HNAE_PORT_SERVICE)
0484         hns_dsaf_get_rx_mac_pause_en(dsaf_dev, mac_cb->mac_id, rx_en);
0485 }
0486 
0487 static void hns_ae_set_promisc_mode(struct hnae_handle *handle, u32 en)
0488 {
0489     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0490 
0491     hns_dsaf_set_promisc_mode(hns_ae_get_dsaf_dev(handle->dev), en);
0492     hns_mac_set_promisc(mac_cb, (u8)!!en);
0493 }
0494 
0495 static int hns_ae_set_pauseparam(struct hnae_handle *handle,
0496                  u32 autoneg, u32 rx_en, u32 tx_en)
0497 {
0498     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0499     struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
0500     int ret;
0501 
0502     ret = hns_mac_set_autoneg(mac_cb, autoneg);
0503     if (ret)
0504         return ret;
0505 
0506     /* Service port's pause feature is provided by DSAF, not mac */
0507     if (handle->port_type == HNAE_PORT_SERVICE) {
0508         ret = hns_dsaf_set_rx_mac_pause_en(dsaf_dev,
0509                            mac_cb->mac_id, rx_en);
0510         if (ret)
0511             return ret;
0512         rx_en = 0;
0513     }
0514     return hns_mac_set_pauseparam(mac_cb, rx_en, tx_en);
0515 }
0516 
0517 static void hns_ae_get_coalesce_usecs(struct hnae_handle *handle,
0518                       u32 *tx_usecs, u32 *rx_usecs)
0519 {
0520     struct ring_pair_cb *ring_pair =
0521         container_of(handle->qs[0], struct ring_pair_cb, q);
0522 
0523     *tx_usecs = hns_rcb_get_coalesce_usecs(ring_pair->rcb_common,
0524                            ring_pair->port_id_in_comm);
0525     *rx_usecs = hns_rcb_get_coalesce_usecs(ring_pair->rcb_common,
0526                            ring_pair->port_id_in_comm);
0527 }
0528 
0529 static void hns_ae_get_max_coalesced_frames(struct hnae_handle *handle,
0530                         u32 *tx_frames, u32 *rx_frames)
0531 {
0532     struct ring_pair_cb *ring_pair =
0533         container_of(handle->qs[0], struct ring_pair_cb, q);
0534     struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
0535 
0536     if (AE_IS_VER1(dsaf_dev->dsaf_ver) ||
0537         handle->port_type == HNAE_PORT_DEBUG)
0538         *tx_frames = hns_rcb_get_rx_coalesced_frames(
0539             ring_pair->rcb_common, ring_pair->port_id_in_comm);
0540     else
0541         *tx_frames = hns_rcb_get_tx_coalesced_frames(
0542             ring_pair->rcb_common, ring_pair->port_id_in_comm);
0543     *rx_frames = hns_rcb_get_rx_coalesced_frames(ring_pair->rcb_common,
0544                           ring_pair->port_id_in_comm);
0545 }
0546 
0547 static int hns_ae_set_coalesce_usecs(struct hnae_handle *handle,
0548                      u32 timeout)
0549 {
0550     struct ring_pair_cb *ring_pair =
0551         container_of(handle->qs[0], struct ring_pair_cb, q);
0552 
0553     return hns_rcb_set_coalesce_usecs(
0554         ring_pair->rcb_common, ring_pair->port_id_in_comm, timeout);
0555 }
0556 
0557 static int hns_ae_set_coalesce_frames(struct hnae_handle *handle,
0558                       u32 tx_frames, u32 rx_frames)
0559 {
0560     int ret;
0561     struct ring_pair_cb *ring_pair =
0562         container_of(handle->qs[0], struct ring_pair_cb, q);
0563     struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
0564 
0565     if (AE_IS_VER1(dsaf_dev->dsaf_ver) ||
0566         handle->port_type == HNAE_PORT_DEBUG) {
0567         if (tx_frames != rx_frames)
0568             return -EINVAL;
0569         return hns_rcb_set_rx_coalesced_frames(
0570             ring_pair->rcb_common,
0571             ring_pair->port_id_in_comm, rx_frames);
0572     } else {
0573         if (tx_frames != 1)
0574             return -EINVAL;
0575         ret = hns_rcb_set_tx_coalesced_frames(
0576             ring_pair->rcb_common,
0577             ring_pair->port_id_in_comm, tx_frames);
0578         if (ret)
0579             return ret;
0580 
0581         return hns_rcb_set_rx_coalesced_frames(
0582             ring_pair->rcb_common,
0583             ring_pair->port_id_in_comm, rx_frames);
0584     }
0585 }
0586 
0587 static void hns_ae_get_coalesce_range(struct hnae_handle *handle,
0588                       u32 *tx_frames_low, u32 *rx_frames_low,
0589                       u32 *tx_frames_high, u32 *rx_frames_high,
0590                       u32 *tx_usecs_low, u32 *rx_usecs_low,
0591                       u32 *tx_usecs_high, u32 *rx_usecs_high)
0592 {
0593     struct dsaf_device *dsaf_dev;
0594 
0595     assert(handle);
0596 
0597     dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
0598 
0599     *tx_frames_low  = HNS_RCB_TX_FRAMES_LOW;
0600     *rx_frames_low  = HNS_RCB_RX_FRAMES_LOW;
0601 
0602     if (AE_IS_VER1(dsaf_dev->dsaf_ver) ||
0603         handle->port_type == HNAE_PORT_DEBUG)
0604         *tx_frames_high =
0605             (dsaf_dev->desc_num - 1 > HNS_RCB_TX_FRAMES_HIGH) ?
0606             HNS_RCB_TX_FRAMES_HIGH : dsaf_dev->desc_num - 1;
0607     else
0608         *tx_frames_high = 1;
0609 
0610     *rx_frames_high = (dsaf_dev->desc_num - 1 > HNS_RCB_RX_FRAMES_HIGH) ?
0611         HNS_RCB_RX_FRAMES_HIGH : dsaf_dev->desc_num - 1;
0612     *tx_usecs_low   = HNS_RCB_TX_USECS_LOW;
0613     *rx_usecs_low   = HNS_RCB_RX_USECS_LOW;
0614     *tx_usecs_high  = HNS_RCB_TX_USECS_HIGH;
0615     *rx_usecs_high  = HNS_RCB_RX_USECS_HIGH;
0616 }
0617 
0618 static void hns_ae_update_stats(struct hnae_handle *handle,
0619                 struct net_device_stats *net_stats)
0620 {
0621     int port;
0622     int idx;
0623     struct dsaf_device *dsaf_dev;
0624     struct hns_mac_cb *mac_cb;
0625     struct hns_ppe_cb *ppe_cb;
0626     struct hnae_queue *queue;
0627     struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
0628     u64 tx_bytes = 0, rx_bytes = 0, tx_packets = 0, rx_packets = 0;
0629     u64 rx_errors = 0, tx_errors = 0, tx_dropped = 0;
0630     u64 rx_missed_errors;
0631 
0632     dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
0633     if (!dsaf_dev)
0634         return;
0635     port = vf_cb->port_index;
0636     ppe_cb = hns_get_ppe_cb(handle);
0637     mac_cb = hns_get_mac_cb(handle);
0638 
0639     for (idx = 0; idx < handle->q_num; idx++) {
0640         queue = handle->qs[idx];
0641         hns_rcb_update_stats(queue);
0642 
0643         tx_bytes += queue->tx_ring.stats.tx_bytes;
0644         tx_packets += queue->tx_ring.stats.tx_pkts;
0645         rx_bytes += queue->rx_ring.stats.rx_bytes;
0646         rx_packets += queue->rx_ring.stats.rx_pkts;
0647 
0648         rx_errors += queue->rx_ring.stats.err_pkt_len
0649                 + queue->rx_ring.stats.l2_err
0650                 + queue->rx_ring.stats.l3l4_csum_err;
0651     }
0652 
0653     hns_ppe_update_stats(ppe_cb);
0654     rx_missed_errors = ppe_cb->hw_stats.rx_drop_no_buf;
0655     tx_errors += ppe_cb->hw_stats.tx_err_checksum
0656         + ppe_cb->hw_stats.tx_err_fifo_empty;
0657 
0658     if (mac_cb->mac_type == HNAE_PORT_SERVICE) {
0659         hns_dsaf_update_stats(dsaf_dev, port);
0660         /* for port upline direction, i.e., rx. */
0661         rx_missed_errors += dsaf_dev->hw_stats[port].bp_drop;
0662         rx_missed_errors += dsaf_dev->hw_stats[port].pad_drop;
0663         rx_missed_errors += dsaf_dev->hw_stats[port].crc_false;
0664 
0665         /* for port downline direction, i.e., tx. */
0666         port = port + DSAF_PPE_INODE_BASE;
0667         hns_dsaf_update_stats(dsaf_dev, port);
0668         tx_dropped += dsaf_dev->hw_stats[port].bp_drop;
0669         tx_dropped += dsaf_dev->hw_stats[port].pad_drop;
0670         tx_dropped += dsaf_dev->hw_stats[port].crc_false;
0671         tx_dropped += dsaf_dev->hw_stats[port].rslt_drop;
0672         tx_dropped += dsaf_dev->hw_stats[port].vlan_drop;
0673         tx_dropped += dsaf_dev->hw_stats[port].stp_drop;
0674     }
0675 
0676     hns_mac_update_stats(mac_cb);
0677     rx_errors += mac_cb->hw_stats.rx_fifo_overrun_err;
0678 
0679     tx_errors += mac_cb->hw_stats.tx_bad_pkts
0680         + mac_cb->hw_stats.tx_fragment_err
0681         + mac_cb->hw_stats.tx_jabber_err
0682         + mac_cb->hw_stats.tx_underrun_err
0683         + mac_cb->hw_stats.tx_crc_err;
0684 
0685     net_stats->tx_bytes = tx_bytes;
0686     net_stats->tx_packets = tx_packets;
0687     net_stats->rx_bytes = rx_bytes;
0688     net_stats->rx_dropped = 0;
0689     net_stats->rx_packets = rx_packets;
0690     net_stats->rx_errors = rx_errors;
0691     net_stats->tx_errors = tx_errors;
0692     net_stats->tx_dropped = tx_dropped;
0693     net_stats->rx_missed_errors = rx_missed_errors;
0694     net_stats->rx_crc_errors = mac_cb->hw_stats.rx_fcs_err;
0695     net_stats->rx_frame_errors = mac_cb->hw_stats.rx_align_err;
0696     net_stats->rx_fifo_errors = mac_cb->hw_stats.rx_fifo_overrun_err;
0697     net_stats->rx_length_errors = mac_cb->hw_stats.rx_len_err;
0698     net_stats->multicast = mac_cb->hw_stats.rx_mc_pkts;
0699 }
0700 
0701 static void hns_ae_get_stats(struct hnae_handle *handle, u64 *data)
0702 {
0703     int idx;
0704     struct hns_mac_cb *mac_cb;
0705     struct hns_ppe_cb *ppe_cb;
0706     u64 *p = data;
0707     struct  hnae_vf_cb *vf_cb;
0708 
0709     if (!handle || !data) {
0710         pr_err("hns_ae_get_stats NULL handle or data pointer!\n");
0711         return;
0712     }
0713 
0714     vf_cb = hns_ae_get_vf_cb(handle);
0715     mac_cb = hns_get_mac_cb(handle);
0716     ppe_cb = hns_get_ppe_cb(handle);
0717 
0718     for (idx = 0; idx < handle->q_num; idx++) {
0719         hns_rcb_get_stats(handle->qs[idx], p);
0720         p += hns_rcb_get_ring_sset_count((int)ETH_SS_STATS);
0721     }
0722 
0723     hns_ppe_get_stats(ppe_cb, p);
0724     p += hns_ppe_get_sset_count((int)ETH_SS_STATS);
0725 
0726     hns_mac_get_stats(mac_cb, p);
0727     p += hns_mac_get_sset_count(mac_cb, (int)ETH_SS_STATS);
0728 
0729     if (mac_cb->mac_type == HNAE_PORT_SERVICE)
0730         hns_dsaf_get_stats(vf_cb->dsaf_dev, p, vf_cb->port_index);
0731 }
0732 
0733 static void hns_ae_get_strings(struct hnae_handle *handle,
0734                    u32 stringset, u8 *data)
0735 {
0736     int port;
0737     int idx;
0738     struct hns_mac_cb *mac_cb;
0739     struct hns_ppe_cb *ppe_cb;
0740     struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
0741     u8 *p = data;
0742     struct  hnae_vf_cb *vf_cb;
0743 
0744     assert(handle);
0745 
0746     vf_cb = hns_ae_get_vf_cb(handle);
0747     port = vf_cb->port_index;
0748     mac_cb = hns_get_mac_cb(handle);
0749     ppe_cb = hns_get_ppe_cb(handle);
0750 
0751     for (idx = 0; idx < handle->q_num; idx++) {
0752         hns_rcb_get_strings(stringset, p, idx);
0753         p += ETH_GSTRING_LEN * hns_rcb_get_ring_sset_count(stringset);
0754     }
0755 
0756     hns_ppe_get_strings(ppe_cb, stringset, p);
0757     p += ETH_GSTRING_LEN * hns_ppe_get_sset_count(stringset);
0758 
0759     hns_mac_get_strings(mac_cb, stringset, p);
0760     p += ETH_GSTRING_LEN * hns_mac_get_sset_count(mac_cb, stringset);
0761 
0762     if (mac_cb->mac_type == HNAE_PORT_SERVICE)
0763         hns_dsaf_get_strings(stringset, p, port, dsaf_dev);
0764 }
0765 
0766 static int hns_ae_get_sset_count(struct hnae_handle *handle, int stringset)
0767 {
0768     u32 sset_count = 0;
0769     struct hns_mac_cb *mac_cb;
0770     struct dsaf_device *dsaf_dev = hns_ae_get_dsaf_dev(handle->dev);
0771 
0772     assert(handle);
0773 
0774     mac_cb = hns_get_mac_cb(handle);
0775 
0776     sset_count += hns_rcb_get_ring_sset_count(stringset) * handle->q_num;
0777     sset_count += hns_ppe_get_sset_count(stringset);
0778     sset_count += hns_mac_get_sset_count(mac_cb, stringset);
0779 
0780     if (mac_cb->mac_type == HNAE_PORT_SERVICE)
0781         sset_count += hns_dsaf_get_sset_count(dsaf_dev, stringset);
0782 
0783     return sset_count;
0784 }
0785 
0786 static int hns_ae_config_loopback(struct hnae_handle *handle,
0787                   enum hnae_loop loop, int en)
0788 {
0789     int ret;
0790     struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
0791     struct hns_mac_cb *mac_cb = hns_get_mac_cb(handle);
0792     struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev;
0793 
0794     switch (loop) {
0795     case MAC_INTERNALLOOP_PHY:
0796         ret = 0;
0797         break;
0798     case MAC_INTERNALLOOP_SERDES:
0799         ret = dsaf_dev->misc_op->cfg_serdes_loopback(vf_cb->mac_cb,
0800                                  !!en);
0801         break;
0802     case MAC_INTERNALLOOP_MAC:
0803         ret = hns_mac_config_mac_loopback(vf_cb->mac_cb, loop, en);
0804         break;
0805     default:
0806         ret = -EINVAL;
0807     }
0808 
0809     return ret;
0810 }
0811 
0812 static void hns_ae_update_led_status(struct hnae_handle *handle)
0813 {
0814     struct hns_mac_cb *mac_cb;
0815 
0816     assert(handle);
0817     mac_cb = hns_get_mac_cb(handle);
0818     if (mac_cb->media_type != HNAE_MEDIA_TYPE_FIBER)
0819         return;
0820 
0821     hns_set_led_opt(mac_cb);
0822 }
0823 
0824 static int hns_ae_cpld_set_led_id(struct hnae_handle *handle,
0825                   enum hnae_led_state status)
0826 {
0827     struct hns_mac_cb *mac_cb;
0828 
0829     assert(handle);
0830 
0831     mac_cb = hns_get_mac_cb(handle);
0832 
0833     return hns_cpld_led_set_id(mac_cb, status);
0834 }
0835 
0836 static void hns_ae_get_regs(struct hnae_handle *handle, void *data)
0837 {
0838     u32 *p = data;
0839     int i;
0840     struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
0841     struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
0842 
0843     hns_ppe_get_regs(ppe_cb, p);
0844     p += hns_ppe_get_regs_count();
0845 
0846     hns_rcb_get_common_regs(vf_cb->dsaf_dev->rcb_common[0], p);
0847     p += hns_rcb_get_common_regs_count();
0848 
0849     for (i = 0; i < handle->q_num; i++) {
0850         hns_rcb_get_ring_regs(handle->qs[i], p);
0851         p += hns_rcb_get_ring_regs_count();
0852     }
0853 
0854     hns_mac_get_regs(vf_cb->mac_cb, p);
0855     p += hns_mac_get_regs_count(vf_cb->mac_cb);
0856 
0857     if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
0858         hns_dsaf_get_regs(vf_cb->dsaf_dev, vf_cb->port_index, p);
0859 }
0860 
0861 static int hns_ae_get_regs_len(struct hnae_handle *handle)
0862 {
0863     u32 total_num;
0864     struct hnae_vf_cb *vf_cb = hns_ae_get_vf_cb(handle);
0865 
0866     total_num = hns_ppe_get_regs_count();
0867     total_num += hns_rcb_get_common_regs_count();
0868     total_num += hns_rcb_get_ring_regs_count() * handle->q_num;
0869     total_num += hns_mac_get_regs_count(vf_cb->mac_cb);
0870 
0871     if (vf_cb->mac_cb->mac_type == HNAE_PORT_SERVICE)
0872         total_num += hns_dsaf_get_regs_count();
0873 
0874     return total_num;
0875 }
0876 
0877 static u32 hns_ae_get_rss_key_size(struct hnae_handle *handle)
0878 {
0879     return HNS_PPEV2_RSS_KEY_SIZE;
0880 }
0881 
0882 static u32 hns_ae_get_rss_indir_size(struct hnae_handle *handle)
0883 {
0884     return HNS_PPEV2_RSS_IND_TBL_SIZE;
0885 }
0886 
0887 static int hns_ae_get_rss(struct hnae_handle *handle, u32 *indir, u8 *key,
0888               u8 *hfunc)
0889 {
0890     struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
0891 
0892     /* currently we support only one type of hash function i.e. Toep hash */
0893     if (hfunc)
0894         *hfunc = ETH_RSS_HASH_TOP;
0895 
0896     /* get the RSS Key required by the user */
0897     if (key)
0898         memcpy(key, ppe_cb->rss_key, HNS_PPEV2_RSS_KEY_SIZE);
0899 
0900     /* update the current hash->queue mappings from the shadow RSS table */
0901     if (indir)
0902         memcpy(indir, ppe_cb->rss_indir_table,
0903                HNS_PPEV2_RSS_IND_TBL_SIZE  * sizeof(*indir));
0904 
0905     return 0;
0906 }
0907 
0908 static int hns_ae_set_rss(struct hnae_handle *handle, const u32 *indir,
0909               const u8 *key, const u8 hfunc)
0910 {
0911     struct hns_ppe_cb *ppe_cb = hns_get_ppe_cb(handle);
0912 
0913     /* set the RSS Hash Key if specififed by the user */
0914     if (key) {
0915         memcpy(ppe_cb->rss_key, key, HNS_PPEV2_RSS_KEY_SIZE);
0916         hns_ppe_set_rss_key(ppe_cb, ppe_cb->rss_key);
0917     }
0918 
0919     if (indir) {
0920         /* update the shadow RSS table with user specified qids */
0921         memcpy(ppe_cb->rss_indir_table, indir,
0922                HNS_PPEV2_RSS_IND_TBL_SIZE  * sizeof(*indir));
0923 
0924         /* now update the hardware */
0925         hns_ppe_set_indir_table(ppe_cb, ppe_cb->rss_indir_table);
0926     }
0927 
0928     return 0;
0929 }
0930 
0931 static struct hnae_ae_ops hns_dsaf_ops = {
0932     .get_handle = hns_ae_get_handle,
0933     .put_handle = hns_ae_put_handle,
0934     .init_queue = hns_ae_init_queue,
0935     .fini_queue = hns_ae_fini_queue,
0936     .start = hns_ae_start,
0937     .stop = hns_ae_stop,
0938     .reset = hns_ae_reset,
0939     .toggle_ring_irq = hns_ae_toggle_ring_irq,
0940     .get_status = hns_ae_get_link_status,
0941     .get_info = hns_ae_get_mac_info,
0942     .adjust_link = hns_ae_adjust_link,
0943     .need_adjust_link = hns_ae_need_adjust_link,
0944     .set_loopback = hns_ae_config_loopback,
0945     .get_ring_bdnum_limit = hns_ae_get_ring_bdnum_limit,
0946     .get_pauseparam = hns_ae_get_pauseparam,
0947     .set_pauseparam = hns_ae_set_pauseparam,
0948     .get_coalesce_usecs = hns_ae_get_coalesce_usecs,
0949     .get_max_coalesced_frames = hns_ae_get_max_coalesced_frames,
0950     .set_coalesce_usecs = hns_ae_set_coalesce_usecs,
0951     .set_coalesce_frames = hns_ae_set_coalesce_frames,
0952     .get_coalesce_range = hns_ae_get_coalesce_range,
0953     .set_promisc_mode = hns_ae_set_promisc_mode,
0954     .set_mac_addr = hns_ae_set_mac_address,
0955     .add_uc_addr = hns_ae_add_uc_address,
0956     .rm_uc_addr = hns_ae_rm_uc_address,
0957     .set_mc_addr = hns_ae_set_multicast_one,
0958     .clr_mc_addr = hns_ae_clr_multicast,
0959     .set_mtu = hns_ae_set_mtu,
0960     .update_stats = hns_ae_update_stats,
0961     .set_tso_stats = hns_ae_set_tso_stats,
0962     .get_stats = hns_ae_get_stats,
0963     .get_strings = hns_ae_get_strings,
0964     .get_sset_count = hns_ae_get_sset_count,
0965     .update_led_status = hns_ae_update_led_status,
0966     .set_led_id = hns_ae_cpld_set_led_id,
0967     .get_regs = hns_ae_get_regs,
0968     .get_regs_len = hns_ae_get_regs_len,
0969     .get_rss_key_size = hns_ae_get_rss_key_size,
0970     .get_rss_indir_size = hns_ae_get_rss_indir_size,
0971     .get_rss = hns_ae_get_rss,
0972     .set_rss = hns_ae_set_rss
0973 };
0974 
0975 int hns_dsaf_ae_init(struct dsaf_device *dsaf_dev)
0976 {
0977     struct hnae_ae_dev *ae_dev = &dsaf_dev->ae_dev;
0978     static atomic_t id = ATOMIC_INIT(-1);
0979 
0980     switch (dsaf_dev->dsaf_ver) {
0981     case AE_VERSION_1:
0982         hns_dsaf_ops.toggle_ring_irq = hns_ae_toggle_ring_irq;
0983         break;
0984     case AE_VERSION_2:
0985         hns_dsaf_ops.toggle_ring_irq = hns_aev2_toggle_ring_irq;
0986         break;
0987     default:
0988         break;
0989     }
0990 
0991     snprintf(ae_dev->name, AE_NAME_SIZE, "%s%d", DSAF_DEVICE_NAME,
0992          (int)atomic_inc_return(&id));
0993     ae_dev->ops = &hns_dsaf_ops;
0994     ae_dev->dev = dsaf_dev->dev;
0995 
0996     return hnae_ae_register(ae_dev, THIS_MODULE);
0997 }
0998 
0999 void hns_dsaf_ae_uninit(struct dsaf_device *dsaf_dev)
1000 {
1001     hnae_ae_unregister(&dsaf_dev->ae_dev);
1002 }