linux/drivers/net/ethernet/intel/iavf/iavf_virtchnl.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright(c) 2013 - 2018 Intel Corporation. */
   3
   4#include "iavf.h"
   5#include "iavf_prototype.h"
   6#include "iavf_client.h"
   7
   8/* busy wait delay in msec */
   9#define IAVF_BUSY_WAIT_DELAY 10
  10#define IAVF_BUSY_WAIT_COUNT 50
  11
  12/**
  13 * iavf_send_pf_msg
  14 * @adapter: adapter structure
  15 * @op: virtual channel opcode
  16 * @msg: pointer to message buffer
  17 * @len: message length
  18 *
  19 * Send message to PF and print status if failure.
  20 **/
  21static int iavf_send_pf_msg(struct iavf_adapter *adapter,
  22                            enum virtchnl_ops op, u8 *msg, u16 len)
  23{
  24        struct iavf_hw *hw = &adapter->hw;
  25        iavf_status err;
  26
  27        if (adapter->flags & IAVF_FLAG_PF_COMMS_FAILED)
  28                return 0; /* nothing to see here, move along */
  29
  30        err = iavf_aq_send_msg_to_pf(hw, op, 0, msg, len, NULL);
  31        if (err)
  32                dev_dbg(&adapter->pdev->dev, "Unable to send opcode %d to PF, err %s, aq_err %s\n",
  33                        op, iavf_stat_str(hw, err),
  34                        iavf_aq_str(hw, hw->aq.asq_last_status));
  35        return err;
  36}
  37
  38/**
  39 * iavf_send_api_ver
  40 * @adapter: adapter structure
  41 *
  42 * Send API version admin queue message to the PF. The reply is not checked
  43 * in this function. Returns 0 if the message was successfully
  44 * sent, or one of the I40E_ADMIN_QUEUE_ERROR_ statuses if not.
  45 **/
  46int iavf_send_api_ver(struct iavf_adapter *adapter)
  47{
  48        struct virtchnl_version_info vvi;
  49
  50        vvi.major = VIRTCHNL_VERSION_MAJOR;
  51        vvi.minor = VIRTCHNL_VERSION_MINOR;
  52
  53        return iavf_send_pf_msg(adapter, VIRTCHNL_OP_VERSION, (u8 *)&vvi,
  54                                sizeof(vvi));
  55}
  56
  57/**
  58 * iavf_verify_api_ver
  59 * @adapter: adapter structure
  60 *
  61 * Compare API versions with the PF. Must be called after admin queue is
  62 * initialized. Returns 0 if API versions match, -EIO if they do not,
  63 * I40E_ERR_ADMIN_QUEUE_NO_WORK if the admin queue is empty, and any errors
  64 * from the firmware are propagated.
  65 **/
  66int iavf_verify_api_ver(struct iavf_adapter *adapter)
  67{
  68        struct virtchnl_version_info *pf_vvi;
  69        struct iavf_hw *hw = &adapter->hw;
  70        struct i40e_arq_event_info event;
  71        enum virtchnl_ops op;
  72        iavf_status err;
  73
  74        event.buf_len = IAVF_MAX_AQ_BUF_SIZE;
  75        event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  76        if (!event.msg_buf) {
  77                err = -ENOMEM;
  78                goto out;
  79        }
  80
  81        while (1) {
  82                err = iavf_clean_arq_element(hw, &event, NULL);
  83                /* When the AQ is empty, iavf_clean_arq_element will return
  84                 * nonzero and this loop will terminate.
  85                 */
  86                if (err)
  87                        goto out_alloc;
  88                op =
  89                    (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
  90                if (op == VIRTCHNL_OP_VERSION)
  91                        break;
  92        }
  93
  94
  95        err = (iavf_status)le32_to_cpu(event.desc.cookie_low);
  96        if (err)
  97                goto out_alloc;
  98
  99        if (op != VIRTCHNL_OP_VERSION) {
 100                dev_info(&adapter->pdev->dev, "Invalid reply type %d from PF\n",
 101                        op);
 102                err = -EIO;
 103                goto out_alloc;
 104        }
 105
 106        pf_vvi = (struct virtchnl_version_info *)event.msg_buf;
 107        adapter->pf_version = *pf_vvi;
 108
 109        if ((pf_vvi->major > VIRTCHNL_VERSION_MAJOR) ||
 110            ((pf_vvi->major == VIRTCHNL_VERSION_MAJOR) &&
 111             (pf_vvi->minor > VIRTCHNL_VERSION_MINOR)))
 112                err = -EIO;
 113
 114out_alloc:
 115        kfree(event.msg_buf);
 116out:
 117        return err;
 118}
 119
 120/**
 121 * iavf_send_vf_config_msg
 122 * @adapter: adapter structure
 123 *
 124 * Send VF configuration request admin queue message to the PF. The reply
 125 * is not checked in this function. Returns 0 if the message was
 126 * successfully sent, or one of the I40E_ADMIN_QUEUE_ERROR_ statuses if not.
 127 **/
 128int iavf_send_vf_config_msg(struct iavf_adapter *adapter)
 129{
 130        u32 caps;
 131
 132        caps = VIRTCHNL_VF_OFFLOAD_L2 |
 133               VIRTCHNL_VF_OFFLOAD_RSS_PF |
 134               VIRTCHNL_VF_OFFLOAD_RSS_AQ |
 135               VIRTCHNL_VF_OFFLOAD_RSS_REG |
 136               VIRTCHNL_VF_OFFLOAD_VLAN |
 137               VIRTCHNL_VF_OFFLOAD_WB_ON_ITR |
 138               VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2 |
 139               VIRTCHNL_VF_OFFLOAD_ENCAP |
 140               VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM |
 141               VIRTCHNL_VF_OFFLOAD_REQ_QUEUES |
 142               VIRTCHNL_VF_OFFLOAD_ADQ;
 143
 144        adapter->current_op = VIRTCHNL_OP_GET_VF_RESOURCES;
 145        adapter->aq_required &= ~IAVF_FLAG_AQ_GET_CONFIG;
 146        if (PF_IS_V11(adapter))
 147                return iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_VF_RESOURCES,
 148                                        (u8 *)&caps, sizeof(caps));
 149        else
 150                return iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_VF_RESOURCES,
 151                                        NULL, 0);
 152}
 153
 154/**
 155 * iavf_validate_num_queues
 156 * @adapter: adapter structure
 157 *
 158 * Validate that the number of queues the PF has sent in
 159 * VIRTCHNL_OP_GET_VF_RESOURCES is not larger than the VF can handle.
 160 **/
 161static void iavf_validate_num_queues(struct iavf_adapter *adapter)
 162{
 163        if (adapter->vf_res->num_queue_pairs > IAVF_MAX_REQ_QUEUES) {
 164                struct virtchnl_vsi_resource *vsi_res;
 165                int i;
 166
 167                dev_info(&adapter->pdev->dev, "Received %d queues, but can only have a max of %d\n",
 168                         adapter->vf_res->num_queue_pairs,
 169                         IAVF_MAX_REQ_QUEUES);
 170                dev_info(&adapter->pdev->dev, "Fixing by reducing queues to %d\n",
 171                         IAVF_MAX_REQ_QUEUES);
 172                adapter->vf_res->num_queue_pairs = IAVF_MAX_REQ_QUEUES;
 173                for (i = 0; i < adapter->vf_res->num_vsis; i++) {
 174                        vsi_res = &adapter->vf_res->vsi_res[i];
 175                        vsi_res->num_queue_pairs = IAVF_MAX_REQ_QUEUES;
 176                }
 177        }
 178}
 179
 180/**
 181 * iavf_get_vf_config
 182 * @adapter: private adapter structure
 183 *
 184 * Get VF configuration from PF and populate hw structure. Must be called after
 185 * admin queue is initialized. Busy waits until response is received from PF,
 186 * with maximum timeout. Response from PF is returned in the buffer for further
 187 * processing by the caller.
 188 **/
 189int iavf_get_vf_config(struct iavf_adapter *adapter)
 190{
 191        struct iavf_hw *hw = &adapter->hw;
 192        struct i40e_arq_event_info event;
 193        enum virtchnl_ops op;
 194        iavf_status err;
 195        u16 len;
 196
 197        len =  sizeof(struct virtchnl_vf_resource) +
 198                IAVF_MAX_VF_VSI * sizeof(struct virtchnl_vsi_resource);
 199        event.buf_len = len;
 200        event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
 201        if (!event.msg_buf) {
 202                err = -ENOMEM;
 203                goto out;
 204        }
 205
 206        while (1) {
 207                /* When the AQ is empty, iavf_clean_arq_element will return
 208                 * nonzero and this loop will terminate.
 209                 */
 210                err = iavf_clean_arq_element(hw, &event, NULL);
 211                if (err)
 212                        goto out_alloc;
 213                op =
 214                    (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
 215                if (op == VIRTCHNL_OP_GET_VF_RESOURCES)
 216                        break;
 217        }
 218
 219        err = (iavf_status)le32_to_cpu(event.desc.cookie_low);
 220        memcpy(adapter->vf_res, event.msg_buf, min(event.msg_len, len));
 221
 222        /* some PFs send more queues than we should have so validate that
 223         * we aren't getting too many queues
 224         */
 225        if (!err)
 226                iavf_validate_num_queues(adapter);
 227        iavf_vf_parse_hw_config(hw, adapter->vf_res);
 228out_alloc:
 229        kfree(event.msg_buf);
 230out:
 231        return err;
 232}
 233
 234/**
 235 * iavf_configure_queues
 236 * @adapter: adapter structure
 237 *
 238 * Request that the PF set up our (previously allocated) queues.
 239 **/
 240void iavf_configure_queues(struct iavf_adapter *adapter)
 241{
 242        struct virtchnl_vsi_queue_config_info *vqci;
 243        struct virtchnl_queue_pair_info *vqpi;
 244        int pairs = adapter->num_active_queues;
 245        int i, len, max_frame = IAVF_MAX_RXBUFFER;
 246
 247        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 248                /* bail because we already have a command pending */
 249                dev_err(&adapter->pdev->dev, "Cannot configure queues, command %d pending\n",
 250                        adapter->current_op);
 251                return;
 252        }
 253        adapter->current_op = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
 254        len = sizeof(struct virtchnl_vsi_queue_config_info) +
 255                       (sizeof(struct virtchnl_queue_pair_info) * pairs);
 256        vqci = kzalloc(len, GFP_KERNEL);
 257        if (!vqci)
 258                return;
 259
 260        /* Limit maximum frame size when jumbo frames is not enabled */
 261        if (!(adapter->flags & IAVF_FLAG_LEGACY_RX) &&
 262            (adapter->netdev->mtu <= ETH_DATA_LEN))
 263                max_frame = IAVF_RXBUFFER_1536 - NET_IP_ALIGN;
 264
 265        vqci->vsi_id = adapter->vsi_res->vsi_id;
 266        vqci->num_queue_pairs = pairs;
 267        vqpi = vqci->qpair;
 268        /* Size check is not needed here - HW max is 16 queue pairs, and we
 269         * can fit info for 31 of them into the AQ buffer before it overflows.
 270         */
 271        for (i = 0; i < pairs; i++) {
 272                vqpi->txq.vsi_id = vqci->vsi_id;
 273                vqpi->txq.queue_id = i;
 274                vqpi->txq.ring_len = adapter->tx_rings[i].count;
 275                vqpi->txq.dma_ring_addr = adapter->tx_rings[i].dma;
 276                vqpi->rxq.vsi_id = vqci->vsi_id;
 277                vqpi->rxq.queue_id = i;
 278                vqpi->rxq.ring_len = adapter->rx_rings[i].count;
 279                vqpi->rxq.dma_ring_addr = adapter->rx_rings[i].dma;
 280                vqpi->rxq.max_pkt_size = max_frame;
 281                vqpi->rxq.databuffer_size =
 282                        ALIGN(adapter->rx_rings[i].rx_buf_len,
 283                              BIT_ULL(IAVF_RXQ_CTX_DBUFF_SHIFT));
 284                vqpi++;
 285        }
 286
 287        adapter->aq_required &= ~IAVF_FLAG_AQ_CONFIGURE_QUEUES;
 288        iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
 289                         (u8 *)vqci, len);
 290        kfree(vqci);
 291}
 292
 293/**
 294 * iavf_enable_queues
 295 * @adapter: adapter structure
 296 *
 297 * Request that the PF enable all of our queues.
 298 **/
 299void iavf_enable_queues(struct iavf_adapter *adapter)
 300{
 301        struct virtchnl_queue_select vqs;
 302
 303        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 304                /* bail because we already have a command pending */
 305                dev_err(&adapter->pdev->dev, "Cannot enable queues, command %d pending\n",
 306                        adapter->current_op);
 307                return;
 308        }
 309        adapter->current_op = VIRTCHNL_OP_ENABLE_QUEUES;
 310        vqs.vsi_id = adapter->vsi_res->vsi_id;
 311        vqs.tx_queues = BIT(adapter->num_active_queues) - 1;
 312        vqs.rx_queues = vqs.tx_queues;
 313        adapter->aq_required &= ~IAVF_FLAG_AQ_ENABLE_QUEUES;
 314        iavf_send_pf_msg(adapter, VIRTCHNL_OP_ENABLE_QUEUES,
 315                         (u8 *)&vqs, sizeof(vqs));
 316}
 317
 318/**
 319 * iavf_disable_queues
 320 * @adapter: adapter structure
 321 *
 322 * Request that the PF disable all of our queues.
 323 **/
 324void iavf_disable_queues(struct iavf_adapter *adapter)
 325{
 326        struct virtchnl_queue_select vqs;
 327
 328        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 329                /* bail because we already have a command pending */
 330                dev_err(&adapter->pdev->dev, "Cannot disable queues, command %d pending\n",
 331                        adapter->current_op);
 332                return;
 333        }
 334        adapter->current_op = VIRTCHNL_OP_DISABLE_QUEUES;
 335        vqs.vsi_id = adapter->vsi_res->vsi_id;
 336        vqs.tx_queues = BIT(adapter->num_active_queues) - 1;
 337        vqs.rx_queues = vqs.tx_queues;
 338        adapter->aq_required &= ~IAVF_FLAG_AQ_DISABLE_QUEUES;
 339        iavf_send_pf_msg(adapter, VIRTCHNL_OP_DISABLE_QUEUES,
 340                         (u8 *)&vqs, sizeof(vqs));
 341}
 342
 343/**
 344 * iavf_map_queues
 345 * @adapter: adapter structure
 346 *
 347 * Request that the PF map queues to interrupt vectors. Misc causes, including
 348 * admin queue, are always mapped to vector 0.
 349 **/
 350void iavf_map_queues(struct iavf_adapter *adapter)
 351{
 352        struct virtchnl_irq_map_info *vimi;
 353        struct virtchnl_vector_map *vecmap;
 354        int v_idx, q_vectors, len;
 355        struct iavf_q_vector *q_vector;
 356
 357        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 358                /* bail because we already have a command pending */
 359                dev_err(&adapter->pdev->dev, "Cannot map queues to vectors, command %d pending\n",
 360                        adapter->current_op);
 361                return;
 362        }
 363        adapter->current_op = VIRTCHNL_OP_CONFIG_IRQ_MAP;
 364
 365        q_vectors = adapter->num_msix_vectors - NONQ_VECS;
 366
 367        len = sizeof(struct virtchnl_irq_map_info) +
 368              (adapter->num_msix_vectors *
 369                sizeof(struct virtchnl_vector_map));
 370        vimi = kzalloc(len, GFP_KERNEL);
 371        if (!vimi)
 372                return;
 373
 374        vimi->num_vectors = adapter->num_msix_vectors;
 375        /* Queue vectors first */
 376        for (v_idx = 0; v_idx < q_vectors; v_idx++) {
 377                q_vector = &adapter->q_vectors[v_idx];
 378                vecmap = &vimi->vecmap[v_idx];
 379
 380                vecmap->vsi_id = adapter->vsi_res->vsi_id;
 381                vecmap->vector_id = v_idx + NONQ_VECS;
 382                vecmap->txq_map = q_vector->ring_mask;
 383                vecmap->rxq_map = q_vector->ring_mask;
 384                vecmap->rxitr_idx = IAVF_RX_ITR;
 385                vecmap->txitr_idx = IAVF_TX_ITR;
 386        }
 387        /* Misc vector last - this is only for AdminQ messages */
 388        vecmap = &vimi->vecmap[v_idx];
 389        vecmap->vsi_id = adapter->vsi_res->vsi_id;
 390        vecmap->vector_id = 0;
 391        vecmap->txq_map = 0;
 392        vecmap->rxq_map = 0;
 393
 394        adapter->aq_required &= ~IAVF_FLAG_AQ_MAP_VECTORS;
 395        iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_IRQ_MAP,
 396                         (u8 *)vimi, len);
 397        kfree(vimi);
 398}
 399
 400/**
 401 * iavf_request_queues
 402 * @adapter: adapter structure
 403 * @num: number of requested queues
 404 *
 405 * We get a default number of queues from the PF.  This enables us to request a
 406 * different number.  Returns 0 on success, negative on failure
 407 **/
 408int iavf_request_queues(struct iavf_adapter *adapter, int num)
 409{
 410        struct virtchnl_vf_res_request vfres;
 411
 412        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 413                /* bail because we already have a command pending */
 414                dev_err(&adapter->pdev->dev, "Cannot request queues, command %d pending\n",
 415                        adapter->current_op);
 416                return -EBUSY;
 417        }
 418
 419        vfres.num_queue_pairs = num;
 420
 421        adapter->current_op = VIRTCHNL_OP_REQUEST_QUEUES;
 422        adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
 423        return iavf_send_pf_msg(adapter, VIRTCHNL_OP_REQUEST_QUEUES,
 424                                (u8 *)&vfres, sizeof(vfres));
 425}
 426
 427/**
 428 * iavf_add_ether_addrs
 429 * @adapter: adapter structure
 430 *
 431 * Request that the PF add one or more addresses to our filters.
 432 **/
 433void iavf_add_ether_addrs(struct iavf_adapter *adapter)
 434{
 435        struct virtchnl_ether_addr_list *veal;
 436        int len, i = 0, count = 0;
 437        struct iavf_mac_filter *f;
 438        bool more = false;
 439
 440        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 441                /* bail because we already have a command pending */
 442                dev_err(&adapter->pdev->dev, "Cannot add filters, command %d pending\n",
 443                        adapter->current_op);
 444                return;
 445        }
 446
 447        spin_lock_bh(&adapter->mac_vlan_list_lock);
 448
 449        list_for_each_entry(f, &adapter->mac_filter_list, list) {
 450                if (f->add)
 451                        count++;
 452        }
 453        if (!count) {
 454                adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_MAC_FILTER;
 455                spin_unlock_bh(&adapter->mac_vlan_list_lock);
 456                return;
 457        }
 458        adapter->current_op = VIRTCHNL_OP_ADD_ETH_ADDR;
 459
 460        len = sizeof(struct virtchnl_ether_addr_list) +
 461              (count * sizeof(struct virtchnl_ether_addr));
 462        if (len > IAVF_MAX_AQ_BUF_SIZE) {
 463                dev_warn(&adapter->pdev->dev, "Too many add MAC changes in one request\n");
 464                count = (IAVF_MAX_AQ_BUF_SIZE -
 465                         sizeof(struct virtchnl_ether_addr_list)) /
 466                        sizeof(struct virtchnl_ether_addr);
 467                len = sizeof(struct virtchnl_ether_addr_list) +
 468                      (count * sizeof(struct virtchnl_ether_addr));
 469                more = true;
 470        }
 471
 472        veal = kzalloc(len, GFP_ATOMIC);
 473        if (!veal) {
 474                spin_unlock_bh(&adapter->mac_vlan_list_lock);
 475                return;
 476        }
 477
 478        veal->vsi_id = adapter->vsi_res->vsi_id;
 479        veal->num_elements = count;
 480        list_for_each_entry(f, &adapter->mac_filter_list, list) {
 481                if (f->add) {
 482                        ether_addr_copy(veal->list[i].addr, f->macaddr);
 483                        i++;
 484                        f->add = false;
 485                        if (i == count)
 486                                break;
 487                }
 488        }
 489        if (!more)
 490                adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_MAC_FILTER;
 491
 492        spin_unlock_bh(&adapter->mac_vlan_list_lock);
 493
 494        iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_ETH_ADDR, (u8 *)veal, len);
 495        kfree(veal);
 496}
 497
 498/**
 499 * iavf_del_ether_addrs
 500 * @adapter: adapter structure
 501 *
 502 * Request that the PF remove one or more addresses from our filters.
 503 **/
 504void iavf_del_ether_addrs(struct iavf_adapter *adapter)
 505{
 506        struct virtchnl_ether_addr_list *veal;
 507        struct iavf_mac_filter *f, *ftmp;
 508        int len, i = 0, count = 0;
 509        bool more = false;
 510
 511        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 512                /* bail because we already have a command pending */
 513                dev_err(&adapter->pdev->dev, "Cannot remove filters, command %d pending\n",
 514                        adapter->current_op);
 515                return;
 516        }
 517
 518        spin_lock_bh(&adapter->mac_vlan_list_lock);
 519
 520        list_for_each_entry(f, &adapter->mac_filter_list, list) {
 521                if (f->remove)
 522                        count++;
 523        }
 524        if (!count) {
 525                adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_MAC_FILTER;
 526                spin_unlock_bh(&adapter->mac_vlan_list_lock);
 527                return;
 528        }
 529        adapter->current_op = VIRTCHNL_OP_DEL_ETH_ADDR;
 530
 531        len = sizeof(struct virtchnl_ether_addr_list) +
 532              (count * sizeof(struct virtchnl_ether_addr));
 533        if (len > IAVF_MAX_AQ_BUF_SIZE) {
 534                dev_warn(&adapter->pdev->dev, "Too many delete MAC changes in one request\n");
 535                count = (IAVF_MAX_AQ_BUF_SIZE -
 536                         sizeof(struct virtchnl_ether_addr_list)) /
 537                        sizeof(struct virtchnl_ether_addr);
 538                len = sizeof(struct virtchnl_ether_addr_list) +
 539                      (count * sizeof(struct virtchnl_ether_addr));
 540                more = true;
 541        }
 542        veal = kzalloc(len, GFP_ATOMIC);
 543        if (!veal) {
 544                spin_unlock_bh(&adapter->mac_vlan_list_lock);
 545                return;
 546        }
 547
 548        veal->vsi_id = adapter->vsi_res->vsi_id;
 549        veal->num_elements = count;
 550        list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
 551                if (f->remove) {
 552                        ether_addr_copy(veal->list[i].addr, f->macaddr);
 553                        i++;
 554                        list_del(&f->list);
 555                        kfree(f);
 556                        if (i == count)
 557                                break;
 558                }
 559        }
 560        if (!more)
 561                adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_MAC_FILTER;
 562
 563        spin_unlock_bh(&adapter->mac_vlan_list_lock);
 564
 565        iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_ETH_ADDR, (u8 *)veal, len);
 566        kfree(veal);
 567}
 568
 569/**
 570 * iavf_add_vlans
 571 * @adapter: adapter structure
 572 *
 573 * Request that the PF add one or more VLAN filters to our VSI.
 574 **/
 575void iavf_add_vlans(struct iavf_adapter *adapter)
 576{
 577        struct virtchnl_vlan_filter_list *vvfl;
 578        int len, i = 0, count = 0;
 579        struct iavf_vlan_filter *f;
 580        bool more = false;
 581
 582        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 583                /* bail because we already have a command pending */
 584                dev_err(&adapter->pdev->dev, "Cannot add VLANs, command %d pending\n",
 585                        adapter->current_op);
 586                return;
 587        }
 588
 589        spin_lock_bh(&adapter->mac_vlan_list_lock);
 590
 591        list_for_each_entry(f, &adapter->vlan_filter_list, list) {
 592                if (f->add)
 593                        count++;
 594        }
 595        if (!count) {
 596                adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_VLAN_FILTER;
 597                spin_unlock_bh(&adapter->mac_vlan_list_lock);
 598                return;
 599        }
 600        adapter->current_op = VIRTCHNL_OP_ADD_VLAN;
 601
 602        len = sizeof(struct virtchnl_vlan_filter_list) +
 603              (count * sizeof(u16));
 604        if (len > IAVF_MAX_AQ_BUF_SIZE) {
 605                dev_warn(&adapter->pdev->dev, "Too many add VLAN changes in one request\n");
 606                count = (IAVF_MAX_AQ_BUF_SIZE -
 607                         sizeof(struct virtchnl_vlan_filter_list)) /
 608                        sizeof(u16);
 609                len = sizeof(struct virtchnl_vlan_filter_list) +
 610                      (count * sizeof(u16));
 611                more = true;
 612        }
 613        vvfl = kzalloc(len, GFP_ATOMIC);
 614        if (!vvfl) {
 615                spin_unlock_bh(&adapter->mac_vlan_list_lock);
 616                return;
 617        }
 618
 619        vvfl->vsi_id = adapter->vsi_res->vsi_id;
 620        vvfl->num_elements = count;
 621        list_for_each_entry(f, &adapter->vlan_filter_list, list) {
 622                if (f->add) {
 623                        vvfl->vlan_id[i] = f->vlan;
 624                        i++;
 625                        f->add = false;
 626                        if (i == count)
 627                                break;
 628                }
 629        }
 630        if (!more)
 631                adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_VLAN_FILTER;
 632
 633        spin_unlock_bh(&adapter->mac_vlan_list_lock);
 634
 635        iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_VLAN, (u8 *)vvfl, len);
 636        kfree(vvfl);
 637}
 638
 639/**
 640 * iavf_del_vlans
 641 * @adapter: adapter structure
 642 *
 643 * Request that the PF remove one or more VLAN filters from our VSI.
 644 **/
 645void iavf_del_vlans(struct iavf_adapter *adapter)
 646{
 647        struct virtchnl_vlan_filter_list *vvfl;
 648        struct iavf_vlan_filter *f, *ftmp;
 649        int len, i = 0, count = 0;
 650        bool more = false;
 651
 652        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 653                /* bail because we already have a command pending */
 654                dev_err(&adapter->pdev->dev, "Cannot remove VLANs, command %d pending\n",
 655                        adapter->current_op);
 656                return;
 657        }
 658
 659        spin_lock_bh(&adapter->mac_vlan_list_lock);
 660
 661        list_for_each_entry(f, &adapter->vlan_filter_list, list) {
 662                if (f->remove)
 663                        count++;
 664        }
 665        if (!count) {
 666                adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_VLAN_FILTER;
 667                spin_unlock_bh(&adapter->mac_vlan_list_lock);
 668                return;
 669        }
 670        adapter->current_op = VIRTCHNL_OP_DEL_VLAN;
 671
 672        len = sizeof(struct virtchnl_vlan_filter_list) +
 673              (count * sizeof(u16));
 674        if (len > IAVF_MAX_AQ_BUF_SIZE) {
 675                dev_warn(&adapter->pdev->dev, "Too many delete VLAN changes in one request\n");
 676                count = (IAVF_MAX_AQ_BUF_SIZE -
 677                         sizeof(struct virtchnl_vlan_filter_list)) /
 678                        sizeof(u16);
 679                len = sizeof(struct virtchnl_vlan_filter_list) +
 680                      (count * sizeof(u16));
 681                more = true;
 682        }
 683        vvfl = kzalloc(len, GFP_ATOMIC);
 684        if (!vvfl) {
 685                spin_unlock_bh(&adapter->mac_vlan_list_lock);
 686                return;
 687        }
 688
 689        vvfl->vsi_id = adapter->vsi_res->vsi_id;
 690        vvfl->num_elements = count;
 691        list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
 692                if (f->remove) {
 693                        vvfl->vlan_id[i] = f->vlan;
 694                        i++;
 695                        list_del(&f->list);
 696                        kfree(f);
 697                        if (i == count)
 698                                break;
 699                }
 700        }
 701        if (!more)
 702                adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_VLAN_FILTER;
 703
 704        spin_unlock_bh(&adapter->mac_vlan_list_lock);
 705
 706        iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_VLAN, (u8 *)vvfl, len);
 707        kfree(vvfl);
 708}
 709
 710/**
 711 * iavf_set_promiscuous
 712 * @adapter: adapter structure
 713 * @flags: bitmask to control unicast/multicast promiscuous.
 714 *
 715 * Request that the PF enable promiscuous mode for our VSI.
 716 **/
 717void iavf_set_promiscuous(struct iavf_adapter *adapter, int flags)
 718{
 719        struct virtchnl_promisc_info vpi;
 720        int promisc_all;
 721
 722        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 723                /* bail because we already have a command pending */
 724                dev_err(&adapter->pdev->dev, "Cannot set promiscuous mode, command %d pending\n",
 725                        adapter->current_op);
 726                return;
 727        }
 728
 729        promisc_all = FLAG_VF_UNICAST_PROMISC |
 730                      FLAG_VF_MULTICAST_PROMISC;
 731        if ((flags & promisc_all) == promisc_all) {
 732                adapter->flags |= IAVF_FLAG_PROMISC_ON;
 733                adapter->aq_required &= ~IAVF_FLAG_AQ_REQUEST_PROMISC;
 734                dev_info(&adapter->pdev->dev, "Entering promiscuous mode\n");
 735        }
 736
 737        if (flags & FLAG_VF_MULTICAST_PROMISC) {
 738                adapter->flags |= IAVF_FLAG_ALLMULTI_ON;
 739                adapter->aq_required &= ~IAVF_FLAG_AQ_REQUEST_ALLMULTI;
 740                dev_info(&adapter->pdev->dev, "Entering multicast promiscuous mode\n");
 741        }
 742
 743        if (!flags) {
 744                adapter->flags &= ~(IAVF_FLAG_PROMISC_ON |
 745                                    IAVF_FLAG_ALLMULTI_ON);
 746                adapter->aq_required &= ~(IAVF_FLAG_AQ_RELEASE_PROMISC |
 747                                          IAVF_FLAG_AQ_RELEASE_ALLMULTI);
 748                dev_info(&adapter->pdev->dev, "Leaving promiscuous mode\n");
 749        }
 750
 751        adapter->current_op = VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE;
 752        vpi.vsi_id = adapter->vsi_res->vsi_id;
 753        vpi.flags = flags;
 754        iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
 755                         (u8 *)&vpi, sizeof(vpi));
 756}
 757
 758/**
 759 * iavf_request_stats
 760 * @adapter: adapter structure
 761 *
 762 * Request VSI statistics from PF.
 763 **/
 764void iavf_request_stats(struct iavf_adapter *adapter)
 765{
 766        struct virtchnl_queue_select vqs;
 767
 768        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 769                /* no error message, this isn't crucial */
 770                return;
 771        }
 772        adapter->current_op = VIRTCHNL_OP_GET_STATS;
 773        vqs.vsi_id = adapter->vsi_res->vsi_id;
 774        /* queue maps are ignored for this message - only the vsi is used */
 775        if (iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_STATS, (u8 *)&vqs,
 776                             sizeof(vqs)))
 777                /* if the request failed, don't lock out others */
 778                adapter->current_op = VIRTCHNL_OP_UNKNOWN;
 779}
 780
 781/**
 782 * iavf_get_hena
 783 * @adapter: adapter structure
 784 *
 785 * Request hash enable capabilities from PF
 786 **/
 787void iavf_get_hena(struct iavf_adapter *adapter)
 788{
 789        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 790                /* bail because we already have a command pending */
 791                dev_err(&adapter->pdev->dev, "Cannot get RSS hash capabilities, command %d pending\n",
 792                        adapter->current_op);
 793                return;
 794        }
 795        adapter->current_op = VIRTCHNL_OP_GET_RSS_HENA_CAPS;
 796        adapter->aq_required &= ~IAVF_FLAG_AQ_GET_HENA;
 797        iavf_send_pf_msg(adapter, VIRTCHNL_OP_GET_RSS_HENA_CAPS, NULL, 0);
 798}
 799
 800/**
 801 * iavf_set_hena
 802 * @adapter: adapter structure
 803 *
 804 * Request the PF to set our RSS hash capabilities
 805 **/
 806void iavf_set_hena(struct iavf_adapter *adapter)
 807{
 808        struct virtchnl_rss_hena vrh;
 809
 810        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 811                /* bail because we already have a command pending */
 812                dev_err(&adapter->pdev->dev, "Cannot set RSS hash enable, command %d pending\n",
 813                        adapter->current_op);
 814                return;
 815        }
 816        vrh.hena = adapter->hena;
 817        adapter->current_op = VIRTCHNL_OP_SET_RSS_HENA;
 818        adapter->aq_required &= ~IAVF_FLAG_AQ_SET_HENA;
 819        iavf_send_pf_msg(adapter, VIRTCHNL_OP_SET_RSS_HENA, (u8 *)&vrh,
 820                         sizeof(vrh));
 821}
 822
 823/**
 824 * iavf_set_rss_key
 825 * @adapter: adapter structure
 826 *
 827 * Request the PF to set our RSS hash key
 828 **/
 829void iavf_set_rss_key(struct iavf_adapter *adapter)
 830{
 831        struct virtchnl_rss_key *vrk;
 832        int len;
 833
 834        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 835                /* bail because we already have a command pending */
 836                dev_err(&adapter->pdev->dev, "Cannot set RSS key, command %d pending\n",
 837                        adapter->current_op);
 838                return;
 839        }
 840        len = sizeof(struct virtchnl_rss_key) +
 841              (adapter->rss_key_size * sizeof(u8)) - 1;
 842        vrk = kzalloc(len, GFP_KERNEL);
 843        if (!vrk)
 844                return;
 845        vrk->vsi_id = adapter->vsi.id;
 846        vrk->key_len = adapter->rss_key_size;
 847        memcpy(vrk->key, adapter->rss_key, adapter->rss_key_size);
 848
 849        adapter->current_op = VIRTCHNL_OP_CONFIG_RSS_KEY;
 850        adapter->aq_required &= ~IAVF_FLAG_AQ_SET_RSS_KEY;
 851        iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_RSS_KEY, (u8 *)vrk, len);
 852        kfree(vrk);
 853}
 854
 855/**
 856 * iavf_set_rss_lut
 857 * @adapter: adapter structure
 858 *
 859 * Request the PF to set our RSS lookup table
 860 **/
 861void iavf_set_rss_lut(struct iavf_adapter *adapter)
 862{
 863        struct virtchnl_rss_lut *vrl;
 864        int len;
 865
 866        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 867                /* bail because we already have a command pending */
 868                dev_err(&adapter->pdev->dev, "Cannot set RSS LUT, command %d pending\n",
 869                        adapter->current_op);
 870                return;
 871        }
 872        len = sizeof(struct virtchnl_rss_lut) +
 873              (adapter->rss_lut_size * sizeof(u8)) - 1;
 874        vrl = kzalloc(len, GFP_KERNEL);
 875        if (!vrl)
 876                return;
 877        vrl->vsi_id = adapter->vsi.id;
 878        vrl->lut_entries = adapter->rss_lut_size;
 879        memcpy(vrl->lut, adapter->rss_lut, adapter->rss_lut_size);
 880        adapter->current_op = VIRTCHNL_OP_CONFIG_RSS_LUT;
 881        adapter->aq_required &= ~IAVF_FLAG_AQ_SET_RSS_LUT;
 882        iavf_send_pf_msg(adapter, VIRTCHNL_OP_CONFIG_RSS_LUT, (u8 *)vrl, len);
 883        kfree(vrl);
 884}
 885
 886/**
 887 * iavf_enable_vlan_stripping
 888 * @adapter: adapter structure
 889 *
 890 * Request VLAN header stripping to be enabled
 891 **/
 892void iavf_enable_vlan_stripping(struct iavf_adapter *adapter)
 893{
 894        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 895                /* bail because we already have a command pending */
 896                dev_err(&adapter->pdev->dev, "Cannot enable stripping, command %d pending\n",
 897                        adapter->current_op);
 898                return;
 899        }
 900        adapter->current_op = VIRTCHNL_OP_ENABLE_VLAN_STRIPPING;
 901        adapter->aq_required &= ~IAVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
 902        iavf_send_pf_msg(adapter, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING, NULL, 0);
 903}
 904
 905/**
 906 * iavf_disable_vlan_stripping
 907 * @adapter: adapter structure
 908 *
 909 * Request VLAN header stripping to be disabled
 910 **/
 911void iavf_disable_vlan_stripping(struct iavf_adapter *adapter)
 912{
 913        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 914                /* bail because we already have a command pending */
 915                dev_err(&adapter->pdev->dev, "Cannot disable stripping, command %d pending\n",
 916                        adapter->current_op);
 917                return;
 918        }
 919        adapter->current_op = VIRTCHNL_OP_DISABLE_VLAN_STRIPPING;
 920        adapter->aq_required &= ~IAVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
 921        iavf_send_pf_msg(adapter, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING, NULL, 0);
 922}
 923
 924/**
 925 * iavf_print_link_message - print link up or down
 926 * @adapter: adapter structure
 927 *
 928 * Log a message telling the world of our wonderous link status
 929 */
 930static void iavf_print_link_message(struct iavf_adapter *adapter)
 931{
 932        struct net_device *netdev = adapter->netdev;
 933        char *speed = "Unknown ";
 934
 935        if (!adapter->link_up) {
 936                netdev_info(netdev, "NIC Link is Down\n");
 937                return;
 938        }
 939
 940        switch (adapter->link_speed) {
 941        case I40E_LINK_SPEED_40GB:
 942                speed = "40 G";
 943                break;
 944        case I40E_LINK_SPEED_25GB:
 945                speed = "25 G";
 946                break;
 947        case I40E_LINK_SPEED_20GB:
 948                speed = "20 G";
 949                break;
 950        case I40E_LINK_SPEED_10GB:
 951                speed = "10 G";
 952                break;
 953        case I40E_LINK_SPEED_1GB:
 954                speed = "1000 M";
 955                break;
 956        case I40E_LINK_SPEED_100MB:
 957                speed = "100 M";
 958                break;
 959        default:
 960                break;
 961        }
 962
 963        netdev_info(netdev, "NIC Link is Up %sbps Full Duplex\n", speed);
 964}
 965
 966/**
 967 * iavf_enable_channel
 968 * @adapter: adapter structure
 969 *
 970 * Request that the PF enable channels as specified by
 971 * the user via tc tool.
 972 **/
 973void iavf_enable_channels(struct iavf_adapter *adapter)
 974{
 975        struct virtchnl_tc_info *vti = NULL;
 976        u16 len;
 977        int i;
 978
 979        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
 980                /* bail because we already have a command pending */
 981                dev_err(&adapter->pdev->dev, "Cannot configure mqprio, command %d pending\n",
 982                        adapter->current_op);
 983                return;
 984        }
 985
 986        len = (adapter->num_tc * sizeof(struct virtchnl_channel_info)) +
 987               sizeof(struct virtchnl_tc_info);
 988
 989        vti = kzalloc(len, GFP_KERNEL);
 990        if (!vti)
 991                return;
 992        vti->num_tc = adapter->num_tc;
 993        for (i = 0; i < vti->num_tc; i++) {
 994                vti->list[i].count = adapter->ch_config.ch_info[i].count;
 995                vti->list[i].offset = adapter->ch_config.ch_info[i].offset;
 996                vti->list[i].pad = 0;
 997                vti->list[i].max_tx_rate =
 998                                adapter->ch_config.ch_info[i].max_tx_rate;
 999        }
1000
1001        adapter->ch_config.state = __IAVF_TC_RUNNING;
1002        adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1003        adapter->current_op = VIRTCHNL_OP_ENABLE_CHANNELS;
1004        adapter->aq_required &= ~IAVF_FLAG_AQ_ENABLE_CHANNELS;
1005        iavf_send_pf_msg(adapter, VIRTCHNL_OP_ENABLE_CHANNELS, (u8 *)vti, len);
1006        kfree(vti);
1007}
1008
1009/**
1010 * iavf_disable_channel
1011 * @adapter: adapter structure
1012 *
1013 * Request that the PF disable channels that are configured
1014 **/
1015void iavf_disable_channels(struct iavf_adapter *adapter)
1016{
1017        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1018                /* bail because we already have a command pending */
1019                dev_err(&adapter->pdev->dev, "Cannot configure mqprio, command %d pending\n",
1020                        adapter->current_op);
1021                return;
1022        }
1023
1024        adapter->ch_config.state = __IAVF_TC_INVALID;
1025        adapter->flags |= IAVF_FLAG_REINIT_ITR_NEEDED;
1026        adapter->current_op = VIRTCHNL_OP_DISABLE_CHANNELS;
1027        adapter->aq_required &= ~IAVF_FLAG_AQ_DISABLE_CHANNELS;
1028        iavf_send_pf_msg(adapter, VIRTCHNL_OP_DISABLE_CHANNELS, NULL, 0);
1029}
1030
1031/**
1032 * iavf_print_cloud_filter
1033 * @adapter: adapter structure
1034 * @f: cloud filter to print
1035 *
1036 * Print the cloud filter
1037 **/
1038static void iavf_print_cloud_filter(struct iavf_adapter *adapter,
1039                                    struct virtchnl_filter *f)
1040{
1041        switch (f->flow_type) {
1042        case VIRTCHNL_TCP_V4_FLOW:
1043                dev_info(&adapter->pdev->dev, "dst_mac: %pM src_mac: %pM vlan_id: %hu dst_ip: %pI4 src_ip %pI4 dst_port %hu src_port %hu\n",
1044                         &f->data.tcp_spec.dst_mac,
1045                         &f->data.tcp_spec.src_mac,
1046                         ntohs(f->data.tcp_spec.vlan_id),
1047                         &f->data.tcp_spec.dst_ip[0],
1048                         &f->data.tcp_spec.src_ip[0],
1049                         ntohs(f->data.tcp_spec.dst_port),
1050                         ntohs(f->data.tcp_spec.src_port));
1051                break;
1052        case VIRTCHNL_TCP_V6_FLOW:
1053                dev_info(&adapter->pdev->dev, "dst_mac: %pM src_mac: %pM vlan_id: %hu dst_ip: %pI6 src_ip %pI6 dst_port %hu src_port %hu\n",
1054                         &f->data.tcp_spec.dst_mac,
1055                         &f->data.tcp_spec.src_mac,
1056                         ntohs(f->data.tcp_spec.vlan_id),
1057                         &f->data.tcp_spec.dst_ip,
1058                         &f->data.tcp_spec.src_ip,
1059                         ntohs(f->data.tcp_spec.dst_port),
1060                         ntohs(f->data.tcp_spec.src_port));
1061                break;
1062        }
1063}
1064
1065/**
1066 * iavf_add_cloud_filter
1067 * @adapter: adapter structure
1068 *
1069 * Request that the PF add cloud filters as specified
1070 * by the user via tc tool.
1071 **/
1072void iavf_add_cloud_filter(struct iavf_adapter *adapter)
1073{
1074        struct iavf_cloud_filter *cf;
1075        struct virtchnl_filter *f;
1076        int len = 0, count = 0;
1077
1078        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1079                /* bail because we already have a command pending */
1080                dev_err(&adapter->pdev->dev, "Cannot add cloud filter, command %d pending\n",
1081                        adapter->current_op);
1082                return;
1083        }
1084        list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1085                if (cf->add) {
1086                        count++;
1087                        break;
1088                }
1089        }
1090        if (!count) {
1091                adapter->aq_required &= ~IAVF_FLAG_AQ_ADD_CLOUD_FILTER;
1092                return;
1093        }
1094        adapter->current_op = VIRTCHNL_OP_ADD_CLOUD_FILTER;
1095
1096        len = sizeof(struct virtchnl_filter);
1097        f = kzalloc(len, GFP_KERNEL);
1098        if (!f)
1099                return;
1100
1101        list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1102                if (cf->add) {
1103                        memcpy(f, &cf->f, sizeof(struct virtchnl_filter));
1104                        cf->add = false;
1105                        cf->state = __IAVF_CF_ADD_PENDING;
1106                        iavf_send_pf_msg(adapter, VIRTCHNL_OP_ADD_CLOUD_FILTER,
1107                                         (u8 *)f, len);
1108                }
1109        }
1110        kfree(f);
1111}
1112
1113/**
1114 * iavf_del_cloud_filter
1115 * @adapter: adapter structure
1116 *
1117 * Request that the PF delete cloud filters as specified
1118 * by the user via tc tool.
1119 **/
1120void iavf_del_cloud_filter(struct iavf_adapter *adapter)
1121{
1122        struct iavf_cloud_filter *cf, *cftmp;
1123        struct virtchnl_filter *f;
1124        int len = 0, count = 0;
1125
1126        if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
1127                /* bail because we already have a command pending */
1128                dev_err(&adapter->pdev->dev, "Cannot remove cloud filter, command %d pending\n",
1129                        adapter->current_op);
1130                return;
1131        }
1132        list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1133                if (cf->del) {
1134                        count++;
1135                        break;
1136                }
1137        }
1138        if (!count) {
1139                adapter->aq_required &= ~IAVF_FLAG_AQ_DEL_CLOUD_FILTER;
1140                return;
1141        }
1142        adapter->current_op = VIRTCHNL_OP_DEL_CLOUD_FILTER;
1143
1144        len = sizeof(struct virtchnl_filter);
1145        f = kzalloc(len, GFP_KERNEL);
1146        if (!f)
1147                return;
1148
1149        list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
1150                if (cf->del) {
1151                        memcpy(f, &cf->f, sizeof(struct virtchnl_filter));
1152                        cf->del = false;
1153                        cf->state = __IAVF_CF_DEL_PENDING;
1154                        iavf_send_pf_msg(adapter, VIRTCHNL_OP_DEL_CLOUD_FILTER,
1155                                         (u8 *)f, len);
1156                }
1157        }
1158        kfree(f);
1159}
1160
1161/**
1162 * iavf_request_reset
1163 * @adapter: adapter structure
1164 *
1165 * Request that the PF reset this VF. No response is expected.
1166 **/
1167void iavf_request_reset(struct iavf_adapter *adapter)
1168{
1169        /* Don't check CURRENT_OP - this is always higher priority */
1170        iavf_send_pf_msg(adapter, VIRTCHNL_OP_RESET_VF, NULL, 0);
1171        adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1172}
1173
1174/**
1175 * iavf_virtchnl_completion
1176 * @adapter: adapter structure
1177 * @v_opcode: opcode sent by PF
1178 * @v_retval: retval sent by PF
1179 * @msg: message sent by PF
1180 * @msglen: message length
1181 *
1182 * Asynchronous completion function for admin queue messages. Rather than busy
1183 * wait, we fire off our requests and assume that no errors will be returned.
1184 * This function handles the reply messages.
1185 **/
1186void iavf_virtchnl_completion(struct iavf_adapter *adapter,
1187                              enum virtchnl_ops v_opcode, iavf_status v_retval,
1188                              u8 *msg, u16 msglen)
1189{
1190        struct net_device *netdev = adapter->netdev;
1191
1192        if (v_opcode == VIRTCHNL_OP_EVENT) {
1193                struct virtchnl_pf_event *vpe =
1194                        (struct virtchnl_pf_event *)msg;
1195                bool link_up = vpe->event_data.link_event.link_status;
1196
1197                switch (vpe->event) {
1198                case VIRTCHNL_EVENT_LINK_CHANGE:
1199                        adapter->link_speed =
1200                                vpe->event_data.link_event.link_speed;
1201
1202                        /* we've already got the right link status, bail */
1203                        if (adapter->link_up == link_up)
1204                                break;
1205
1206                        if (link_up) {
1207                                /* If we get link up message and start queues
1208                                 * before our queues are configured it will
1209                                 * trigger a TX hang. In that case, just ignore
1210                                 * the link status message,we'll get another one
1211                                 * after we enable queues and actually prepared
1212                                 * to send traffic.
1213                                 */
1214                                if (adapter->state != __IAVF_RUNNING)
1215                                        break;
1216
1217                                /* For ADq enabled VF, we reconfigure VSIs and
1218                                 * re-allocate queues. Hence wait till all
1219                                 * queues are enabled.
1220                                 */
1221                                if (adapter->flags &
1222                                    IAVF_FLAG_QUEUES_DISABLED)
1223                                        break;
1224                        }
1225
1226                        adapter->link_up = link_up;
1227                        if (link_up) {
1228                                netif_tx_start_all_queues(netdev);
1229                                netif_carrier_on(netdev);
1230                        } else {
1231                                netif_tx_stop_all_queues(netdev);
1232                                netif_carrier_off(netdev);
1233                        }
1234                        iavf_print_link_message(adapter);
1235                        break;
1236                case VIRTCHNL_EVENT_RESET_IMPENDING:
1237                        dev_info(&adapter->pdev->dev, "Reset warning received from the PF\n");
1238                        if (!(adapter->flags & IAVF_FLAG_RESET_PENDING)) {
1239                                adapter->flags |= IAVF_FLAG_RESET_PENDING;
1240                                dev_info(&adapter->pdev->dev, "Scheduling reset task\n");
1241                                schedule_work(&adapter->reset_task);
1242                        }
1243                        break;
1244                default:
1245                        dev_err(&adapter->pdev->dev, "Unknown event %d from PF\n",
1246                                vpe->event);
1247                        break;
1248                }
1249                return;
1250        }
1251        if (v_retval) {
1252                switch (v_opcode) {
1253                case VIRTCHNL_OP_ADD_VLAN:
1254                        dev_err(&adapter->pdev->dev, "Failed to add VLAN filter, error %s\n",
1255                                iavf_stat_str(&adapter->hw, v_retval));
1256                        break;
1257                case VIRTCHNL_OP_ADD_ETH_ADDR:
1258                        dev_err(&adapter->pdev->dev, "Failed to add MAC filter, error %s\n",
1259                                iavf_stat_str(&adapter->hw, v_retval));
1260                        break;
1261                case VIRTCHNL_OP_DEL_VLAN:
1262                        dev_err(&adapter->pdev->dev, "Failed to delete VLAN filter, error %s\n",
1263                                iavf_stat_str(&adapter->hw, v_retval));
1264                        break;
1265                case VIRTCHNL_OP_DEL_ETH_ADDR:
1266                        dev_err(&adapter->pdev->dev, "Failed to delete MAC filter, error %s\n",
1267                                iavf_stat_str(&adapter->hw, v_retval));
1268                        break;
1269                case VIRTCHNL_OP_ENABLE_CHANNELS:
1270                        dev_err(&adapter->pdev->dev, "Failed to configure queue channels, error %s\n",
1271                                iavf_stat_str(&adapter->hw, v_retval));
1272                        adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1273                        adapter->ch_config.state = __IAVF_TC_INVALID;
1274                        netdev_reset_tc(netdev);
1275                        netif_tx_start_all_queues(netdev);
1276                        break;
1277                case VIRTCHNL_OP_DISABLE_CHANNELS:
1278                        dev_err(&adapter->pdev->dev, "Failed to disable queue channels, error %s\n",
1279                                iavf_stat_str(&adapter->hw, v_retval));
1280                        adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1281                        adapter->ch_config.state = __IAVF_TC_RUNNING;
1282                        netif_tx_start_all_queues(netdev);
1283                        break;
1284                case VIRTCHNL_OP_ADD_CLOUD_FILTER: {
1285                        struct iavf_cloud_filter *cf, *cftmp;
1286
1287                        list_for_each_entry_safe(cf, cftmp,
1288                                                 &adapter->cloud_filter_list,
1289                                                 list) {
1290                                if (cf->state == __IAVF_CF_ADD_PENDING) {
1291                                        cf->state = __IAVF_CF_INVALID;
1292                                        dev_info(&adapter->pdev->dev, "Failed to add cloud filter, error %s\n",
1293                                                 iavf_stat_str(&adapter->hw,
1294                                                               v_retval));
1295                                        iavf_print_cloud_filter(adapter,
1296                                                                &cf->f);
1297                                        list_del(&cf->list);
1298                                        kfree(cf);
1299                                        adapter->num_cloud_filters--;
1300                                }
1301                        }
1302                        }
1303                        break;
1304                case VIRTCHNL_OP_DEL_CLOUD_FILTER: {
1305                        struct iavf_cloud_filter *cf;
1306
1307                        list_for_each_entry(cf, &adapter->cloud_filter_list,
1308                                            list) {
1309                                if (cf->state == __IAVF_CF_DEL_PENDING) {
1310                                        cf->state = __IAVF_CF_ACTIVE;
1311                                        dev_info(&adapter->pdev->dev, "Failed to del cloud filter, error %s\n",
1312                                                 iavf_stat_str(&adapter->hw,
1313                                                               v_retval));
1314                                        iavf_print_cloud_filter(adapter,
1315                                                                &cf->f);
1316                                }
1317                        }
1318                        }
1319                        break;
1320                default:
1321                        dev_err(&adapter->pdev->dev, "PF returned error %d (%s) to our request %d\n",
1322                                v_retval, iavf_stat_str(&adapter->hw, v_retval),
1323                                v_opcode);
1324                }
1325        }
1326        switch (v_opcode) {
1327        case VIRTCHNL_OP_GET_STATS: {
1328                struct iavf_eth_stats *stats =
1329                        (struct iavf_eth_stats *)msg;
1330                netdev->stats.rx_packets = stats->rx_unicast +
1331                                           stats->rx_multicast +
1332                                           stats->rx_broadcast;
1333                netdev->stats.tx_packets = stats->tx_unicast +
1334                                           stats->tx_multicast +
1335                                           stats->tx_broadcast;
1336                netdev->stats.rx_bytes = stats->rx_bytes;
1337                netdev->stats.tx_bytes = stats->tx_bytes;
1338                netdev->stats.tx_errors = stats->tx_errors;
1339                netdev->stats.rx_dropped = stats->rx_discards;
1340                netdev->stats.tx_dropped = stats->tx_discards;
1341                adapter->current_stats = *stats;
1342                }
1343                break;
1344        case VIRTCHNL_OP_GET_VF_RESOURCES: {
1345                u16 len = sizeof(struct virtchnl_vf_resource) +
1346                          IAVF_MAX_VF_VSI *
1347                          sizeof(struct virtchnl_vsi_resource);
1348                memcpy(adapter->vf_res, msg, min(msglen, len));
1349                iavf_validate_num_queues(adapter);
1350                iavf_vf_parse_hw_config(&adapter->hw, adapter->vf_res);
1351                if (is_zero_ether_addr(adapter->hw.mac.addr)) {
1352                        /* restore current mac address */
1353                        ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
1354                } else {
1355                        /* refresh current mac address if changed */
1356                        ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
1357                        ether_addr_copy(netdev->perm_addr,
1358                                        adapter->hw.mac.addr);
1359                }
1360                iavf_process_config(adapter);
1361                }
1362                break;
1363        case VIRTCHNL_OP_ENABLE_QUEUES:
1364                /* enable transmits */
1365                iavf_irq_enable(adapter, true);
1366                adapter->flags &= ~IAVF_FLAG_QUEUES_DISABLED;
1367                break;
1368        case VIRTCHNL_OP_DISABLE_QUEUES:
1369                iavf_free_all_tx_resources(adapter);
1370                iavf_free_all_rx_resources(adapter);
1371                if (adapter->state == __IAVF_DOWN_PENDING) {
1372                        adapter->state = __IAVF_DOWN;
1373                        wake_up(&adapter->down_waitqueue);
1374                }
1375                break;
1376        case VIRTCHNL_OP_VERSION:
1377        case VIRTCHNL_OP_CONFIG_IRQ_MAP:
1378                /* Don't display an error if we get these out of sequence.
1379                 * If the firmware needed to get kicked, we'll get these and
1380                 * it's no problem.
1381                 */
1382                if (v_opcode != adapter->current_op)
1383                        return;
1384                break;
1385        case VIRTCHNL_OP_IWARP:
1386                /* Gobble zero-length replies from the PF. They indicate that
1387                 * a previous message was received OK, and the client doesn't
1388                 * care about that.
1389                 */
1390                if (msglen && CLIENT_ENABLED(adapter))
1391                        iavf_notify_client_message(&adapter->vsi, msg, msglen);
1392                break;
1393
1394        case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
1395                adapter->client_pending &=
1396                                ~(BIT(VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP));
1397                break;
1398        case VIRTCHNL_OP_GET_RSS_HENA_CAPS: {
1399                struct virtchnl_rss_hena *vrh = (struct virtchnl_rss_hena *)msg;
1400
1401                if (msglen == sizeof(*vrh))
1402                        adapter->hena = vrh->hena;
1403                else
1404                        dev_warn(&adapter->pdev->dev,
1405                                 "Invalid message %d from PF\n", v_opcode);
1406                }
1407                break;
1408        case VIRTCHNL_OP_REQUEST_QUEUES: {
1409                struct virtchnl_vf_res_request *vfres =
1410                        (struct virtchnl_vf_res_request *)msg;
1411
1412                if (vfres->num_queue_pairs != adapter->num_req_queues) {
1413                        dev_info(&adapter->pdev->dev,
1414                                 "Requested %d queues, PF can support %d\n",
1415                                 adapter->num_req_queues,
1416                                 vfres->num_queue_pairs);
1417                        adapter->num_req_queues = 0;
1418                        adapter->flags &= ~IAVF_FLAG_REINIT_ITR_NEEDED;
1419                }
1420                }
1421                break;
1422        case VIRTCHNL_OP_ADD_CLOUD_FILTER: {
1423                struct iavf_cloud_filter *cf;
1424
1425                list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
1426                        if (cf->state == __IAVF_CF_ADD_PENDING)
1427                                cf->state = __IAVF_CF_ACTIVE;
1428                }
1429                }
1430                break;
1431        case VIRTCHNL_OP_DEL_CLOUD_FILTER: {
1432                struct iavf_cloud_filter *cf, *cftmp;
1433
1434                list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
1435                                         list) {
1436                        if (cf->state == __IAVF_CF_DEL_PENDING) {
1437                                cf->state = __IAVF_CF_INVALID;
1438                                list_del(&cf->list);
1439                                kfree(cf);
1440                                adapter->num_cloud_filters--;
1441                        }
1442                }
1443                }
1444                break;
1445        default:
1446                if (adapter->current_op && (v_opcode != adapter->current_op))
1447                        dev_warn(&adapter->pdev->dev, "Expected response %d from PF, received %d\n",
1448                                 adapter->current_op, v_opcode);
1449                break;
1450        } /* switch v_opcode */
1451        adapter->current_op = VIRTCHNL_OP_UNKNOWN;
1452}
1453