18077c727SJoshua Hay // SPDX-License-Identifier: GPL-2.0-only 28077c727SJoshua Hay /* Copyright (C) 2023 Intel Corporation */ 38077c727SJoshua Hay 48077c727SJoshua Hay #include "idpf.h" 55dc283faSAlan Brady #include "idpf_virtchnl.h" 68077c727SJoshua Hay 71c325aacSAlan Brady static const struct net_device_ops idpf_netdev_ops_splitq; 81c325aacSAlan Brady static const struct net_device_ops idpf_netdev_ops_singleq; 91c325aacSAlan Brady 104930fbf4SPavan Kumar Linga /** 114930fbf4SPavan Kumar Linga * idpf_init_vector_stack - Fill the MSIX vector stack with vector index 124930fbf4SPavan Kumar Linga * @adapter: private data struct 134930fbf4SPavan Kumar Linga * 144930fbf4SPavan Kumar Linga * Return 0 on success, error on failure 154930fbf4SPavan Kumar Linga */ 164930fbf4SPavan Kumar Linga static int idpf_init_vector_stack(struct idpf_adapter *adapter) 174930fbf4SPavan Kumar Linga { 184930fbf4SPavan Kumar Linga struct idpf_vector_lifo *stack; 194930fbf4SPavan Kumar Linga u16 min_vec; 204930fbf4SPavan Kumar Linga u32 i; 214930fbf4SPavan Kumar Linga 224930fbf4SPavan Kumar Linga mutex_lock(&adapter->vector_lock); 234930fbf4SPavan Kumar Linga min_vec = adapter->num_msix_entries - adapter->num_avail_msix; 244930fbf4SPavan Kumar Linga stack = &adapter->vector_stack; 254930fbf4SPavan Kumar Linga stack->size = adapter->num_msix_entries; 264930fbf4SPavan Kumar Linga /* set the base and top to point at start of the 'free pool' to 274930fbf4SPavan Kumar Linga * distribute the unused vectors on-demand basis 284930fbf4SPavan Kumar Linga */ 294930fbf4SPavan Kumar Linga stack->base = min_vec; 304930fbf4SPavan Kumar Linga stack->top = min_vec; 314930fbf4SPavan Kumar Linga 324930fbf4SPavan Kumar Linga stack->vec_idx = kcalloc(stack->size, sizeof(u16), GFP_KERNEL); 334930fbf4SPavan Kumar Linga if (!stack->vec_idx) { 344930fbf4SPavan Kumar Linga mutex_unlock(&adapter->vector_lock); 354930fbf4SPavan Kumar Linga 364930fbf4SPavan Kumar Linga return -ENOMEM; 374930fbf4SPavan Kumar Linga } 384930fbf4SPavan Kumar Linga 394930fbf4SPavan Kumar Linga for (i = 0; i < stack->size; i++) 404930fbf4SPavan Kumar Linga stack->vec_idx[i] = i; 414930fbf4SPavan Kumar Linga 424930fbf4SPavan Kumar Linga mutex_unlock(&adapter->vector_lock); 434930fbf4SPavan Kumar Linga 444930fbf4SPavan Kumar Linga return 0; 454930fbf4SPavan Kumar Linga } 464930fbf4SPavan Kumar Linga 474930fbf4SPavan Kumar Linga /** 484930fbf4SPavan Kumar Linga * idpf_deinit_vector_stack - zero out the MSIX vector stack 494930fbf4SPavan Kumar Linga * @adapter: private data struct 504930fbf4SPavan Kumar Linga */ 514930fbf4SPavan Kumar Linga static void idpf_deinit_vector_stack(struct idpf_adapter *adapter) 524930fbf4SPavan Kumar Linga { 534930fbf4SPavan Kumar Linga struct idpf_vector_lifo *stack; 544930fbf4SPavan Kumar Linga 554930fbf4SPavan Kumar Linga mutex_lock(&adapter->vector_lock); 564930fbf4SPavan Kumar Linga stack = &adapter->vector_stack; 574930fbf4SPavan Kumar Linga kfree(stack->vec_idx); 584930fbf4SPavan Kumar Linga stack->vec_idx = NULL; 594930fbf4SPavan Kumar Linga mutex_unlock(&adapter->vector_lock); 604930fbf4SPavan Kumar Linga } 614930fbf4SPavan Kumar Linga 624930fbf4SPavan Kumar Linga /** 634930fbf4SPavan Kumar Linga * idpf_mb_intr_rel_irq - Free the IRQ association with the OS 644930fbf4SPavan Kumar Linga * @adapter: adapter structure 654930fbf4SPavan Kumar Linga * 664930fbf4SPavan Kumar Linga * This will also disable interrupt mode and queue up mailbox task. Mailbox 674930fbf4SPavan Kumar Linga * task will reschedule itself if not in interrupt mode. 684930fbf4SPavan Kumar Linga */ 694930fbf4SPavan Kumar Linga static void idpf_mb_intr_rel_irq(struct idpf_adapter *adapter) 704930fbf4SPavan Kumar Linga { 714930fbf4SPavan Kumar Linga clear_bit(IDPF_MB_INTR_MODE, adapter->flags); 724930fbf4SPavan Kumar Linga free_irq(adapter->msix_entries[0].vector, adapter); 734930fbf4SPavan Kumar Linga queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0); 744930fbf4SPavan Kumar Linga } 754930fbf4SPavan Kumar Linga 764930fbf4SPavan Kumar Linga /** 774930fbf4SPavan Kumar Linga * idpf_intr_rel - Release interrupt capabilities and free memory 784930fbf4SPavan Kumar Linga * @adapter: adapter to disable interrupts on 794930fbf4SPavan Kumar Linga */ 804930fbf4SPavan Kumar Linga void idpf_intr_rel(struct idpf_adapter *adapter) 814930fbf4SPavan Kumar Linga { 824930fbf4SPavan Kumar Linga if (!adapter->msix_entries) 834930fbf4SPavan Kumar Linga return; 844930fbf4SPavan Kumar Linga 854930fbf4SPavan Kumar Linga idpf_mb_intr_rel_irq(adapter); 864930fbf4SPavan Kumar Linga pci_free_irq_vectors(adapter->pdev); 876009e63cSAlan Brady idpf_send_dealloc_vectors_msg(adapter); 884930fbf4SPavan Kumar Linga idpf_deinit_vector_stack(adapter); 894930fbf4SPavan Kumar Linga kfree(adapter->msix_entries); 904930fbf4SPavan Kumar Linga adapter->msix_entries = NULL; 914930fbf4SPavan Kumar Linga } 924930fbf4SPavan Kumar Linga 934930fbf4SPavan Kumar Linga /** 944930fbf4SPavan Kumar Linga * idpf_mb_intr_clean - Interrupt handler for the mailbox 954930fbf4SPavan Kumar Linga * @irq: interrupt number 964930fbf4SPavan Kumar Linga * @data: pointer to the adapter structure 974930fbf4SPavan Kumar Linga */ 984930fbf4SPavan Kumar Linga static irqreturn_t idpf_mb_intr_clean(int __always_unused irq, void *data) 994930fbf4SPavan Kumar Linga { 1004930fbf4SPavan Kumar Linga struct idpf_adapter *adapter = (struct idpf_adapter *)data; 1014930fbf4SPavan Kumar Linga 1024930fbf4SPavan Kumar Linga queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0); 1034930fbf4SPavan Kumar Linga 1044930fbf4SPavan Kumar Linga return IRQ_HANDLED; 1054930fbf4SPavan Kumar Linga } 1064930fbf4SPavan Kumar Linga 1074930fbf4SPavan Kumar Linga /** 1084930fbf4SPavan Kumar Linga * idpf_mb_irq_enable - Enable MSIX interrupt for the mailbox 1094930fbf4SPavan Kumar Linga * @adapter: adapter to get the hardware address for register write 1104930fbf4SPavan Kumar Linga */ 1114930fbf4SPavan Kumar Linga static void idpf_mb_irq_enable(struct idpf_adapter *adapter) 1124930fbf4SPavan Kumar Linga { 1134930fbf4SPavan Kumar Linga struct idpf_intr_reg *intr = &adapter->mb_vector.intr_reg; 1144930fbf4SPavan Kumar Linga u32 val; 1154930fbf4SPavan Kumar Linga 1164930fbf4SPavan Kumar Linga val = intr->dyn_ctl_intena_m | intr->dyn_ctl_itridx_m; 1174930fbf4SPavan Kumar Linga writel(val, intr->dyn_ctl); 1184930fbf4SPavan Kumar Linga writel(intr->icr_ena_ctlq_m, intr->icr_ena); 1194930fbf4SPavan Kumar Linga } 1204930fbf4SPavan Kumar Linga 1214930fbf4SPavan Kumar Linga /** 1224930fbf4SPavan Kumar Linga * idpf_mb_intr_req_irq - Request irq for the mailbox interrupt 1234930fbf4SPavan Kumar Linga * @adapter: adapter structure to pass to the mailbox irq handler 1244930fbf4SPavan Kumar Linga */ 1254930fbf4SPavan Kumar Linga static int idpf_mb_intr_req_irq(struct idpf_adapter *adapter) 1264930fbf4SPavan Kumar Linga { 1274930fbf4SPavan Kumar Linga struct idpf_q_vector *mb_vector = &adapter->mb_vector; 1284930fbf4SPavan Kumar Linga int irq_num, mb_vidx = 0, err; 1294930fbf4SPavan Kumar Linga 1304930fbf4SPavan Kumar Linga irq_num = adapter->msix_entries[mb_vidx].vector; 1314930fbf4SPavan Kumar Linga mb_vector->name = kasprintf(GFP_KERNEL, "%s-%s-%d", 1324930fbf4SPavan Kumar Linga dev_driver_string(&adapter->pdev->dev), 1334930fbf4SPavan Kumar Linga "Mailbox", mb_vidx); 1344930fbf4SPavan Kumar Linga err = request_irq(irq_num, adapter->irq_mb_handler, 0, 1354930fbf4SPavan Kumar Linga mb_vector->name, adapter); 1364930fbf4SPavan Kumar Linga if (err) { 1374930fbf4SPavan Kumar Linga dev_err(&adapter->pdev->dev, 1384930fbf4SPavan Kumar Linga "IRQ request for mailbox failed, error: %d\n", err); 1394930fbf4SPavan Kumar Linga 1404930fbf4SPavan Kumar Linga return err; 1414930fbf4SPavan Kumar Linga } 1424930fbf4SPavan Kumar Linga 1434930fbf4SPavan Kumar Linga set_bit(IDPF_MB_INTR_MODE, adapter->flags); 1444930fbf4SPavan Kumar Linga 1454930fbf4SPavan Kumar Linga return 0; 1464930fbf4SPavan Kumar Linga } 1474930fbf4SPavan Kumar Linga 1484930fbf4SPavan Kumar Linga /** 1494930fbf4SPavan Kumar Linga * idpf_set_mb_vec_id - Set vector index for mailbox 1504930fbf4SPavan Kumar Linga * @adapter: adapter structure to access the vector chunks 1514930fbf4SPavan Kumar Linga * 1524930fbf4SPavan Kumar Linga * The first vector id in the requested vector chunks from the CP is for 1534930fbf4SPavan Kumar Linga * the mailbox 1544930fbf4SPavan Kumar Linga */ 1554930fbf4SPavan Kumar Linga static void idpf_set_mb_vec_id(struct idpf_adapter *adapter) 1564930fbf4SPavan Kumar Linga { 1574930fbf4SPavan Kumar Linga if (adapter->req_vec_chunks) 1584930fbf4SPavan Kumar Linga adapter->mb_vector.v_idx = 1594930fbf4SPavan Kumar Linga le16_to_cpu(adapter->caps.mailbox_vector_id); 1604930fbf4SPavan Kumar Linga else 1614930fbf4SPavan Kumar Linga adapter->mb_vector.v_idx = 0; 1624930fbf4SPavan Kumar Linga } 1634930fbf4SPavan Kumar Linga 1644930fbf4SPavan Kumar Linga /** 1654930fbf4SPavan Kumar Linga * idpf_mb_intr_init - Initialize the mailbox interrupt 1664930fbf4SPavan Kumar Linga * @adapter: adapter structure to store the mailbox vector 1674930fbf4SPavan Kumar Linga */ 1684930fbf4SPavan Kumar Linga static int idpf_mb_intr_init(struct idpf_adapter *adapter) 1694930fbf4SPavan Kumar Linga { 1704930fbf4SPavan Kumar Linga adapter->dev_ops.reg_ops.mb_intr_reg_init(adapter); 1714930fbf4SPavan Kumar Linga adapter->irq_mb_handler = idpf_mb_intr_clean; 1724930fbf4SPavan Kumar Linga 1734930fbf4SPavan Kumar Linga return idpf_mb_intr_req_irq(adapter); 1744930fbf4SPavan Kumar Linga } 1754930fbf4SPavan Kumar Linga 1764930fbf4SPavan Kumar Linga /** 177d4d55871SPavan Kumar Linga * idpf_vector_lifo_push - push MSIX vector index onto stack 178d4d55871SPavan Kumar Linga * @adapter: private data struct 179d4d55871SPavan Kumar Linga * @vec_idx: vector index to store 180d4d55871SPavan Kumar Linga */ 181d4d55871SPavan Kumar Linga static int idpf_vector_lifo_push(struct idpf_adapter *adapter, u16 vec_idx) 182d4d55871SPavan Kumar Linga { 183d4d55871SPavan Kumar Linga struct idpf_vector_lifo *stack = &adapter->vector_stack; 184d4d55871SPavan Kumar Linga 185d4d55871SPavan Kumar Linga lockdep_assert_held(&adapter->vector_lock); 186d4d55871SPavan Kumar Linga 187d4d55871SPavan Kumar Linga if (stack->top == stack->base) { 188d4d55871SPavan Kumar Linga dev_err(&adapter->pdev->dev, "Exceeded the vector stack limit: %d\n", 189d4d55871SPavan Kumar Linga stack->top); 190d4d55871SPavan Kumar Linga return -EINVAL; 191d4d55871SPavan Kumar Linga } 192d4d55871SPavan Kumar Linga 193d4d55871SPavan Kumar Linga stack->vec_idx[--stack->top] = vec_idx; 194d4d55871SPavan Kumar Linga 195d4d55871SPavan Kumar Linga return 0; 196d4d55871SPavan Kumar Linga } 197d4d55871SPavan Kumar Linga 198d4d55871SPavan Kumar Linga /** 199d4d55871SPavan Kumar Linga * idpf_vector_lifo_pop - pop MSIX vector index from stack 200d4d55871SPavan Kumar Linga * @adapter: private data struct 201d4d55871SPavan Kumar Linga */ 202d4d55871SPavan Kumar Linga static int idpf_vector_lifo_pop(struct idpf_adapter *adapter) 203d4d55871SPavan Kumar Linga { 204d4d55871SPavan Kumar Linga struct idpf_vector_lifo *stack = &adapter->vector_stack; 205d4d55871SPavan Kumar Linga 206d4d55871SPavan Kumar Linga lockdep_assert_held(&adapter->vector_lock); 207d4d55871SPavan Kumar Linga 208d4d55871SPavan Kumar Linga if (stack->top == stack->size) { 209d4d55871SPavan Kumar Linga dev_err(&adapter->pdev->dev, "No interrupt vectors are available to distribute!\n"); 210d4d55871SPavan Kumar Linga 211d4d55871SPavan Kumar Linga return -EINVAL; 212d4d55871SPavan Kumar Linga } 213d4d55871SPavan Kumar Linga 214d4d55871SPavan Kumar Linga return stack->vec_idx[stack->top++]; 215d4d55871SPavan Kumar Linga } 216d4d55871SPavan Kumar Linga 217d4d55871SPavan Kumar Linga /** 218d4d55871SPavan Kumar Linga * idpf_vector_stash - Store the vector indexes onto the stack 219d4d55871SPavan Kumar Linga * @adapter: private data struct 220d4d55871SPavan Kumar Linga * @q_vector_idxs: vector index array 221d4d55871SPavan Kumar Linga * @vec_info: info related to the number of vectors 222d4d55871SPavan Kumar Linga * 223d4d55871SPavan Kumar Linga * This function is a no-op if there are no vectors indexes to be stashed 224d4d55871SPavan Kumar Linga */ 225d4d55871SPavan Kumar Linga static void idpf_vector_stash(struct idpf_adapter *adapter, u16 *q_vector_idxs, 226d4d55871SPavan Kumar Linga struct idpf_vector_info *vec_info) 227d4d55871SPavan Kumar Linga { 228d4d55871SPavan Kumar Linga int i, base = 0; 229d4d55871SPavan Kumar Linga u16 vec_idx; 230d4d55871SPavan Kumar Linga 231d4d55871SPavan Kumar Linga lockdep_assert_held(&adapter->vector_lock); 232d4d55871SPavan Kumar Linga 233d4d55871SPavan Kumar Linga if (!vec_info->num_curr_vecs) 234d4d55871SPavan Kumar Linga return; 235d4d55871SPavan Kumar Linga 236d4d55871SPavan Kumar Linga /* For default vports, no need to stash vector allocated from the 237d4d55871SPavan Kumar Linga * default pool onto the stack 238d4d55871SPavan Kumar Linga */ 239d4d55871SPavan Kumar Linga if (vec_info->default_vport) 240d4d55871SPavan Kumar Linga base = IDPF_MIN_Q_VEC; 241d4d55871SPavan Kumar Linga 242d4d55871SPavan Kumar Linga for (i = vec_info->num_curr_vecs - 1; i >= base ; i--) { 243d4d55871SPavan Kumar Linga vec_idx = q_vector_idxs[i]; 244d4d55871SPavan Kumar Linga idpf_vector_lifo_push(adapter, vec_idx); 245d4d55871SPavan Kumar Linga adapter->num_avail_msix++; 246d4d55871SPavan Kumar Linga } 247d4d55871SPavan Kumar Linga } 248d4d55871SPavan Kumar Linga 249d4d55871SPavan Kumar Linga /** 250d4d55871SPavan Kumar Linga * idpf_req_rel_vector_indexes - Request or release MSIX vector indexes 251d4d55871SPavan Kumar Linga * @adapter: driver specific private structure 252d4d55871SPavan Kumar Linga * @q_vector_idxs: vector index array 253d4d55871SPavan Kumar Linga * @vec_info: info related to the number of vectors 254d4d55871SPavan Kumar Linga * 255d4d55871SPavan Kumar Linga * This is the core function to distribute the MSIX vectors acquired from the 256d4d55871SPavan Kumar Linga * OS. It expects the caller to pass the number of vectors required and 257d4d55871SPavan Kumar Linga * also previously allocated. First, it stashes previously allocated vector 258d4d55871SPavan Kumar Linga * indexes on to the stack and then figures out if it can allocate requested 259d4d55871SPavan Kumar Linga * vectors. It can wait on acquiring the mutex lock. If the caller passes 0 as 260d4d55871SPavan Kumar Linga * requested vectors, then this function just stashes the already allocated 261d4d55871SPavan Kumar Linga * vectors and returns 0. 262d4d55871SPavan Kumar Linga * 263d4d55871SPavan Kumar Linga * Returns actual number of vectors allocated on success, error value on failure 264d4d55871SPavan Kumar Linga * If 0 is returned, implies the stack has no vectors to allocate which is also 265d4d55871SPavan Kumar Linga * a failure case for the caller 266d4d55871SPavan Kumar Linga */ 267d4d55871SPavan Kumar Linga int idpf_req_rel_vector_indexes(struct idpf_adapter *adapter, 268d4d55871SPavan Kumar Linga u16 *q_vector_idxs, 269d4d55871SPavan Kumar Linga struct idpf_vector_info *vec_info) 270d4d55871SPavan Kumar Linga { 271d4d55871SPavan Kumar Linga u16 num_req_vecs, num_alloc_vecs = 0, max_vecs; 272d4d55871SPavan Kumar Linga struct idpf_vector_lifo *stack; 273d4d55871SPavan Kumar Linga int i, j, vecid; 274d4d55871SPavan Kumar Linga 275d4d55871SPavan Kumar Linga mutex_lock(&adapter->vector_lock); 276d4d55871SPavan Kumar Linga stack = &adapter->vector_stack; 277d4d55871SPavan Kumar Linga num_req_vecs = vec_info->num_req_vecs; 278d4d55871SPavan Kumar Linga 279d4d55871SPavan Kumar Linga /* Stash interrupt vector indexes onto the stack if required */ 280d4d55871SPavan Kumar Linga idpf_vector_stash(adapter, q_vector_idxs, vec_info); 281d4d55871SPavan Kumar Linga 282d4d55871SPavan Kumar Linga if (!num_req_vecs) 283d4d55871SPavan Kumar Linga goto rel_lock; 284d4d55871SPavan Kumar Linga 285d4d55871SPavan Kumar Linga if (vec_info->default_vport) { 286d4d55871SPavan Kumar Linga /* As IDPF_MIN_Q_VEC per default vport is put aside in the 287d4d55871SPavan Kumar Linga * default pool of the stack, use them for default vports 288d4d55871SPavan Kumar Linga */ 289d4d55871SPavan Kumar Linga j = vec_info->index * IDPF_MIN_Q_VEC + IDPF_MBX_Q_VEC; 290d4d55871SPavan Kumar Linga for (i = 0; i < IDPF_MIN_Q_VEC; i++) { 291d4d55871SPavan Kumar Linga q_vector_idxs[num_alloc_vecs++] = stack->vec_idx[j++]; 292d4d55871SPavan Kumar Linga num_req_vecs--; 293d4d55871SPavan Kumar Linga } 294d4d55871SPavan Kumar Linga } 295d4d55871SPavan Kumar Linga 296d4d55871SPavan Kumar Linga /* Find if stack has enough vector to allocate */ 297d4d55871SPavan Kumar Linga max_vecs = min(adapter->num_avail_msix, num_req_vecs); 298d4d55871SPavan Kumar Linga 299d4d55871SPavan Kumar Linga for (j = 0; j < max_vecs; j++) { 300d4d55871SPavan Kumar Linga vecid = idpf_vector_lifo_pop(adapter); 301d4d55871SPavan Kumar Linga q_vector_idxs[num_alloc_vecs++] = vecid; 302d4d55871SPavan Kumar Linga } 303d4d55871SPavan Kumar Linga adapter->num_avail_msix -= max_vecs; 304d4d55871SPavan Kumar Linga 305d4d55871SPavan Kumar Linga rel_lock: 306d4d55871SPavan Kumar Linga mutex_unlock(&adapter->vector_lock); 307d4d55871SPavan Kumar Linga 308d4d55871SPavan Kumar Linga return num_alloc_vecs; 309d4d55871SPavan Kumar Linga } 310d4d55871SPavan Kumar Linga 311d4d55871SPavan Kumar Linga /** 3124930fbf4SPavan Kumar Linga * idpf_intr_req - Request interrupt capabilities 3134930fbf4SPavan Kumar Linga * @adapter: adapter to enable interrupts on 3144930fbf4SPavan Kumar Linga * 3154930fbf4SPavan Kumar Linga * Returns 0 on success, negative on failure 3164930fbf4SPavan Kumar Linga */ 3174930fbf4SPavan Kumar Linga int idpf_intr_req(struct idpf_adapter *adapter) 3184930fbf4SPavan Kumar Linga { 3194930fbf4SPavan Kumar Linga u16 default_vports = idpf_get_default_vports(adapter); 3204930fbf4SPavan Kumar Linga int num_q_vecs, total_vecs, num_vec_ids; 3214930fbf4SPavan Kumar Linga int min_vectors, v_actual, err; 3224930fbf4SPavan Kumar Linga unsigned int vector; 3234930fbf4SPavan Kumar Linga u16 *vecids; 3244930fbf4SPavan Kumar Linga 3254930fbf4SPavan Kumar Linga total_vecs = idpf_get_reserved_vecs(adapter); 3264930fbf4SPavan Kumar Linga num_q_vecs = total_vecs - IDPF_MBX_Q_VEC; 3274930fbf4SPavan Kumar Linga 3284930fbf4SPavan Kumar Linga err = idpf_send_alloc_vectors_msg(adapter, num_q_vecs); 3294930fbf4SPavan Kumar Linga if (err) { 3304930fbf4SPavan Kumar Linga dev_err(&adapter->pdev->dev, 3314930fbf4SPavan Kumar Linga "Failed to allocate %d vectors: %d\n", num_q_vecs, err); 3324930fbf4SPavan Kumar Linga 3334930fbf4SPavan Kumar Linga return -EAGAIN; 3344930fbf4SPavan Kumar Linga } 3354930fbf4SPavan Kumar Linga 3364930fbf4SPavan Kumar Linga min_vectors = IDPF_MBX_Q_VEC + IDPF_MIN_Q_VEC * default_vports; 3374930fbf4SPavan Kumar Linga v_actual = pci_alloc_irq_vectors(adapter->pdev, min_vectors, 3384930fbf4SPavan Kumar Linga total_vecs, PCI_IRQ_MSIX); 3394930fbf4SPavan Kumar Linga if (v_actual < min_vectors) { 3404930fbf4SPavan Kumar Linga dev_err(&adapter->pdev->dev, "Failed to allocate MSIX vectors: %d\n", 3414930fbf4SPavan Kumar Linga v_actual); 3424930fbf4SPavan Kumar Linga err = -EAGAIN; 3434930fbf4SPavan Kumar Linga goto send_dealloc_vecs; 3444930fbf4SPavan Kumar Linga } 3454930fbf4SPavan Kumar Linga 3464930fbf4SPavan Kumar Linga adapter->msix_entries = kcalloc(v_actual, sizeof(struct msix_entry), 3474930fbf4SPavan Kumar Linga GFP_KERNEL); 3484930fbf4SPavan Kumar Linga 3494930fbf4SPavan Kumar Linga if (!adapter->msix_entries) { 3504930fbf4SPavan Kumar Linga err = -ENOMEM; 3514930fbf4SPavan Kumar Linga goto free_irq; 3524930fbf4SPavan Kumar Linga } 3534930fbf4SPavan Kumar Linga 3544930fbf4SPavan Kumar Linga idpf_set_mb_vec_id(adapter); 3554930fbf4SPavan Kumar Linga 3564930fbf4SPavan Kumar Linga vecids = kcalloc(total_vecs, sizeof(u16), GFP_KERNEL); 3574930fbf4SPavan Kumar Linga if (!vecids) { 3584930fbf4SPavan Kumar Linga err = -ENOMEM; 3594930fbf4SPavan Kumar Linga goto free_msix; 3604930fbf4SPavan Kumar Linga } 3614930fbf4SPavan Kumar Linga 3624930fbf4SPavan Kumar Linga if (adapter->req_vec_chunks) { 3634930fbf4SPavan Kumar Linga struct virtchnl2_vector_chunks *vchunks; 3644930fbf4SPavan Kumar Linga struct virtchnl2_alloc_vectors *ac; 3654930fbf4SPavan Kumar Linga 3664930fbf4SPavan Kumar Linga ac = adapter->req_vec_chunks; 3674930fbf4SPavan Kumar Linga vchunks = &ac->vchunks; 3684930fbf4SPavan Kumar Linga 3694930fbf4SPavan Kumar Linga num_vec_ids = idpf_get_vec_ids(adapter, vecids, total_vecs, 3704930fbf4SPavan Kumar Linga vchunks); 3714930fbf4SPavan Kumar Linga if (num_vec_ids < v_actual) { 3724930fbf4SPavan Kumar Linga err = -EINVAL; 3734930fbf4SPavan Kumar Linga goto free_vecids; 3744930fbf4SPavan Kumar Linga } 3754930fbf4SPavan Kumar Linga } else { 3764930fbf4SPavan Kumar Linga int i; 3774930fbf4SPavan Kumar Linga 3784930fbf4SPavan Kumar Linga for (i = 0; i < v_actual; i++) 3794930fbf4SPavan Kumar Linga vecids[i] = i; 3804930fbf4SPavan Kumar Linga } 3814930fbf4SPavan Kumar Linga 3824930fbf4SPavan Kumar Linga for (vector = 0; vector < v_actual; vector++) { 3834930fbf4SPavan Kumar Linga adapter->msix_entries[vector].entry = vecids[vector]; 3844930fbf4SPavan Kumar Linga adapter->msix_entries[vector].vector = 3854930fbf4SPavan Kumar Linga pci_irq_vector(adapter->pdev, vector); 3864930fbf4SPavan Kumar Linga } 3874930fbf4SPavan Kumar Linga 3884930fbf4SPavan Kumar Linga adapter->num_req_msix = total_vecs; 3894930fbf4SPavan Kumar Linga adapter->num_msix_entries = v_actual; 3904930fbf4SPavan Kumar Linga /* 'num_avail_msix' is used to distribute excess vectors to the vports 3914930fbf4SPavan Kumar Linga * after considering the minimum vectors required per each default 3924930fbf4SPavan Kumar Linga * vport 3934930fbf4SPavan Kumar Linga */ 3944930fbf4SPavan Kumar Linga adapter->num_avail_msix = v_actual - min_vectors; 3954930fbf4SPavan Kumar Linga 3964930fbf4SPavan Kumar Linga /* Fill MSIX vector lifo stack with vector indexes */ 3974930fbf4SPavan Kumar Linga err = idpf_init_vector_stack(adapter); 3984930fbf4SPavan Kumar Linga if (err) 3994930fbf4SPavan Kumar Linga goto free_vecids; 4004930fbf4SPavan Kumar Linga 4014930fbf4SPavan Kumar Linga err = idpf_mb_intr_init(adapter); 4024930fbf4SPavan Kumar Linga if (err) 4034930fbf4SPavan Kumar Linga goto deinit_vec_stack; 4044930fbf4SPavan Kumar Linga idpf_mb_irq_enable(adapter); 4054930fbf4SPavan Kumar Linga kfree(vecids); 4064930fbf4SPavan Kumar Linga 4074930fbf4SPavan Kumar Linga return 0; 4084930fbf4SPavan Kumar Linga 4094930fbf4SPavan Kumar Linga deinit_vec_stack: 4104930fbf4SPavan Kumar Linga idpf_deinit_vector_stack(adapter); 4114930fbf4SPavan Kumar Linga free_vecids: 4124930fbf4SPavan Kumar Linga kfree(vecids); 4134930fbf4SPavan Kumar Linga free_msix: 4144930fbf4SPavan Kumar Linga kfree(adapter->msix_entries); 4154930fbf4SPavan Kumar Linga adapter->msix_entries = NULL; 4164930fbf4SPavan Kumar Linga free_irq: 4174930fbf4SPavan Kumar Linga pci_free_irq_vectors(adapter->pdev); 4184930fbf4SPavan Kumar Linga send_dealloc_vecs: 4194930fbf4SPavan Kumar Linga idpf_send_dealloc_vectors_msg(adapter); 4204930fbf4SPavan Kumar Linga 4214930fbf4SPavan Kumar Linga return err; 4224930fbf4SPavan Kumar Linga } 4234930fbf4SPavan Kumar Linga 4244930fbf4SPavan Kumar Linga /** 425ce1b75d0SPavan Kumar Linga * idpf_find_mac_filter - Search filter list for specific mac filter 426ce1b75d0SPavan Kumar Linga * @vconfig: Vport config structure 427ce1b75d0SPavan Kumar Linga * @macaddr: The MAC address 428ce1b75d0SPavan Kumar Linga * 429ce1b75d0SPavan Kumar Linga * Returns ptr to the filter object or NULL. Must be called while holding the 430ce1b75d0SPavan Kumar Linga * mac_filter_list_lock. 431ce1b75d0SPavan Kumar Linga **/ 432ce1b75d0SPavan Kumar Linga static struct idpf_mac_filter *idpf_find_mac_filter(struct idpf_vport_config *vconfig, 433ce1b75d0SPavan Kumar Linga const u8 *macaddr) 434ce1b75d0SPavan Kumar Linga { 435ce1b75d0SPavan Kumar Linga struct idpf_mac_filter *f; 436ce1b75d0SPavan Kumar Linga 437ce1b75d0SPavan Kumar Linga if (!macaddr) 438ce1b75d0SPavan Kumar Linga return NULL; 439ce1b75d0SPavan Kumar Linga 440ce1b75d0SPavan Kumar Linga list_for_each_entry(f, &vconfig->user_config.mac_filter_list, list) { 441ce1b75d0SPavan Kumar Linga if (ether_addr_equal(macaddr, f->macaddr)) 442ce1b75d0SPavan Kumar Linga return f; 443ce1b75d0SPavan Kumar Linga } 444ce1b75d0SPavan Kumar Linga 445ce1b75d0SPavan Kumar Linga return NULL; 446ce1b75d0SPavan Kumar Linga } 447ce1b75d0SPavan Kumar Linga 448ce1b75d0SPavan Kumar Linga /** 449a251eee6SJoshua Hay * __idpf_del_mac_filter - Delete a MAC filter from the filter list 450a251eee6SJoshua Hay * @vport_config: Vport config structure 451a251eee6SJoshua Hay * @macaddr: The MAC address 452a251eee6SJoshua Hay * 453a251eee6SJoshua Hay * Returns 0 on success, error value on failure 454a251eee6SJoshua Hay **/ 455a251eee6SJoshua Hay static int __idpf_del_mac_filter(struct idpf_vport_config *vport_config, 456a251eee6SJoshua Hay const u8 *macaddr) 457a251eee6SJoshua Hay { 458a251eee6SJoshua Hay struct idpf_mac_filter *f; 459a251eee6SJoshua Hay 460a251eee6SJoshua Hay spin_lock_bh(&vport_config->mac_filter_list_lock); 461a251eee6SJoshua Hay f = idpf_find_mac_filter(vport_config, macaddr); 462a251eee6SJoshua Hay if (f) { 463a251eee6SJoshua Hay list_del(&f->list); 464a251eee6SJoshua Hay kfree(f); 465a251eee6SJoshua Hay } 466a251eee6SJoshua Hay spin_unlock_bh(&vport_config->mac_filter_list_lock); 467a251eee6SJoshua Hay 468a251eee6SJoshua Hay return 0; 469a251eee6SJoshua Hay } 470a251eee6SJoshua Hay 471a251eee6SJoshua Hay /** 472a251eee6SJoshua Hay * idpf_del_mac_filter - Delete a MAC filter from the filter list 473a251eee6SJoshua Hay * @vport: Main vport structure 474a251eee6SJoshua Hay * @np: Netdev private structure 475a251eee6SJoshua Hay * @macaddr: The MAC address 476a251eee6SJoshua Hay * @async: Don't wait for return message 477a251eee6SJoshua Hay * 478a251eee6SJoshua Hay * Removes filter from list and if interface is up, tells hardware about the 479a251eee6SJoshua Hay * removed filter. 480a251eee6SJoshua Hay **/ 481a251eee6SJoshua Hay static int idpf_del_mac_filter(struct idpf_vport *vport, 482a251eee6SJoshua Hay struct idpf_netdev_priv *np, 483a251eee6SJoshua Hay const u8 *macaddr, bool async) 484a251eee6SJoshua Hay { 485a251eee6SJoshua Hay struct idpf_vport_config *vport_config; 486a251eee6SJoshua Hay struct idpf_mac_filter *f; 487a251eee6SJoshua Hay 488a251eee6SJoshua Hay vport_config = np->adapter->vport_config[np->vport_idx]; 489a251eee6SJoshua Hay 490a251eee6SJoshua Hay spin_lock_bh(&vport_config->mac_filter_list_lock); 491a251eee6SJoshua Hay f = idpf_find_mac_filter(vport_config, macaddr); 492a251eee6SJoshua Hay if (f) { 493a251eee6SJoshua Hay f->remove = true; 494a251eee6SJoshua Hay } else { 495a251eee6SJoshua Hay spin_unlock_bh(&vport_config->mac_filter_list_lock); 496a251eee6SJoshua Hay 497a251eee6SJoshua Hay return -EINVAL; 498a251eee6SJoshua Hay } 499a251eee6SJoshua Hay spin_unlock_bh(&vport_config->mac_filter_list_lock); 500a251eee6SJoshua Hay 501a251eee6SJoshua Hay if (np->state == __IDPF_VPORT_UP) { 502a251eee6SJoshua Hay int err; 503a251eee6SJoshua Hay 504a251eee6SJoshua Hay err = idpf_add_del_mac_filters(vport, np, false, async); 505a251eee6SJoshua Hay if (err) 506a251eee6SJoshua Hay return err; 507a251eee6SJoshua Hay } 508a251eee6SJoshua Hay 509a251eee6SJoshua Hay return __idpf_del_mac_filter(vport_config, macaddr); 510a251eee6SJoshua Hay } 511a251eee6SJoshua Hay 512a251eee6SJoshua Hay /** 513ce1b75d0SPavan Kumar Linga * __idpf_add_mac_filter - Add mac filter helper function 514ce1b75d0SPavan Kumar Linga * @vport_config: Vport config structure 515ce1b75d0SPavan Kumar Linga * @macaddr: Address to add 516ce1b75d0SPavan Kumar Linga * 517ce1b75d0SPavan Kumar Linga * Takes mac_filter_list_lock spinlock to add new filter to list. 518ce1b75d0SPavan Kumar Linga */ 519ce1b75d0SPavan Kumar Linga static int __idpf_add_mac_filter(struct idpf_vport_config *vport_config, 520ce1b75d0SPavan Kumar Linga const u8 *macaddr) 521ce1b75d0SPavan Kumar Linga { 522ce1b75d0SPavan Kumar Linga struct idpf_mac_filter *f; 523ce1b75d0SPavan Kumar Linga 524ce1b75d0SPavan Kumar Linga spin_lock_bh(&vport_config->mac_filter_list_lock); 525ce1b75d0SPavan Kumar Linga 526ce1b75d0SPavan Kumar Linga f = idpf_find_mac_filter(vport_config, macaddr); 527ce1b75d0SPavan Kumar Linga if (f) { 528ce1b75d0SPavan Kumar Linga f->remove = false; 529ce1b75d0SPavan Kumar Linga spin_unlock_bh(&vport_config->mac_filter_list_lock); 530ce1b75d0SPavan Kumar Linga 531ce1b75d0SPavan Kumar Linga return 0; 532ce1b75d0SPavan Kumar Linga } 533ce1b75d0SPavan Kumar Linga 534ce1b75d0SPavan Kumar Linga f = kzalloc(sizeof(*f), GFP_ATOMIC); 535ce1b75d0SPavan Kumar Linga if (!f) { 536ce1b75d0SPavan Kumar Linga spin_unlock_bh(&vport_config->mac_filter_list_lock); 537ce1b75d0SPavan Kumar Linga 538ce1b75d0SPavan Kumar Linga return -ENOMEM; 539ce1b75d0SPavan Kumar Linga } 540ce1b75d0SPavan Kumar Linga 541ce1b75d0SPavan Kumar Linga ether_addr_copy(f->macaddr, macaddr); 542ce1b75d0SPavan Kumar Linga list_add_tail(&f->list, &vport_config->user_config.mac_filter_list); 543ce1b75d0SPavan Kumar Linga f->add = true; 544ce1b75d0SPavan Kumar Linga 545ce1b75d0SPavan Kumar Linga spin_unlock_bh(&vport_config->mac_filter_list_lock); 546ce1b75d0SPavan Kumar Linga 547ce1b75d0SPavan Kumar Linga return 0; 548ce1b75d0SPavan Kumar Linga } 549ce1b75d0SPavan Kumar Linga 550ce1b75d0SPavan Kumar Linga /** 551ce1b75d0SPavan Kumar Linga * idpf_add_mac_filter - Add a mac filter to the filter list 552ce1b75d0SPavan Kumar Linga * @vport: Main vport structure 553ce1b75d0SPavan Kumar Linga * @np: Netdev private structure 554ce1b75d0SPavan Kumar Linga * @macaddr: The MAC address 555ce1b75d0SPavan Kumar Linga * @async: Don't wait for return message 556ce1b75d0SPavan Kumar Linga * 557ce1b75d0SPavan Kumar Linga * Returns 0 on success or error on failure. If interface is up, we'll also 558ce1b75d0SPavan Kumar Linga * send the virtchnl message to tell hardware about the filter. 559ce1b75d0SPavan Kumar Linga **/ 560ce1b75d0SPavan Kumar Linga static int idpf_add_mac_filter(struct idpf_vport *vport, 561ce1b75d0SPavan Kumar Linga struct idpf_netdev_priv *np, 562ce1b75d0SPavan Kumar Linga const u8 *macaddr, bool async) 563ce1b75d0SPavan Kumar Linga { 564ce1b75d0SPavan Kumar Linga struct idpf_vport_config *vport_config; 565ce1b75d0SPavan Kumar Linga int err; 566ce1b75d0SPavan Kumar Linga 567ce1b75d0SPavan Kumar Linga vport_config = np->adapter->vport_config[np->vport_idx]; 568ce1b75d0SPavan Kumar Linga err = __idpf_add_mac_filter(vport_config, macaddr); 569ce1b75d0SPavan Kumar Linga if (err) 570ce1b75d0SPavan Kumar Linga return err; 571ce1b75d0SPavan Kumar Linga 572ce1b75d0SPavan Kumar Linga if (np->state == __IDPF_VPORT_UP) 573ce1b75d0SPavan Kumar Linga err = idpf_add_del_mac_filters(vport, np, true, async); 574ce1b75d0SPavan Kumar Linga 575ce1b75d0SPavan Kumar Linga return err; 576ce1b75d0SPavan Kumar Linga } 577ce1b75d0SPavan Kumar Linga 578ce1b75d0SPavan Kumar Linga /** 579a251eee6SJoshua Hay * idpf_del_all_mac_filters - Delete all MAC filters in list 580a251eee6SJoshua Hay * @vport: main vport struct 581a251eee6SJoshua Hay * 582a251eee6SJoshua Hay * Takes mac_filter_list_lock spinlock. Deletes all filters 583a251eee6SJoshua Hay */ 584a251eee6SJoshua Hay static void idpf_del_all_mac_filters(struct idpf_vport *vport) 585a251eee6SJoshua Hay { 586a251eee6SJoshua Hay struct idpf_vport_config *vport_config; 587a251eee6SJoshua Hay struct idpf_mac_filter *f, *ftmp; 588a251eee6SJoshua Hay 589a251eee6SJoshua Hay vport_config = vport->adapter->vport_config[vport->idx]; 590a251eee6SJoshua Hay spin_lock_bh(&vport_config->mac_filter_list_lock); 591a251eee6SJoshua Hay 592a251eee6SJoshua Hay list_for_each_entry_safe(f, ftmp, &vport_config->user_config.mac_filter_list, 593a251eee6SJoshua Hay list) { 594a251eee6SJoshua Hay list_del(&f->list); 595a251eee6SJoshua Hay kfree(f); 596a251eee6SJoshua Hay } 597a251eee6SJoshua Hay 598a251eee6SJoshua Hay spin_unlock_bh(&vport_config->mac_filter_list_lock); 599a251eee6SJoshua Hay } 600a251eee6SJoshua Hay 601a251eee6SJoshua Hay /** 602a251eee6SJoshua Hay * idpf_restore_mac_filters - Re-add all MAC filters in list 603a251eee6SJoshua Hay * @vport: main vport struct 604a251eee6SJoshua Hay * 605a251eee6SJoshua Hay * Takes mac_filter_list_lock spinlock. Sets add field to true for filters to 606a251eee6SJoshua Hay * resync filters back to HW. 607a251eee6SJoshua Hay */ 608a251eee6SJoshua Hay static void idpf_restore_mac_filters(struct idpf_vport *vport) 609a251eee6SJoshua Hay { 610a251eee6SJoshua Hay struct idpf_vport_config *vport_config; 611a251eee6SJoshua Hay struct idpf_mac_filter *f; 612a251eee6SJoshua Hay 613a251eee6SJoshua Hay vport_config = vport->adapter->vport_config[vport->idx]; 614a251eee6SJoshua Hay spin_lock_bh(&vport_config->mac_filter_list_lock); 615a251eee6SJoshua Hay 616a251eee6SJoshua Hay list_for_each_entry(f, &vport_config->user_config.mac_filter_list, list) 617a251eee6SJoshua Hay f->add = true; 618a251eee6SJoshua Hay 619a251eee6SJoshua Hay spin_unlock_bh(&vport_config->mac_filter_list_lock); 620a251eee6SJoshua Hay 621a251eee6SJoshua Hay idpf_add_del_mac_filters(vport, netdev_priv(vport->netdev), 622a251eee6SJoshua Hay true, false); 623a251eee6SJoshua Hay } 624a251eee6SJoshua Hay 625a251eee6SJoshua Hay /** 626a251eee6SJoshua Hay * idpf_remove_mac_filters - Remove all MAC filters in list 627a251eee6SJoshua Hay * @vport: main vport struct 628a251eee6SJoshua Hay * 629a251eee6SJoshua Hay * Takes mac_filter_list_lock spinlock. Sets remove field to true for filters 630a251eee6SJoshua Hay * to remove filters in HW. 631a251eee6SJoshua Hay */ 632a251eee6SJoshua Hay static void idpf_remove_mac_filters(struct idpf_vport *vport) 633a251eee6SJoshua Hay { 634a251eee6SJoshua Hay struct idpf_vport_config *vport_config; 635a251eee6SJoshua Hay struct idpf_mac_filter *f; 636a251eee6SJoshua Hay 637a251eee6SJoshua Hay vport_config = vport->adapter->vport_config[vport->idx]; 638a251eee6SJoshua Hay spin_lock_bh(&vport_config->mac_filter_list_lock); 639a251eee6SJoshua Hay 640a251eee6SJoshua Hay list_for_each_entry(f, &vport_config->user_config.mac_filter_list, list) 641a251eee6SJoshua Hay f->remove = true; 642a251eee6SJoshua Hay 643a251eee6SJoshua Hay spin_unlock_bh(&vport_config->mac_filter_list_lock); 644a251eee6SJoshua Hay 645a251eee6SJoshua Hay idpf_add_del_mac_filters(vport, netdev_priv(vport->netdev), 646a251eee6SJoshua Hay false, false); 647a251eee6SJoshua Hay } 648a251eee6SJoshua Hay 649a251eee6SJoshua Hay /** 650ce1b75d0SPavan Kumar Linga * idpf_deinit_mac_addr - deinitialize mac address for vport 651ce1b75d0SPavan Kumar Linga * @vport: main vport structure 652ce1b75d0SPavan Kumar Linga */ 653ce1b75d0SPavan Kumar Linga static void idpf_deinit_mac_addr(struct idpf_vport *vport) 654ce1b75d0SPavan Kumar Linga { 655ce1b75d0SPavan Kumar Linga struct idpf_vport_config *vport_config; 656ce1b75d0SPavan Kumar Linga struct idpf_mac_filter *f; 657ce1b75d0SPavan Kumar Linga 658ce1b75d0SPavan Kumar Linga vport_config = vport->adapter->vport_config[vport->idx]; 659ce1b75d0SPavan Kumar Linga 660ce1b75d0SPavan Kumar Linga spin_lock_bh(&vport_config->mac_filter_list_lock); 661ce1b75d0SPavan Kumar Linga 662ce1b75d0SPavan Kumar Linga f = idpf_find_mac_filter(vport_config, vport->default_mac_addr); 663ce1b75d0SPavan Kumar Linga if (f) { 664ce1b75d0SPavan Kumar Linga list_del(&f->list); 665ce1b75d0SPavan Kumar Linga kfree(f); 666ce1b75d0SPavan Kumar Linga } 667ce1b75d0SPavan Kumar Linga 668ce1b75d0SPavan Kumar Linga spin_unlock_bh(&vport_config->mac_filter_list_lock); 669ce1b75d0SPavan Kumar Linga } 670ce1b75d0SPavan Kumar Linga 671ce1b75d0SPavan Kumar Linga /** 672ce1b75d0SPavan Kumar Linga * idpf_init_mac_addr - initialize mac address for vport 673ce1b75d0SPavan Kumar Linga * @vport: main vport structure 674ce1b75d0SPavan Kumar Linga * @netdev: pointer to netdev struct associated with this vport 675ce1b75d0SPavan Kumar Linga */ 676ce1b75d0SPavan Kumar Linga static int idpf_init_mac_addr(struct idpf_vport *vport, 677ce1b75d0SPavan Kumar Linga struct net_device *netdev) 678ce1b75d0SPavan Kumar Linga { 679ce1b75d0SPavan Kumar Linga struct idpf_netdev_priv *np = netdev_priv(netdev); 680ce1b75d0SPavan Kumar Linga struct idpf_adapter *adapter = vport->adapter; 681ce1b75d0SPavan Kumar Linga int err; 682ce1b75d0SPavan Kumar Linga 683ce1b75d0SPavan Kumar Linga if (is_valid_ether_addr(vport->default_mac_addr)) { 684ce1b75d0SPavan Kumar Linga eth_hw_addr_set(netdev, vport->default_mac_addr); 685ce1b75d0SPavan Kumar Linga ether_addr_copy(netdev->perm_addr, vport->default_mac_addr); 686ce1b75d0SPavan Kumar Linga 687ce1b75d0SPavan Kumar Linga return idpf_add_mac_filter(vport, np, vport->default_mac_addr, 688ce1b75d0SPavan Kumar Linga false); 689ce1b75d0SPavan Kumar Linga } 690ce1b75d0SPavan Kumar Linga 691ce1b75d0SPavan Kumar Linga if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, 692ce1b75d0SPavan Kumar Linga VIRTCHNL2_CAP_MACFILTER)) { 693ce1b75d0SPavan Kumar Linga dev_err(&adapter->pdev->dev, 694ce1b75d0SPavan Kumar Linga "MAC address is not provided and capability is not set\n"); 695ce1b75d0SPavan Kumar Linga 696ce1b75d0SPavan Kumar Linga return -EINVAL; 697ce1b75d0SPavan Kumar Linga } 698ce1b75d0SPavan Kumar Linga 699ce1b75d0SPavan Kumar Linga eth_hw_addr_random(netdev); 700ce1b75d0SPavan Kumar Linga err = idpf_add_mac_filter(vport, np, netdev->dev_addr, false); 701ce1b75d0SPavan Kumar Linga if (err) 702ce1b75d0SPavan Kumar Linga return err; 703ce1b75d0SPavan Kumar Linga 704ce1b75d0SPavan Kumar Linga dev_info(&adapter->pdev->dev, "Invalid MAC address %pM, using random %pM\n", 705ce1b75d0SPavan Kumar Linga vport->default_mac_addr, netdev->dev_addr); 706ce1b75d0SPavan Kumar Linga ether_addr_copy(vport->default_mac_addr, netdev->dev_addr); 707ce1b75d0SPavan Kumar Linga 708ce1b75d0SPavan Kumar Linga return 0; 709ce1b75d0SPavan Kumar Linga } 710ce1b75d0SPavan Kumar Linga 711ce1b75d0SPavan Kumar Linga /** 7120fe45467SPavan Kumar Linga * idpf_cfg_netdev - Allocate, configure and register a netdev 7130fe45467SPavan Kumar Linga * @vport: main vport structure 7140fe45467SPavan Kumar Linga * 7150fe45467SPavan Kumar Linga * Returns 0 on success, negative value on failure. 7160fe45467SPavan Kumar Linga */ 7170fe45467SPavan Kumar Linga static int idpf_cfg_netdev(struct idpf_vport *vport) 7180fe45467SPavan Kumar Linga { 7190fe45467SPavan Kumar Linga struct idpf_adapter *adapter = vport->adapter; 7200fe45467SPavan Kumar Linga struct idpf_vport_config *vport_config; 7210fe45467SPavan Kumar Linga netdev_features_t dflt_features; 7220fe45467SPavan Kumar Linga netdev_features_t offloads = 0; 7230fe45467SPavan Kumar Linga struct idpf_netdev_priv *np; 7240fe45467SPavan Kumar Linga struct net_device *netdev; 7250fe45467SPavan Kumar Linga u16 idx = vport->idx; 726ce1b75d0SPavan Kumar Linga int err; 7270fe45467SPavan Kumar Linga 7280fe45467SPavan Kumar Linga vport_config = adapter->vport_config[idx]; 7290fe45467SPavan Kumar Linga 7300fe45467SPavan Kumar Linga /* It's possible we already have a netdev allocated and registered for 7310fe45467SPavan Kumar Linga * this vport 7320fe45467SPavan Kumar Linga */ 7330fe45467SPavan Kumar Linga if (test_bit(IDPF_VPORT_REG_NETDEV, vport_config->flags)) { 7340fe45467SPavan Kumar Linga netdev = adapter->netdevs[idx]; 7350fe45467SPavan Kumar Linga np = netdev_priv(netdev); 7360fe45467SPavan Kumar Linga np->vport = vport; 737ce1b75d0SPavan Kumar Linga np->vport_idx = vport->idx; 738ce1b75d0SPavan Kumar Linga np->vport_id = vport->vport_id; 7390fe45467SPavan Kumar Linga vport->netdev = netdev; 7400fe45467SPavan Kumar Linga 741ce1b75d0SPavan Kumar Linga return idpf_init_mac_addr(vport, netdev); 7420fe45467SPavan Kumar Linga } 7430fe45467SPavan Kumar Linga 7440fe45467SPavan Kumar Linga netdev = alloc_etherdev_mqs(sizeof(struct idpf_netdev_priv), 7450fe45467SPavan Kumar Linga vport_config->max_q.max_txq, 7460fe45467SPavan Kumar Linga vport_config->max_q.max_rxq); 7470fe45467SPavan Kumar Linga if (!netdev) 7480fe45467SPavan Kumar Linga return -ENOMEM; 7490fe45467SPavan Kumar Linga 7500fe45467SPavan Kumar Linga vport->netdev = netdev; 7510fe45467SPavan Kumar Linga np = netdev_priv(netdev); 7520fe45467SPavan Kumar Linga np->vport = vport; 753ce1b75d0SPavan Kumar Linga np->adapter = adapter; 754ce1b75d0SPavan Kumar Linga np->vport_idx = vport->idx; 755ce1b75d0SPavan Kumar Linga np->vport_id = vport->vport_id; 756ce1b75d0SPavan Kumar Linga 757a251eee6SJoshua Hay spin_lock_init(&np->stats_lock); 758a251eee6SJoshua Hay 759ce1b75d0SPavan Kumar Linga err = idpf_init_mac_addr(vport, netdev); 760ce1b75d0SPavan Kumar Linga if (err) { 761ce1b75d0SPavan Kumar Linga free_netdev(vport->netdev); 762ce1b75d0SPavan Kumar Linga vport->netdev = NULL; 763ce1b75d0SPavan Kumar Linga 764ce1b75d0SPavan Kumar Linga return err; 765ce1b75d0SPavan Kumar Linga } 7660fe45467SPavan Kumar Linga 7671c325aacSAlan Brady /* assign netdev_ops */ 7681c325aacSAlan Brady if (idpf_is_queue_model_split(vport->txq_model)) 7691c325aacSAlan Brady netdev->netdev_ops = &idpf_netdev_ops_splitq; 7701c325aacSAlan Brady else 7711c325aacSAlan Brady netdev->netdev_ops = &idpf_netdev_ops_singleq; 7721c325aacSAlan Brady 7730fe45467SPavan Kumar Linga /* setup watchdog timeout value to be 5 second */ 7740fe45467SPavan Kumar Linga netdev->watchdog_timeo = 5 * HZ; 7750fe45467SPavan Kumar Linga 776359724faSMichal Schmidt netdev->dev_port = idx; 777359724faSMichal Schmidt 7780fe45467SPavan Kumar Linga /* configure default MTU size */ 7790fe45467SPavan Kumar Linga netdev->min_mtu = ETH_MIN_MTU; 7800fe45467SPavan Kumar Linga netdev->max_mtu = vport->max_mtu; 7810fe45467SPavan Kumar Linga 7820fe45467SPavan Kumar Linga dflt_features = NETIF_F_SG | 7830fe45467SPavan Kumar Linga NETIF_F_HIGHDMA; 7840fe45467SPavan Kumar Linga 7850fe45467SPavan Kumar Linga if (idpf_is_cap_ena_all(adapter, IDPF_RSS_CAPS, IDPF_CAP_RSS)) 7860fe45467SPavan Kumar Linga dflt_features |= NETIF_F_RXHASH; 7870fe45467SPavan Kumar Linga if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_RX_CSUM_L4V4)) 7880fe45467SPavan Kumar Linga dflt_features |= NETIF_F_IP_CSUM; 7890fe45467SPavan Kumar Linga if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_RX_CSUM_L4V6)) 7900fe45467SPavan Kumar Linga dflt_features |= NETIF_F_IPV6_CSUM; 7910fe45467SPavan Kumar Linga if (idpf_is_cap_ena(adapter, IDPF_CSUM_CAPS, IDPF_CAP_RX_CSUM)) 7920fe45467SPavan Kumar Linga dflt_features |= NETIF_F_RXCSUM; 7930fe45467SPavan Kumar Linga if (idpf_is_cap_ena_all(adapter, IDPF_CSUM_CAPS, IDPF_CAP_SCTP_CSUM)) 7940fe45467SPavan Kumar Linga dflt_features |= NETIF_F_SCTP_CRC; 7950fe45467SPavan Kumar Linga 7960fe45467SPavan Kumar Linga if (idpf_is_cap_ena(adapter, IDPF_SEG_CAPS, VIRTCHNL2_CAP_SEG_IPV4_TCP)) 7970fe45467SPavan Kumar Linga dflt_features |= NETIF_F_TSO; 7980fe45467SPavan Kumar Linga if (idpf_is_cap_ena(adapter, IDPF_SEG_CAPS, VIRTCHNL2_CAP_SEG_IPV6_TCP)) 7990fe45467SPavan Kumar Linga dflt_features |= NETIF_F_TSO6; 8000fe45467SPavan Kumar Linga if (idpf_is_cap_ena_all(adapter, IDPF_SEG_CAPS, 8010fe45467SPavan Kumar Linga VIRTCHNL2_CAP_SEG_IPV4_UDP | 8020fe45467SPavan Kumar Linga VIRTCHNL2_CAP_SEG_IPV6_UDP)) 8030fe45467SPavan Kumar Linga dflt_features |= NETIF_F_GSO_UDP_L4; 8040fe45467SPavan Kumar Linga if (idpf_is_cap_ena_all(adapter, IDPF_RSC_CAPS, IDPF_CAP_RSC)) 8050fe45467SPavan Kumar Linga offloads |= NETIF_F_GRO_HW; 8060fe45467SPavan Kumar Linga /* advertise to stack only if offloads for encapsulated packets is 8070fe45467SPavan Kumar Linga * supported 8080fe45467SPavan Kumar Linga */ 8090fe45467SPavan Kumar Linga if (idpf_is_cap_ena(vport->adapter, IDPF_SEG_CAPS, 8100fe45467SPavan Kumar Linga VIRTCHNL2_CAP_SEG_TX_SINGLE_TUNNEL)) { 8110fe45467SPavan Kumar Linga offloads |= NETIF_F_GSO_UDP_TUNNEL | 8120fe45467SPavan Kumar Linga NETIF_F_GSO_GRE | 8130fe45467SPavan Kumar Linga NETIF_F_GSO_GRE_CSUM | 8140fe45467SPavan Kumar Linga NETIF_F_GSO_PARTIAL | 8150fe45467SPavan Kumar Linga NETIF_F_GSO_UDP_TUNNEL_CSUM | 8160fe45467SPavan Kumar Linga NETIF_F_GSO_IPXIP4 | 8170fe45467SPavan Kumar Linga NETIF_F_GSO_IPXIP6 | 8180fe45467SPavan Kumar Linga 0; 8190fe45467SPavan Kumar Linga 8200fe45467SPavan Kumar Linga if (!idpf_is_cap_ena_all(vport->adapter, IDPF_CSUM_CAPS, 8210fe45467SPavan Kumar Linga IDPF_CAP_TUNNEL_TX_CSUM)) 8220fe45467SPavan Kumar Linga netdev->gso_partial_features |= 8230fe45467SPavan Kumar Linga NETIF_F_GSO_UDP_TUNNEL_CSUM; 8240fe45467SPavan Kumar Linga 8250fe45467SPavan Kumar Linga netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM; 8260fe45467SPavan Kumar Linga offloads |= NETIF_F_TSO_MANGLEID; 8270fe45467SPavan Kumar Linga } 8280fe45467SPavan Kumar Linga if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_LOOPBACK)) 8290fe45467SPavan Kumar Linga offloads |= NETIF_F_LOOPBACK; 8300fe45467SPavan Kumar Linga 8310fe45467SPavan Kumar Linga netdev->features |= dflt_features; 8320fe45467SPavan Kumar Linga netdev->hw_features |= dflt_features | offloads; 8330fe45467SPavan Kumar Linga netdev->hw_enc_features |= dflt_features | offloads; 83402cbfba1SAlan Brady idpf_set_ethtool_ops(netdev); 8350fe45467SPavan Kumar Linga SET_NETDEV_DEV(netdev, &adapter->pdev->dev); 8360fe45467SPavan Kumar Linga 8370fe45467SPavan Kumar Linga /* carrier off on init to avoid Tx hangs */ 8380fe45467SPavan Kumar Linga netif_carrier_off(netdev); 8390fe45467SPavan Kumar Linga 8400fe45467SPavan Kumar Linga /* make sure transmit queues start off as stopped */ 8410fe45467SPavan Kumar Linga netif_tx_stop_all_queues(netdev); 8420fe45467SPavan Kumar Linga 8430fe45467SPavan Kumar Linga /* The vport can be arbitrarily released so we need to also track 8440fe45467SPavan Kumar Linga * netdevs in the adapter struct 8450fe45467SPavan Kumar Linga */ 8460fe45467SPavan Kumar Linga adapter->netdevs[idx] = netdev; 8470fe45467SPavan Kumar Linga 8480fe45467SPavan Kumar Linga return 0; 8490fe45467SPavan Kumar Linga } 8500fe45467SPavan Kumar Linga 8510fe45467SPavan Kumar Linga /** 8520fe45467SPavan Kumar Linga * idpf_get_free_slot - get the next non-NULL location index in array 8530fe45467SPavan Kumar Linga * @adapter: adapter in which to look for a free vport slot 8540fe45467SPavan Kumar Linga */ 8550fe45467SPavan Kumar Linga static int idpf_get_free_slot(struct idpf_adapter *adapter) 8560fe45467SPavan Kumar Linga { 8570fe45467SPavan Kumar Linga unsigned int i; 8580fe45467SPavan Kumar Linga 8590fe45467SPavan Kumar Linga for (i = 0; i < adapter->max_vports; i++) { 8600fe45467SPavan Kumar Linga if (!adapter->vports[i]) 8610fe45467SPavan Kumar Linga return i; 8620fe45467SPavan Kumar Linga } 8630fe45467SPavan Kumar Linga 8640fe45467SPavan Kumar Linga return IDPF_NO_FREE_SLOT; 8650fe45467SPavan Kumar Linga } 8660fe45467SPavan Kumar Linga 8670fe45467SPavan Kumar Linga /** 868a251eee6SJoshua Hay * idpf_remove_features - Turn off feature configs 869a251eee6SJoshua Hay * @vport: virtual port structure 870a251eee6SJoshua Hay */ 871a251eee6SJoshua Hay static void idpf_remove_features(struct idpf_vport *vport) 872a251eee6SJoshua Hay { 873a251eee6SJoshua Hay struct idpf_adapter *adapter = vport->adapter; 874a251eee6SJoshua Hay 875a251eee6SJoshua Hay if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER)) 876a251eee6SJoshua Hay idpf_remove_mac_filters(vport); 877a251eee6SJoshua Hay } 878a251eee6SJoshua Hay 879a251eee6SJoshua Hay /** 8801c325aacSAlan Brady * idpf_vport_stop - Disable a vport 8811c325aacSAlan Brady * @vport: vport to disable 8821c325aacSAlan Brady */ 8831c325aacSAlan Brady static void idpf_vport_stop(struct idpf_vport *vport) 8841c325aacSAlan Brady { 8851c325aacSAlan Brady struct idpf_netdev_priv *np = netdev_priv(vport->netdev); 8861c325aacSAlan Brady 8871c325aacSAlan Brady if (np->state <= __IDPF_VPORT_DOWN) 8881c325aacSAlan Brady return; 8891c325aacSAlan Brady 8901c325aacSAlan Brady netif_carrier_off(vport->netdev); 891d4d55871SPavan Kumar Linga netif_tx_disable(vport->netdev); 8921c325aacSAlan Brady 893d4d55871SPavan Kumar Linga idpf_send_disable_vport_msg(vport); 894d4d55871SPavan Kumar Linga idpf_send_disable_queues_msg(vport); 895d4d55871SPavan Kumar Linga idpf_send_map_unmap_queue_vector_msg(vport, false); 896a251eee6SJoshua Hay /* Normally we ask for queues in create_vport, but if the number of 897a251eee6SJoshua Hay * initially requested queues have changed, for example via ethtool 898a251eee6SJoshua Hay * set channels, we do delete queues and then add the queues back 899a251eee6SJoshua Hay * instead of deleting and reallocating the vport. 90002cbfba1SAlan Brady */ 90102cbfba1SAlan Brady if (test_and_clear_bit(IDPF_VPORT_DEL_QUEUES, vport->flags)) 90202cbfba1SAlan Brady idpf_send_delete_queues_msg(vport); 903d4d55871SPavan Kumar Linga 904a251eee6SJoshua Hay idpf_remove_features(vport); 905a251eee6SJoshua Hay 906d4d55871SPavan Kumar Linga vport->link_up = false; 907d4d55871SPavan Kumar Linga idpf_vport_intr_deinit(vport); 9081c325aacSAlan Brady idpf_vport_intr_rel(vport); 9091c325aacSAlan Brady idpf_vport_queues_rel(vport); 9101c325aacSAlan Brady np->state = __IDPF_VPORT_DOWN; 9111c325aacSAlan Brady } 9121c325aacSAlan Brady 9131c325aacSAlan Brady /** 9141c325aacSAlan Brady * idpf_stop - Disables a network interface 9151c325aacSAlan Brady * @netdev: network interface device structure 9161c325aacSAlan Brady * 9171c325aacSAlan Brady * The stop entry point is called when an interface is de-activated by the OS, 9181c325aacSAlan Brady * and the netdevice enters the DOWN state. The hardware is still under the 9191c325aacSAlan Brady * driver's control, but the netdev interface is disabled. 9201c325aacSAlan Brady * 9211c325aacSAlan Brady * Returns success only - not allowed to fail 9221c325aacSAlan Brady */ 9231c325aacSAlan Brady static int idpf_stop(struct net_device *netdev) 9241c325aacSAlan Brady { 9251c325aacSAlan Brady struct idpf_netdev_priv *np = netdev_priv(netdev); 9261c325aacSAlan Brady struct idpf_vport *vport; 9271c325aacSAlan Brady 9281c325aacSAlan Brady if (test_bit(IDPF_REMOVE_IN_PROG, np->adapter->flags)) 9291c325aacSAlan Brady return 0; 9301c325aacSAlan Brady 9311c325aacSAlan Brady idpf_vport_ctrl_lock(netdev); 9321c325aacSAlan Brady vport = idpf_netdev_to_vport(netdev); 9331c325aacSAlan Brady 9341c325aacSAlan Brady idpf_vport_stop(vport); 9351c325aacSAlan Brady 9361c325aacSAlan Brady idpf_vport_ctrl_unlock(netdev); 9371c325aacSAlan Brady 9381c325aacSAlan Brady return 0; 9391c325aacSAlan Brady } 9401c325aacSAlan Brady 9411c325aacSAlan Brady /** 9420fe45467SPavan Kumar Linga * idpf_decfg_netdev - Unregister the netdev 9430fe45467SPavan Kumar Linga * @vport: vport for which netdev to be unregistered 9440fe45467SPavan Kumar Linga */ 9450fe45467SPavan Kumar Linga static void idpf_decfg_netdev(struct idpf_vport *vport) 9460fe45467SPavan Kumar Linga { 9470fe45467SPavan Kumar Linga struct idpf_adapter *adapter = vport->adapter; 9480fe45467SPavan Kumar Linga 9490fe45467SPavan Kumar Linga unregister_netdev(vport->netdev); 9500fe45467SPavan Kumar Linga free_netdev(vport->netdev); 9510fe45467SPavan Kumar Linga vport->netdev = NULL; 9520fe45467SPavan Kumar Linga 9530fe45467SPavan Kumar Linga adapter->netdevs[vport->idx] = NULL; 9540fe45467SPavan Kumar Linga } 9550fe45467SPavan Kumar Linga 9560fe45467SPavan Kumar Linga /** 9570fe45467SPavan Kumar Linga * idpf_vport_rel - Delete a vport and free its resources 9580fe45467SPavan Kumar Linga * @vport: the vport being removed 9590fe45467SPavan Kumar Linga */ 9600fe45467SPavan Kumar Linga static void idpf_vport_rel(struct idpf_vport *vport) 9610fe45467SPavan Kumar Linga { 9620fe45467SPavan Kumar Linga struct idpf_adapter *adapter = vport->adapter; 9630fe45467SPavan Kumar Linga struct idpf_vport_config *vport_config; 964d4d55871SPavan Kumar Linga struct idpf_vector_info vec_info; 96595af467dSAlan Brady struct idpf_rss_data *rss_data; 9660fe45467SPavan Kumar Linga struct idpf_vport_max_q max_q; 9670fe45467SPavan Kumar Linga u16 idx = vport->idx; 9680fe45467SPavan Kumar Linga 9690fe45467SPavan Kumar Linga vport_config = adapter->vport_config[vport->idx]; 97095af467dSAlan Brady idpf_deinit_rss(vport); 97195af467dSAlan Brady rss_data = &vport_config->user_config.rss_data; 97295af467dSAlan Brady kfree(rss_data->rss_key); 97395af467dSAlan Brady rss_data->rss_key = NULL; 9740fe45467SPavan Kumar Linga 9750fe45467SPavan Kumar Linga idpf_send_destroy_vport_msg(vport); 9760fe45467SPavan Kumar Linga 9770fe45467SPavan Kumar Linga /* Release all max queues allocated to the adapter's pool */ 9780fe45467SPavan Kumar Linga max_q.max_rxq = vport_config->max_q.max_rxq; 9790fe45467SPavan Kumar Linga max_q.max_txq = vport_config->max_q.max_txq; 9800fe45467SPavan Kumar Linga max_q.max_bufq = vport_config->max_q.max_bufq; 9810fe45467SPavan Kumar Linga max_q.max_complq = vport_config->max_q.max_complq; 9820fe45467SPavan Kumar Linga idpf_vport_dealloc_max_qs(adapter, &max_q); 9830fe45467SPavan Kumar Linga 984d4d55871SPavan Kumar Linga /* Release all the allocated vectors on the stack */ 985d4d55871SPavan Kumar Linga vec_info.num_req_vecs = 0; 986d4d55871SPavan Kumar Linga vec_info.num_curr_vecs = vport->num_q_vectors; 987d4d55871SPavan Kumar Linga vec_info.default_vport = vport->default_vport; 988d4d55871SPavan Kumar Linga 989d4d55871SPavan Kumar Linga idpf_req_rel_vector_indexes(adapter, vport->q_vector_idxs, &vec_info); 990d4d55871SPavan Kumar Linga 991d4d55871SPavan Kumar Linga kfree(vport->q_vector_idxs); 992d4d55871SPavan Kumar Linga vport->q_vector_idxs = NULL; 993d4d55871SPavan Kumar Linga 9940fe45467SPavan Kumar Linga kfree(adapter->vport_params_recvd[idx]); 9950fe45467SPavan Kumar Linga adapter->vport_params_recvd[idx] = NULL; 9960fe45467SPavan Kumar Linga kfree(adapter->vport_params_reqd[idx]); 9970fe45467SPavan Kumar Linga adapter->vport_params_reqd[idx] = NULL; 998a251eee6SJoshua Hay if (adapter->vport_config[idx]) { 999a251eee6SJoshua Hay kfree(adapter->vport_config[idx]->req_qs_chunks); 1000a251eee6SJoshua Hay adapter->vport_config[idx]->req_qs_chunks = NULL; 1001a251eee6SJoshua Hay } 10020fe45467SPavan Kumar Linga kfree(vport); 10030fe45467SPavan Kumar Linga adapter->num_alloc_vports--; 10040fe45467SPavan Kumar Linga } 10050fe45467SPavan Kumar Linga 10060fe45467SPavan Kumar Linga /** 10070fe45467SPavan Kumar Linga * idpf_vport_dealloc - cleanup and release a given vport 10080fe45467SPavan Kumar Linga * @vport: pointer to idpf vport structure 10090fe45467SPavan Kumar Linga * 10100fe45467SPavan Kumar Linga * returns nothing 10110fe45467SPavan Kumar Linga */ 10120fe45467SPavan Kumar Linga static void idpf_vport_dealloc(struct idpf_vport *vport) 10130fe45467SPavan Kumar Linga { 10140fe45467SPavan Kumar Linga struct idpf_adapter *adapter = vport->adapter; 10150fe45467SPavan Kumar Linga unsigned int i = vport->idx; 10160fe45467SPavan Kumar Linga 1017ce1b75d0SPavan Kumar Linga idpf_deinit_mac_addr(vport); 1018d4d55871SPavan Kumar Linga idpf_vport_stop(vport); 1019ce1b75d0SPavan Kumar Linga 10200fe45467SPavan Kumar Linga if (!test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags)) 10210fe45467SPavan Kumar Linga idpf_decfg_netdev(vport); 1022a251eee6SJoshua Hay if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags)) 1023a251eee6SJoshua Hay idpf_del_all_mac_filters(vport); 10240fe45467SPavan Kumar Linga 10250fe45467SPavan Kumar Linga if (adapter->netdevs[i]) { 10260fe45467SPavan Kumar Linga struct idpf_netdev_priv *np = netdev_priv(adapter->netdevs[i]); 10270fe45467SPavan Kumar Linga 10280fe45467SPavan Kumar Linga np->vport = NULL; 10290fe45467SPavan Kumar Linga } 10300fe45467SPavan Kumar Linga 10310fe45467SPavan Kumar Linga idpf_vport_rel(vport); 10320fe45467SPavan Kumar Linga 10330fe45467SPavan Kumar Linga adapter->vports[i] = NULL; 10340fe45467SPavan Kumar Linga adapter->next_vport = idpf_get_free_slot(adapter); 10350fe45467SPavan Kumar Linga } 10360fe45467SPavan Kumar Linga 10370fe45467SPavan Kumar Linga /** 10389b1aa3efSMichal Kubiak * idpf_is_hsplit_supported - check whether the header split is supported 10399b1aa3efSMichal Kubiak * @vport: virtual port to check the capability for 10409b1aa3efSMichal Kubiak * 10419b1aa3efSMichal Kubiak * Return: true if it's supported by the HW/FW, false if not. 10429b1aa3efSMichal Kubiak */ 10439b1aa3efSMichal Kubiak static bool idpf_is_hsplit_supported(const struct idpf_vport *vport) 10449b1aa3efSMichal Kubiak { 10459b1aa3efSMichal Kubiak return idpf_is_queue_model_split(vport->rxq_model) && 10469b1aa3efSMichal Kubiak idpf_is_cap_ena_all(vport->adapter, IDPF_HSPLIT_CAPS, 10479b1aa3efSMichal Kubiak IDPF_CAP_HSPLIT); 10489b1aa3efSMichal Kubiak } 10499b1aa3efSMichal Kubiak 10509b1aa3efSMichal Kubiak /** 10519b1aa3efSMichal Kubiak * idpf_vport_get_hsplit - get the current header split feature state 10529b1aa3efSMichal Kubiak * @vport: virtual port to query the state for 10539b1aa3efSMichal Kubiak * 10549b1aa3efSMichal Kubiak * Return: ``ETHTOOL_TCP_DATA_SPLIT_UNKNOWN`` if not supported, 10559b1aa3efSMichal Kubiak * ``ETHTOOL_TCP_DATA_SPLIT_DISABLED`` if disabled, 10569b1aa3efSMichal Kubiak * ``ETHTOOL_TCP_DATA_SPLIT_ENABLED`` if active. 10579b1aa3efSMichal Kubiak */ 10589b1aa3efSMichal Kubiak u8 idpf_vport_get_hsplit(const struct idpf_vport *vport) 10599b1aa3efSMichal Kubiak { 10609b1aa3efSMichal Kubiak const struct idpf_vport_user_config_data *config; 10619b1aa3efSMichal Kubiak 10629b1aa3efSMichal Kubiak if (!idpf_is_hsplit_supported(vport)) 10639b1aa3efSMichal Kubiak return ETHTOOL_TCP_DATA_SPLIT_UNKNOWN; 10649b1aa3efSMichal Kubiak 10659b1aa3efSMichal Kubiak config = &vport->adapter->vport_config[vport->idx]->user_config; 10669b1aa3efSMichal Kubiak 10679b1aa3efSMichal Kubiak return test_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags) ? 10689b1aa3efSMichal Kubiak ETHTOOL_TCP_DATA_SPLIT_ENABLED : 10699b1aa3efSMichal Kubiak ETHTOOL_TCP_DATA_SPLIT_DISABLED; 10709b1aa3efSMichal Kubiak } 10719b1aa3efSMichal Kubiak 10729b1aa3efSMichal Kubiak /** 10739b1aa3efSMichal Kubiak * idpf_vport_set_hsplit - enable or disable header split on a given vport 10749b1aa3efSMichal Kubiak * @vport: virtual port to configure 10759b1aa3efSMichal Kubiak * @val: Ethtool flag controlling the header split state 10769b1aa3efSMichal Kubiak * 10779b1aa3efSMichal Kubiak * Return: true on success, false if not supported by the HW. 10789b1aa3efSMichal Kubiak */ 10799b1aa3efSMichal Kubiak bool idpf_vport_set_hsplit(const struct idpf_vport *vport, u8 val) 10809b1aa3efSMichal Kubiak { 10819b1aa3efSMichal Kubiak struct idpf_vport_user_config_data *config; 10829b1aa3efSMichal Kubiak 10839b1aa3efSMichal Kubiak if (!idpf_is_hsplit_supported(vport)) 10849b1aa3efSMichal Kubiak return val == ETHTOOL_TCP_DATA_SPLIT_UNKNOWN; 10859b1aa3efSMichal Kubiak 10869b1aa3efSMichal Kubiak config = &vport->adapter->vport_config[vport->idx]->user_config; 10879b1aa3efSMichal Kubiak 10889b1aa3efSMichal Kubiak switch (val) { 10899b1aa3efSMichal Kubiak case ETHTOOL_TCP_DATA_SPLIT_UNKNOWN: 10909b1aa3efSMichal Kubiak /* Default is to enable */ 10919b1aa3efSMichal Kubiak case ETHTOOL_TCP_DATA_SPLIT_ENABLED: 10929b1aa3efSMichal Kubiak __set_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags); 10939b1aa3efSMichal Kubiak return true; 10949b1aa3efSMichal Kubiak case ETHTOOL_TCP_DATA_SPLIT_DISABLED: 10959b1aa3efSMichal Kubiak __clear_bit(__IDPF_USER_FLAG_HSPLIT, config->user_flags); 10969b1aa3efSMichal Kubiak return true; 10979b1aa3efSMichal Kubiak default: 10989b1aa3efSMichal Kubiak return false; 10999b1aa3efSMichal Kubiak } 11009b1aa3efSMichal Kubiak } 11019b1aa3efSMichal Kubiak 11029b1aa3efSMichal Kubiak /** 11030fe45467SPavan Kumar Linga * idpf_vport_alloc - Allocates the next available struct vport in the adapter 11040fe45467SPavan Kumar Linga * @adapter: board private structure 11050fe45467SPavan Kumar Linga * @max_q: vport max queue info 11060fe45467SPavan Kumar Linga * 11070fe45467SPavan Kumar Linga * returns a pointer to a vport on success, NULL on failure. 11080fe45467SPavan Kumar Linga */ 11090fe45467SPavan Kumar Linga static struct idpf_vport *idpf_vport_alloc(struct idpf_adapter *adapter, 11100fe45467SPavan Kumar Linga struct idpf_vport_max_q *max_q) 11110fe45467SPavan Kumar Linga { 111295af467dSAlan Brady struct idpf_rss_data *rss_data; 11130fe45467SPavan Kumar Linga u16 idx = adapter->next_vport; 11140fe45467SPavan Kumar Linga struct idpf_vport *vport; 1115d4d55871SPavan Kumar Linga u16 num_max_q; 11160fe45467SPavan Kumar Linga 11170fe45467SPavan Kumar Linga if (idx == IDPF_NO_FREE_SLOT) 11180fe45467SPavan Kumar Linga return NULL; 11190fe45467SPavan Kumar Linga 11200fe45467SPavan Kumar Linga vport = kzalloc(sizeof(*vport), GFP_KERNEL); 11210fe45467SPavan Kumar Linga if (!vport) 11220fe45467SPavan Kumar Linga return vport; 11230fe45467SPavan Kumar Linga 11240fe45467SPavan Kumar Linga if (!adapter->vport_config[idx]) { 11250fe45467SPavan Kumar Linga struct idpf_vport_config *vport_config; 11260fe45467SPavan Kumar Linga 11270fe45467SPavan Kumar Linga vport_config = kzalloc(sizeof(*vport_config), GFP_KERNEL); 11280fe45467SPavan Kumar Linga if (!vport_config) { 11290fe45467SPavan Kumar Linga kfree(vport); 11300fe45467SPavan Kumar Linga 11310fe45467SPavan Kumar Linga return NULL; 11320fe45467SPavan Kumar Linga } 11330fe45467SPavan Kumar Linga 11340fe45467SPavan Kumar Linga adapter->vport_config[idx] = vport_config; 11350fe45467SPavan Kumar Linga } 11360fe45467SPavan Kumar Linga 11370fe45467SPavan Kumar Linga vport->idx = idx; 11380fe45467SPavan Kumar Linga vport->adapter = adapter; 1139c2d548caSJoshua Hay vport->compln_clean_budget = IDPF_TX_COMPLQ_CLEAN_BUDGET; 11400fe45467SPavan Kumar Linga vport->default_vport = adapter->num_alloc_vports < 11410fe45467SPavan Kumar Linga idpf_get_default_vports(adapter); 11420fe45467SPavan Kumar Linga 1143d4d55871SPavan Kumar Linga num_max_q = max(max_q->max_txq, max_q->max_rxq); 1144d4d55871SPavan Kumar Linga vport->q_vector_idxs = kcalloc(num_max_q, sizeof(u16), GFP_KERNEL); 1145d4d55871SPavan Kumar Linga if (!vport->q_vector_idxs) { 1146d4d55871SPavan Kumar Linga kfree(vport); 1147d4d55871SPavan Kumar Linga 1148d4d55871SPavan Kumar Linga return NULL; 1149d4d55871SPavan Kumar Linga } 11500fe45467SPavan Kumar Linga idpf_vport_init(vport, max_q); 11510fe45467SPavan Kumar Linga 115295af467dSAlan Brady /* This alloc is done separate from the LUT because it's not strictly 115395af467dSAlan Brady * dependent on how many queues we have. If we change number of queues 115495af467dSAlan Brady * and soft reset we'll need a new LUT but the key can remain the same 115595af467dSAlan Brady * for as long as the vport exists. 115695af467dSAlan Brady */ 115795af467dSAlan Brady rss_data = &adapter->vport_config[idx]->user_config.rss_data; 115895af467dSAlan Brady rss_data->rss_key = kzalloc(rss_data->rss_key_size, GFP_KERNEL); 115995af467dSAlan Brady if (!rss_data->rss_key) { 116095af467dSAlan Brady kfree(vport); 116195af467dSAlan Brady 116295af467dSAlan Brady return NULL; 116395af467dSAlan Brady } 116495af467dSAlan Brady /* Initialize default rss key */ 116595af467dSAlan Brady netdev_rss_key_fill((void *)rss_data->rss_key, rss_data->rss_key_size); 116695af467dSAlan Brady 11670fe45467SPavan Kumar Linga /* fill vport slot in the adapter struct */ 11680fe45467SPavan Kumar Linga adapter->vports[idx] = vport; 11690fe45467SPavan Kumar Linga adapter->vport_ids[idx] = idpf_get_vport_id(vport); 11700fe45467SPavan Kumar Linga 11710fe45467SPavan Kumar Linga adapter->num_alloc_vports++; 11720fe45467SPavan Kumar Linga /* prepare adapter->next_vport for next use */ 11730fe45467SPavan Kumar Linga adapter->next_vport = idpf_get_free_slot(adapter); 11740fe45467SPavan Kumar Linga 11750fe45467SPavan Kumar Linga return vport; 11760fe45467SPavan Kumar Linga } 11770fe45467SPavan Kumar Linga 11780fe45467SPavan Kumar Linga /** 1179a251eee6SJoshua Hay * idpf_get_stats64 - get statistics for network device structure 1180a251eee6SJoshua Hay * @netdev: network interface device structure 1181a251eee6SJoshua Hay * @stats: main device statistics structure 1182a251eee6SJoshua Hay */ 1183a251eee6SJoshua Hay static void idpf_get_stats64(struct net_device *netdev, 1184a251eee6SJoshua Hay struct rtnl_link_stats64 *stats) 1185a251eee6SJoshua Hay { 1186a251eee6SJoshua Hay struct idpf_netdev_priv *np = netdev_priv(netdev); 1187a251eee6SJoshua Hay 1188a251eee6SJoshua Hay spin_lock_bh(&np->stats_lock); 1189a251eee6SJoshua Hay *stats = np->netstats; 1190a251eee6SJoshua Hay spin_unlock_bh(&np->stats_lock); 1191a251eee6SJoshua Hay } 1192a251eee6SJoshua Hay 1193a251eee6SJoshua Hay /** 1194a251eee6SJoshua Hay * idpf_statistics_task - Delayed task to get statistics over mailbox 1195a251eee6SJoshua Hay * @work: work_struct handle to our data 1196a251eee6SJoshua Hay */ 1197a251eee6SJoshua Hay void idpf_statistics_task(struct work_struct *work) 1198a251eee6SJoshua Hay { 1199a251eee6SJoshua Hay struct idpf_adapter *adapter; 1200a251eee6SJoshua Hay int i; 1201a251eee6SJoshua Hay 1202a251eee6SJoshua Hay adapter = container_of(work, struct idpf_adapter, stats_task.work); 1203a251eee6SJoshua Hay 1204a251eee6SJoshua Hay for (i = 0; i < adapter->max_vports; i++) { 1205a251eee6SJoshua Hay struct idpf_vport *vport = adapter->vports[i]; 1206a251eee6SJoshua Hay 1207a251eee6SJoshua Hay if (vport && !test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags)) 1208a251eee6SJoshua Hay idpf_send_get_stats_msg(vport); 1209a251eee6SJoshua Hay } 1210a251eee6SJoshua Hay 1211a251eee6SJoshua Hay queue_delayed_work(adapter->stats_wq, &adapter->stats_task, 1212a251eee6SJoshua Hay msecs_to_jiffies(10000)); 1213a251eee6SJoshua Hay } 1214a251eee6SJoshua Hay 1215a251eee6SJoshua Hay /** 12164930fbf4SPavan Kumar Linga * idpf_mbx_task - Delayed task to handle mailbox responses 12174930fbf4SPavan Kumar Linga * @work: work_struct handle 12184930fbf4SPavan Kumar Linga */ 12194930fbf4SPavan Kumar Linga void idpf_mbx_task(struct work_struct *work) 12204930fbf4SPavan Kumar Linga { 12214930fbf4SPavan Kumar Linga struct idpf_adapter *adapter; 12224930fbf4SPavan Kumar Linga 12234930fbf4SPavan Kumar Linga adapter = container_of(work, struct idpf_adapter, mbx_task.work); 12244930fbf4SPavan Kumar Linga 12254930fbf4SPavan Kumar Linga if (test_bit(IDPF_MB_INTR_MODE, adapter->flags)) 12264930fbf4SPavan Kumar Linga idpf_mb_irq_enable(adapter); 12274930fbf4SPavan Kumar Linga else 12284930fbf4SPavan Kumar Linga queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 12294930fbf4SPavan Kumar Linga msecs_to_jiffies(300)); 12304930fbf4SPavan Kumar Linga 1231e54232daSAlan Brady idpf_recv_mb_msg(adapter); 12324930fbf4SPavan Kumar Linga } 12334930fbf4SPavan Kumar Linga 12344930fbf4SPavan Kumar Linga /** 12354930fbf4SPavan Kumar Linga * idpf_service_task - Delayed task for handling mailbox responses 12364930fbf4SPavan Kumar Linga * @work: work_struct handle to our data 12374930fbf4SPavan Kumar Linga * 12384930fbf4SPavan Kumar Linga */ 12394930fbf4SPavan Kumar Linga void idpf_service_task(struct work_struct *work) 12404930fbf4SPavan Kumar Linga { 12414930fbf4SPavan Kumar Linga struct idpf_adapter *adapter; 12424930fbf4SPavan Kumar Linga 12434930fbf4SPavan Kumar Linga adapter = container_of(work, struct idpf_adapter, serv_task.work); 12444930fbf4SPavan Kumar Linga 12454930fbf4SPavan Kumar Linga if (idpf_is_reset_detected(adapter) && 12464930fbf4SPavan Kumar Linga !idpf_is_reset_in_prog(adapter) && 12474930fbf4SPavan Kumar Linga !test_bit(IDPF_REMOVE_IN_PROG, adapter->flags)) { 12484930fbf4SPavan Kumar Linga dev_info(&adapter->pdev->dev, "HW reset detected\n"); 12494930fbf4SPavan Kumar Linga set_bit(IDPF_HR_FUNC_RESET, adapter->flags); 12504930fbf4SPavan Kumar Linga queue_delayed_work(adapter->vc_event_wq, 12514930fbf4SPavan Kumar Linga &adapter->vc_event_task, 12524930fbf4SPavan Kumar Linga msecs_to_jiffies(10)); 12534930fbf4SPavan Kumar Linga } 12544930fbf4SPavan Kumar Linga 12554930fbf4SPavan Kumar Linga queue_delayed_work(adapter->serv_wq, &adapter->serv_task, 12564930fbf4SPavan Kumar Linga msecs_to_jiffies(300)); 12574930fbf4SPavan Kumar Linga } 12584930fbf4SPavan Kumar Linga 12598077c727SJoshua Hay /** 1260a251eee6SJoshua Hay * idpf_restore_features - Restore feature configs 1261a251eee6SJoshua Hay * @vport: virtual port structure 1262a251eee6SJoshua Hay */ 1263a251eee6SJoshua Hay static void idpf_restore_features(struct idpf_vport *vport) 1264a251eee6SJoshua Hay { 1265a251eee6SJoshua Hay struct idpf_adapter *adapter = vport->adapter; 1266a251eee6SJoshua Hay 1267a251eee6SJoshua Hay if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER)) 1268a251eee6SJoshua Hay idpf_restore_mac_filters(vport); 1269a251eee6SJoshua Hay } 1270a251eee6SJoshua Hay 1271a251eee6SJoshua Hay /** 127202cbfba1SAlan Brady * idpf_set_real_num_queues - set number of queues for netdev 127302cbfba1SAlan Brady * @vport: virtual port structure 127402cbfba1SAlan Brady * 127502cbfba1SAlan Brady * Returns 0 on success, negative on failure. 127602cbfba1SAlan Brady */ 127702cbfba1SAlan Brady static int idpf_set_real_num_queues(struct idpf_vport *vport) 127802cbfba1SAlan Brady { 127902cbfba1SAlan Brady int err; 128002cbfba1SAlan Brady 128102cbfba1SAlan Brady err = netif_set_real_num_rx_queues(vport->netdev, vport->num_rxq); 128202cbfba1SAlan Brady if (err) 128302cbfba1SAlan Brady return err; 128402cbfba1SAlan Brady 128502cbfba1SAlan Brady return netif_set_real_num_tx_queues(vport->netdev, vport->num_txq); 128602cbfba1SAlan Brady } 128702cbfba1SAlan Brady 128802cbfba1SAlan Brady /** 1289d4d55871SPavan Kumar Linga * idpf_up_complete - Complete interface up sequence 1290d4d55871SPavan Kumar Linga * @vport: virtual port structure 1291d4d55871SPavan Kumar Linga * 1292d4d55871SPavan Kumar Linga * Returns 0 on success, negative on failure. 1293d4d55871SPavan Kumar Linga */ 1294d4d55871SPavan Kumar Linga static int idpf_up_complete(struct idpf_vport *vport) 1295d4d55871SPavan Kumar Linga { 1296d4d55871SPavan Kumar Linga struct idpf_netdev_priv *np = netdev_priv(vport->netdev); 1297d4d55871SPavan Kumar Linga 1298d4d55871SPavan Kumar Linga if (vport->link_up && !netif_carrier_ok(vport->netdev)) { 1299d4d55871SPavan Kumar Linga netif_carrier_on(vport->netdev); 1300d4d55871SPavan Kumar Linga netif_tx_start_all_queues(vport->netdev); 1301d4d55871SPavan Kumar Linga } 1302d4d55871SPavan Kumar Linga 1303d4d55871SPavan Kumar Linga np->state = __IDPF_VPORT_UP; 1304d4d55871SPavan Kumar Linga 1305d4d55871SPavan Kumar Linga return 0; 1306d4d55871SPavan Kumar Linga } 1307d4d55871SPavan Kumar Linga 1308d4d55871SPavan Kumar Linga /** 1309d4d55871SPavan Kumar Linga * idpf_rx_init_buf_tail - Write initial buffer ring tail value 1310d4d55871SPavan Kumar Linga * @vport: virtual port struct 1311d4d55871SPavan Kumar Linga */ 1312d4d55871SPavan Kumar Linga static void idpf_rx_init_buf_tail(struct idpf_vport *vport) 1313d4d55871SPavan Kumar Linga { 1314d4d55871SPavan Kumar Linga int i, j; 1315d4d55871SPavan Kumar Linga 1316d4d55871SPavan Kumar Linga for (i = 0; i < vport->num_rxq_grp; i++) { 1317d4d55871SPavan Kumar Linga struct idpf_rxq_group *grp = &vport->rxq_grps[i]; 1318d4d55871SPavan Kumar Linga 1319d4d55871SPavan Kumar Linga if (idpf_is_queue_model_split(vport->rxq_model)) { 1320d4d55871SPavan Kumar Linga for (j = 0; j < vport->num_bufqs_per_qgrp; j++) { 1321*e4891e46SAlexander Lobakin const struct idpf_buf_queue *q = 1322d4d55871SPavan Kumar Linga &grp->splitq.bufq_sets[j].bufq; 1323d4d55871SPavan Kumar Linga 1324d4d55871SPavan Kumar Linga writel(q->next_to_alloc, q->tail); 1325d4d55871SPavan Kumar Linga } 1326d4d55871SPavan Kumar Linga } else { 1327d4d55871SPavan Kumar Linga for (j = 0; j < grp->singleq.num_rxq; j++) { 1328*e4891e46SAlexander Lobakin const struct idpf_rx_queue *q = 1329d4d55871SPavan Kumar Linga grp->singleq.rxqs[j]; 1330d4d55871SPavan Kumar Linga 1331d4d55871SPavan Kumar Linga writel(q->next_to_alloc, q->tail); 1332d4d55871SPavan Kumar Linga } 1333d4d55871SPavan Kumar Linga } 1334d4d55871SPavan Kumar Linga } 1335d4d55871SPavan Kumar Linga } 1336d4d55871SPavan Kumar Linga 1337d4d55871SPavan Kumar Linga /** 13381c325aacSAlan Brady * idpf_vport_open - Bring up a vport 13391c325aacSAlan Brady * @vport: vport to bring up 13401c325aacSAlan Brady * @alloc_res: allocate queue resources 13411c325aacSAlan Brady */ 13421c325aacSAlan Brady static int idpf_vport_open(struct idpf_vport *vport, bool alloc_res) 13431c325aacSAlan Brady { 13441c325aacSAlan Brady struct idpf_netdev_priv *np = netdev_priv(vport->netdev); 13451c325aacSAlan Brady struct idpf_adapter *adapter = vport->adapter; 134695af467dSAlan Brady struct idpf_vport_config *vport_config; 13471c325aacSAlan Brady int err; 13481c325aacSAlan Brady 13491c325aacSAlan Brady if (np->state != __IDPF_VPORT_DOWN) 13501c325aacSAlan Brady return -EBUSY; 13511c325aacSAlan Brady 13521c325aacSAlan Brady /* we do not allow interface up just yet */ 13531c325aacSAlan Brady netif_carrier_off(vport->netdev); 13541c325aacSAlan Brady 13551c325aacSAlan Brady if (alloc_res) { 13561c325aacSAlan Brady err = idpf_vport_queues_alloc(vport); 13571c325aacSAlan Brady if (err) 13581c325aacSAlan Brady return err; 13591c325aacSAlan Brady } 13601c325aacSAlan Brady 13611c325aacSAlan Brady err = idpf_vport_intr_alloc(vport); 13621c325aacSAlan Brady if (err) { 13631c325aacSAlan Brady dev_err(&adapter->pdev->dev, "Failed to allocate interrupts for vport %u: %d\n", 13641c325aacSAlan Brady vport->vport_id, err); 13651c325aacSAlan Brady goto queues_rel; 13661c325aacSAlan Brady } 13671c325aacSAlan Brady 13681c325aacSAlan Brady err = idpf_vport_queue_ids_init(vport); 13691c325aacSAlan Brady if (err) { 13701c325aacSAlan Brady dev_err(&adapter->pdev->dev, "Failed to initialize queue ids for vport %u: %d\n", 13711c325aacSAlan Brady vport->vport_id, err); 13721c325aacSAlan Brady goto intr_rel; 13731c325aacSAlan Brady } 13741c325aacSAlan Brady 1375d4d55871SPavan Kumar Linga err = idpf_vport_intr_init(vport); 1376d4d55871SPavan Kumar Linga if (err) { 1377d4d55871SPavan Kumar Linga dev_err(&adapter->pdev->dev, "Failed to initialize interrupts for vport %u: %d\n", 1378d4d55871SPavan Kumar Linga vport->vport_id, err); 1379d4d55871SPavan Kumar Linga goto intr_rel; 1380d4d55871SPavan Kumar Linga } 1381d4d55871SPavan Kumar Linga 138295af467dSAlan Brady err = idpf_rx_bufs_init_all(vport); 138395af467dSAlan Brady if (err) { 138495af467dSAlan Brady dev_err(&adapter->pdev->dev, "Failed to initialize RX buffers for vport %u: %d\n", 138595af467dSAlan Brady vport->vport_id, err); 138695af467dSAlan Brady goto intr_rel; 138795af467dSAlan Brady } 138895af467dSAlan Brady 13891c325aacSAlan Brady err = idpf_queue_reg_init(vport); 13901c325aacSAlan Brady if (err) { 13911c325aacSAlan Brady dev_err(&adapter->pdev->dev, "Failed to initialize queue registers for vport %u: %d\n", 13921c325aacSAlan Brady vport->vport_id, err); 13931c325aacSAlan Brady goto intr_rel; 13941c325aacSAlan Brady } 13951c325aacSAlan Brady 1396d4d55871SPavan Kumar Linga idpf_rx_init_buf_tail(vport); 1397d514c8b5SAlexander Lobakin idpf_vport_intr_ena(vport); 1398d4d55871SPavan Kumar Linga 139995af467dSAlan Brady err = idpf_send_config_queues_msg(vport); 14001c325aacSAlan Brady if (err) { 14011c325aacSAlan Brady dev_err(&adapter->pdev->dev, "Failed to configure queues for vport %u, %d\n", 14021c325aacSAlan Brady vport->vport_id, err); 1403d4d55871SPavan Kumar Linga goto intr_deinit; 1404d4d55871SPavan Kumar Linga } 1405d4d55871SPavan Kumar Linga 1406d4d55871SPavan Kumar Linga err = idpf_send_map_unmap_queue_vector_msg(vport, true); 1407d4d55871SPavan Kumar Linga if (err) { 1408d4d55871SPavan Kumar Linga dev_err(&adapter->pdev->dev, "Failed to map queue vectors for vport %u: %d\n", 1409d4d55871SPavan Kumar Linga vport->vport_id, err); 1410d4d55871SPavan Kumar Linga goto intr_deinit; 1411d4d55871SPavan Kumar Linga } 1412d4d55871SPavan Kumar Linga 1413d4d55871SPavan Kumar Linga err = idpf_send_enable_queues_msg(vport); 1414d4d55871SPavan Kumar Linga if (err) { 1415d4d55871SPavan Kumar Linga dev_err(&adapter->pdev->dev, "Failed to enable queues for vport %u: %d\n", 1416d4d55871SPavan Kumar Linga vport->vport_id, err); 1417d4d55871SPavan Kumar Linga goto unmap_queue_vectors; 1418d4d55871SPavan Kumar Linga } 1419d4d55871SPavan Kumar Linga 1420d4d55871SPavan Kumar Linga err = idpf_send_enable_vport_msg(vport); 1421d4d55871SPavan Kumar Linga if (err) { 1422d4d55871SPavan Kumar Linga dev_err(&adapter->pdev->dev, "Failed to enable vport %u: %d\n", 1423d4d55871SPavan Kumar Linga vport->vport_id, err); 1424d4d55871SPavan Kumar Linga err = -EAGAIN; 1425d4d55871SPavan Kumar Linga goto disable_queues; 14261c325aacSAlan Brady } 14271c325aacSAlan Brady 1428a251eee6SJoshua Hay idpf_restore_features(vport); 1429a251eee6SJoshua Hay 143095af467dSAlan Brady vport_config = adapter->vport_config[vport->idx]; 143195af467dSAlan Brady if (vport_config->user_config.rss_data.rss_lut) 143295af467dSAlan Brady err = idpf_config_rss(vport); 143395af467dSAlan Brady else 143495af467dSAlan Brady err = idpf_init_rss(vport); 143595af467dSAlan Brady if (err) { 143695af467dSAlan Brady dev_err(&adapter->pdev->dev, "Failed to initialize RSS for vport %u: %d\n", 143795af467dSAlan Brady vport->vport_id, err); 1438d4d55871SPavan Kumar Linga goto disable_vport; 1439d4d55871SPavan Kumar Linga } 1440d4d55871SPavan Kumar Linga 1441d4d55871SPavan Kumar Linga err = idpf_up_complete(vport); 1442d4d55871SPavan Kumar Linga if (err) { 1443d4d55871SPavan Kumar Linga dev_err(&adapter->pdev->dev, "Failed to complete interface up for vport %u: %d\n", 1444d4d55871SPavan Kumar Linga vport->vport_id, err); 1445d4d55871SPavan Kumar Linga goto deinit_rss; 144695af467dSAlan Brady } 144795af467dSAlan Brady 14481c325aacSAlan Brady return 0; 14491c325aacSAlan Brady 1450d4d55871SPavan Kumar Linga deinit_rss: 1451d4d55871SPavan Kumar Linga idpf_deinit_rss(vport); 1452d4d55871SPavan Kumar Linga disable_vport: 1453d4d55871SPavan Kumar Linga idpf_send_disable_vport_msg(vport); 1454d4d55871SPavan Kumar Linga disable_queues: 1455d4d55871SPavan Kumar Linga idpf_send_disable_queues_msg(vport); 1456d4d55871SPavan Kumar Linga unmap_queue_vectors: 1457d4d55871SPavan Kumar Linga idpf_send_map_unmap_queue_vector_msg(vport, false); 1458d4d55871SPavan Kumar Linga intr_deinit: 1459d4d55871SPavan Kumar Linga idpf_vport_intr_deinit(vport); 14601c325aacSAlan Brady intr_rel: 14611c325aacSAlan Brady idpf_vport_intr_rel(vport); 14621c325aacSAlan Brady queues_rel: 14631c325aacSAlan Brady idpf_vport_queues_rel(vport); 14641c325aacSAlan Brady 14651c325aacSAlan Brady return err; 14661c325aacSAlan Brady } 14671c325aacSAlan Brady 14681c325aacSAlan Brady /** 14690fe45467SPavan Kumar Linga * idpf_init_task - Delayed initialization task 14700fe45467SPavan Kumar Linga * @work: work_struct handle to our data 14710fe45467SPavan Kumar Linga * 14720fe45467SPavan Kumar Linga * Init task finishes up pending work started in probe. Due to the asynchronous 14730fe45467SPavan Kumar Linga * nature in which the device communicates with hardware, we may have to wait 14740fe45467SPavan Kumar Linga * several milliseconds to get a response. Instead of busy polling in probe, 14750fe45467SPavan Kumar Linga * pulling it out into a delayed work task prevents us from bogging down the 14760fe45467SPavan Kumar Linga * whole system waiting for a response from hardware. 14770fe45467SPavan Kumar Linga */ 14780fe45467SPavan Kumar Linga void idpf_init_task(struct work_struct *work) 14790fe45467SPavan Kumar Linga { 1480ce1b75d0SPavan Kumar Linga struct idpf_vport_config *vport_config; 14810fe45467SPavan Kumar Linga struct idpf_vport_max_q max_q; 14820fe45467SPavan Kumar Linga struct idpf_adapter *adapter; 14831c325aacSAlan Brady struct idpf_netdev_priv *np; 14840fe45467SPavan Kumar Linga struct idpf_vport *vport; 14850fe45467SPavan Kumar Linga u16 num_default_vports; 14860fe45467SPavan Kumar Linga struct pci_dev *pdev; 14870fe45467SPavan Kumar Linga bool default_vport; 14880fe45467SPavan Kumar Linga int index, err; 14890fe45467SPavan Kumar Linga 14900fe45467SPavan Kumar Linga adapter = container_of(work, struct idpf_adapter, init_task.work); 14910fe45467SPavan Kumar Linga 14920fe45467SPavan Kumar Linga num_default_vports = idpf_get_default_vports(adapter); 14930fe45467SPavan Kumar Linga if (adapter->num_alloc_vports < num_default_vports) 14940fe45467SPavan Kumar Linga default_vport = true; 14950fe45467SPavan Kumar Linga else 14960fe45467SPavan Kumar Linga default_vport = false; 14970fe45467SPavan Kumar Linga 14980fe45467SPavan Kumar Linga err = idpf_vport_alloc_max_qs(adapter, &max_q); 14990fe45467SPavan Kumar Linga if (err) 15000fe45467SPavan Kumar Linga goto unwind_vports; 15010fe45467SPavan Kumar Linga 15020fe45467SPavan Kumar Linga err = idpf_send_create_vport_msg(adapter, &max_q); 15030fe45467SPavan Kumar Linga if (err) { 15040fe45467SPavan Kumar Linga idpf_vport_dealloc_max_qs(adapter, &max_q); 15050fe45467SPavan Kumar Linga goto unwind_vports; 15060fe45467SPavan Kumar Linga } 15070fe45467SPavan Kumar Linga 15080fe45467SPavan Kumar Linga pdev = adapter->pdev; 15090fe45467SPavan Kumar Linga vport = idpf_vport_alloc(adapter, &max_q); 15100fe45467SPavan Kumar Linga if (!vport) { 15110fe45467SPavan Kumar Linga err = -EFAULT; 15120fe45467SPavan Kumar Linga dev_err(&pdev->dev, "failed to allocate vport: %d\n", 15130fe45467SPavan Kumar Linga err); 15140fe45467SPavan Kumar Linga idpf_vport_dealloc_max_qs(adapter, &max_q); 15150fe45467SPavan Kumar Linga goto unwind_vports; 15160fe45467SPavan Kumar Linga } 15170fe45467SPavan Kumar Linga 15180fe45467SPavan Kumar Linga index = vport->idx; 1519ce1b75d0SPavan Kumar Linga vport_config = adapter->vport_config[index]; 15200fe45467SPavan Kumar Linga 1521c2d548caSJoshua Hay init_waitqueue_head(&vport->sw_marker_wq); 15220fe45467SPavan Kumar Linga 1523ce1b75d0SPavan Kumar Linga spin_lock_init(&vport_config->mac_filter_list_lock); 1524ce1b75d0SPavan Kumar Linga 1525ce1b75d0SPavan Kumar Linga INIT_LIST_HEAD(&vport_config->user_config.mac_filter_list); 1526ce1b75d0SPavan Kumar Linga 1527ce1b75d0SPavan Kumar Linga err = idpf_check_supported_desc_ids(vport); 1528ce1b75d0SPavan Kumar Linga if (err) { 1529ce1b75d0SPavan Kumar Linga dev_err(&pdev->dev, "failed to get required descriptor ids\n"); 1530ce1b75d0SPavan Kumar Linga goto cfg_netdev_err; 1531ce1b75d0SPavan Kumar Linga } 15320fe45467SPavan Kumar Linga 15330fe45467SPavan Kumar Linga if (idpf_cfg_netdev(vport)) 15340fe45467SPavan Kumar Linga goto cfg_netdev_err; 15350fe45467SPavan Kumar Linga 1536ce1b75d0SPavan Kumar Linga err = idpf_send_get_rx_ptype_msg(vport); 1537ce1b75d0SPavan Kumar Linga if (err) 1538ce1b75d0SPavan Kumar Linga goto handle_err; 1539ce1b75d0SPavan Kumar Linga 15401c325aacSAlan Brady /* Once state is put into DOWN, driver is ready for dev_open */ 15411c325aacSAlan Brady np = netdev_priv(vport->netdev); 15421c325aacSAlan Brady np->state = __IDPF_VPORT_DOWN; 15431c325aacSAlan Brady if (test_and_clear_bit(IDPF_VPORT_UP_REQUESTED, vport_config->flags)) 15441c325aacSAlan Brady idpf_vport_open(vport, true); 15451c325aacSAlan Brady 15460fe45467SPavan Kumar Linga /* Spawn and return 'idpf_init_task' work queue until all the 15470fe45467SPavan Kumar Linga * default vports are created 15480fe45467SPavan Kumar Linga */ 15490fe45467SPavan Kumar Linga if (adapter->num_alloc_vports < num_default_vports) { 15500fe45467SPavan Kumar Linga queue_delayed_work(adapter->init_wq, &adapter->init_task, 15510fe45467SPavan Kumar Linga msecs_to_jiffies(5 * (adapter->pdev->devfn & 0x07))); 15520fe45467SPavan Kumar Linga 15530fe45467SPavan Kumar Linga return; 15540fe45467SPavan Kumar Linga } 15550fe45467SPavan Kumar Linga 15560fe45467SPavan Kumar Linga for (index = 0; index < adapter->max_vports; index++) { 15570fe45467SPavan Kumar Linga if (adapter->netdevs[index] && 15580fe45467SPavan Kumar Linga !test_bit(IDPF_VPORT_REG_NETDEV, 15590fe45467SPavan Kumar Linga adapter->vport_config[index]->flags)) { 15600fe45467SPavan Kumar Linga register_netdev(adapter->netdevs[index]); 15610fe45467SPavan Kumar Linga set_bit(IDPF_VPORT_REG_NETDEV, 15620fe45467SPavan Kumar Linga adapter->vport_config[index]->flags); 15630fe45467SPavan Kumar Linga } 15640fe45467SPavan Kumar Linga } 15650fe45467SPavan Kumar Linga 15660fe45467SPavan Kumar Linga /* As all the required vports are created, clear the reset flag 15670fe45467SPavan Kumar Linga * unconditionally here in case we were in reset and the link was down. 15680fe45467SPavan Kumar Linga */ 15690fe45467SPavan Kumar Linga clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags); 1570a251eee6SJoshua Hay /* Start the statistics task now */ 1571a251eee6SJoshua Hay queue_delayed_work(adapter->stats_wq, &adapter->stats_task, 1572a251eee6SJoshua Hay msecs_to_jiffies(10 * (pdev->devfn & 0x07))); 15730fe45467SPavan Kumar Linga 15740fe45467SPavan Kumar Linga return; 15750fe45467SPavan Kumar Linga 1576ce1b75d0SPavan Kumar Linga handle_err: 1577ce1b75d0SPavan Kumar Linga idpf_decfg_netdev(vport); 15780fe45467SPavan Kumar Linga cfg_netdev_err: 15790fe45467SPavan Kumar Linga idpf_vport_rel(vport); 15800fe45467SPavan Kumar Linga adapter->vports[index] = NULL; 15810fe45467SPavan Kumar Linga unwind_vports: 15820fe45467SPavan Kumar Linga if (default_vport) { 15830fe45467SPavan Kumar Linga for (index = 0; index < adapter->max_vports; index++) { 15840fe45467SPavan Kumar Linga if (adapter->vports[index]) 15850fe45467SPavan Kumar Linga idpf_vport_dealloc(adapter->vports[index]); 15860fe45467SPavan Kumar Linga } 15870fe45467SPavan Kumar Linga } 15880fe45467SPavan Kumar Linga clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags); 15890fe45467SPavan Kumar Linga } 15900fe45467SPavan Kumar Linga 15910fe45467SPavan Kumar Linga /** 1592a251eee6SJoshua Hay * idpf_sriov_ena - Enable or change number of VFs 1593a251eee6SJoshua Hay * @adapter: private data struct 1594a251eee6SJoshua Hay * @num_vfs: number of VFs to allocate 1595a251eee6SJoshua Hay */ 1596a251eee6SJoshua Hay static int idpf_sriov_ena(struct idpf_adapter *adapter, int num_vfs) 1597a251eee6SJoshua Hay { 1598a251eee6SJoshua Hay struct device *dev = &adapter->pdev->dev; 1599a251eee6SJoshua Hay int err; 1600a251eee6SJoshua Hay 1601a251eee6SJoshua Hay err = idpf_send_set_sriov_vfs_msg(adapter, num_vfs); 1602a251eee6SJoshua Hay if (err) { 1603a251eee6SJoshua Hay dev_err(dev, "Failed to allocate VFs: %d\n", err); 1604a251eee6SJoshua Hay 1605a251eee6SJoshua Hay return err; 1606a251eee6SJoshua Hay } 1607a251eee6SJoshua Hay 1608a251eee6SJoshua Hay err = pci_enable_sriov(adapter->pdev, num_vfs); 1609a251eee6SJoshua Hay if (err) { 1610a251eee6SJoshua Hay idpf_send_set_sriov_vfs_msg(adapter, 0); 1611a251eee6SJoshua Hay dev_err(dev, "Failed to enable SR-IOV: %d\n", err); 1612a251eee6SJoshua Hay 1613a251eee6SJoshua Hay return err; 1614a251eee6SJoshua Hay } 1615a251eee6SJoshua Hay 1616a251eee6SJoshua Hay adapter->num_vfs = num_vfs; 1617a251eee6SJoshua Hay 1618a251eee6SJoshua Hay return num_vfs; 1619a251eee6SJoshua Hay } 1620a251eee6SJoshua Hay 1621a251eee6SJoshua Hay /** 1622a251eee6SJoshua Hay * idpf_sriov_configure - Configure the requested VFs 1623a251eee6SJoshua Hay * @pdev: pointer to a pci_dev structure 1624a251eee6SJoshua Hay * @num_vfs: number of vfs to allocate 1625a251eee6SJoshua Hay * 1626a251eee6SJoshua Hay * Enable or change the number of VFs. Called when the user updates the number 1627a251eee6SJoshua Hay * of VFs in sysfs. 1628a251eee6SJoshua Hay **/ 1629a251eee6SJoshua Hay int idpf_sriov_configure(struct pci_dev *pdev, int num_vfs) 1630a251eee6SJoshua Hay { 1631a251eee6SJoshua Hay struct idpf_adapter *adapter = pci_get_drvdata(pdev); 1632a251eee6SJoshua Hay 1633a251eee6SJoshua Hay if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_SRIOV)) { 1634a251eee6SJoshua Hay dev_info(&pdev->dev, "SR-IOV is not supported on this device\n"); 1635a251eee6SJoshua Hay 1636a251eee6SJoshua Hay return -EOPNOTSUPP; 1637a251eee6SJoshua Hay } 1638a251eee6SJoshua Hay 1639a251eee6SJoshua Hay if (num_vfs) 1640a251eee6SJoshua Hay return idpf_sriov_ena(adapter, num_vfs); 1641a251eee6SJoshua Hay 1642a251eee6SJoshua Hay if (pci_vfs_assigned(pdev)) { 1643a251eee6SJoshua Hay dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs\n"); 1644a251eee6SJoshua Hay 1645a251eee6SJoshua Hay return -EBUSY; 1646a251eee6SJoshua Hay } 1647a251eee6SJoshua Hay 1648a251eee6SJoshua Hay pci_disable_sriov(adapter->pdev); 1649a251eee6SJoshua Hay idpf_send_set_sriov_vfs_msg(adapter, 0); 1650a251eee6SJoshua Hay adapter->num_vfs = 0; 1651a251eee6SJoshua Hay 1652a251eee6SJoshua Hay return 0; 1653a251eee6SJoshua Hay } 1654a251eee6SJoshua Hay 1655a251eee6SJoshua Hay /** 16560fe45467SPavan Kumar Linga * idpf_deinit_task - Device deinit routine 16570fe45467SPavan Kumar Linga * @adapter: Driver specific private structure 16580fe45467SPavan Kumar Linga * 16590fe45467SPavan Kumar Linga * Extended remove logic which will be used for 16600fe45467SPavan Kumar Linga * hard reset as well 16610fe45467SPavan Kumar Linga */ 16620fe45467SPavan Kumar Linga void idpf_deinit_task(struct idpf_adapter *adapter) 16630fe45467SPavan Kumar Linga { 16640fe45467SPavan Kumar Linga unsigned int i; 16650fe45467SPavan Kumar Linga 16660fe45467SPavan Kumar Linga /* Wait until the init_task is done else this thread might release 16670fe45467SPavan Kumar Linga * the resources first and the other thread might end up in a bad state 16680fe45467SPavan Kumar Linga */ 16690fe45467SPavan Kumar Linga cancel_delayed_work_sync(&adapter->init_task); 16700fe45467SPavan Kumar Linga 16710fe45467SPavan Kumar Linga if (!adapter->vports) 16720fe45467SPavan Kumar Linga return; 16730fe45467SPavan Kumar Linga 1674a251eee6SJoshua Hay cancel_delayed_work_sync(&adapter->stats_task); 1675a251eee6SJoshua Hay 16760fe45467SPavan Kumar Linga for (i = 0; i < adapter->max_vports; i++) { 16770fe45467SPavan Kumar Linga if (adapter->vports[i]) 16780fe45467SPavan Kumar Linga idpf_vport_dealloc(adapter->vports[i]); 16790fe45467SPavan Kumar Linga } 16800fe45467SPavan Kumar Linga } 16810fe45467SPavan Kumar Linga 16820fe45467SPavan Kumar Linga /** 16838077c727SJoshua Hay * idpf_check_reset_complete - check that reset is complete 16848077c727SJoshua Hay * @hw: pointer to hw struct 16858077c727SJoshua Hay * @reset_reg: struct with reset registers 16868077c727SJoshua Hay * 16878077c727SJoshua Hay * Returns 0 if device is ready to use, or -EBUSY if it's in reset. 16888077c727SJoshua Hay **/ 16898077c727SJoshua Hay static int idpf_check_reset_complete(struct idpf_hw *hw, 16908077c727SJoshua Hay struct idpf_reset_reg *reset_reg) 16918077c727SJoshua Hay { 16928077c727SJoshua Hay struct idpf_adapter *adapter = hw->back; 16938077c727SJoshua Hay int i; 16948077c727SJoshua Hay 16958077c727SJoshua Hay for (i = 0; i < 2000; i++) { 16968077c727SJoshua Hay u32 reg_val = readl(reset_reg->rstat); 16978077c727SJoshua Hay 16988077c727SJoshua Hay /* 0xFFFFFFFF might be read if other side hasn't cleared the 16998077c727SJoshua Hay * register for us yet and 0xFFFFFFFF is not a valid value for 17008077c727SJoshua Hay * the register, so treat that as invalid. 17018077c727SJoshua Hay */ 17028077c727SJoshua Hay if (reg_val != 0xFFFFFFFF && (reg_val & reset_reg->rstat_m)) 17038077c727SJoshua Hay return 0; 17048077c727SJoshua Hay 17058077c727SJoshua Hay usleep_range(5000, 10000); 17068077c727SJoshua Hay } 17078077c727SJoshua Hay 17088077c727SJoshua Hay dev_warn(&adapter->pdev->dev, "Device reset timeout!\n"); 17098077c727SJoshua Hay /* Clear the reset flag unconditionally here since the reset 17108077c727SJoshua Hay * technically isn't in progress anymore from the driver's perspective 17118077c727SJoshua Hay */ 17128077c727SJoshua Hay clear_bit(IDPF_HR_RESET_IN_PROG, adapter->flags); 17138077c727SJoshua Hay 17148077c727SJoshua Hay return -EBUSY; 17158077c727SJoshua Hay } 17168077c727SJoshua Hay 17178077c727SJoshua Hay /** 17180fe45467SPavan Kumar Linga * idpf_set_vport_state - Set the vport state to be after the reset 17190fe45467SPavan Kumar Linga * @adapter: Driver specific private structure 17200fe45467SPavan Kumar Linga */ 17210fe45467SPavan Kumar Linga static void idpf_set_vport_state(struct idpf_adapter *adapter) 17220fe45467SPavan Kumar Linga { 17230fe45467SPavan Kumar Linga u16 i; 17240fe45467SPavan Kumar Linga 17250fe45467SPavan Kumar Linga for (i = 0; i < adapter->max_vports; i++) { 17260fe45467SPavan Kumar Linga struct idpf_netdev_priv *np; 17270fe45467SPavan Kumar Linga 17280fe45467SPavan Kumar Linga if (!adapter->netdevs[i]) 17290fe45467SPavan Kumar Linga continue; 17300fe45467SPavan Kumar Linga 17310fe45467SPavan Kumar Linga np = netdev_priv(adapter->netdevs[i]); 17320fe45467SPavan Kumar Linga if (np->state == __IDPF_VPORT_UP) 17330fe45467SPavan Kumar Linga set_bit(IDPF_VPORT_UP_REQUESTED, 17340fe45467SPavan Kumar Linga adapter->vport_config[i]->flags); 17350fe45467SPavan Kumar Linga } 17360fe45467SPavan Kumar Linga } 17370fe45467SPavan Kumar Linga 17380fe45467SPavan Kumar Linga /** 17398077c727SJoshua Hay * idpf_init_hard_reset - Initiate a hardware reset 17408077c727SJoshua Hay * @adapter: Driver specific private structure 17418077c727SJoshua Hay * 17428077c727SJoshua Hay * Deallocate the vports and all the resources associated with them and 17438077c727SJoshua Hay * reallocate. Also reinitialize the mailbox. Return 0 on success, 17448077c727SJoshua Hay * negative on failure. 17458077c727SJoshua Hay */ 17468077c727SJoshua Hay static int idpf_init_hard_reset(struct idpf_adapter *adapter) 17478077c727SJoshua Hay { 17488077c727SJoshua Hay struct idpf_reg_ops *reg_ops = &adapter->dev_ops.reg_ops; 17498077c727SJoshua Hay struct device *dev = &adapter->pdev->dev; 17500fe45467SPavan Kumar Linga struct net_device *netdev; 17518077c727SJoshua Hay int err; 17520fe45467SPavan Kumar Linga u16 i; 17538077c727SJoshua Hay 17548077c727SJoshua Hay mutex_lock(&adapter->vport_ctrl_lock); 17558077c727SJoshua Hay 17568077c727SJoshua Hay dev_info(dev, "Device HW Reset initiated\n"); 17570fe45467SPavan Kumar Linga 17580fe45467SPavan Kumar Linga /* Avoid TX hangs on reset */ 17590fe45467SPavan Kumar Linga for (i = 0; i < adapter->max_vports; i++) { 17600fe45467SPavan Kumar Linga netdev = adapter->netdevs[i]; 17610fe45467SPavan Kumar Linga if (!netdev) 17620fe45467SPavan Kumar Linga continue; 17630fe45467SPavan Kumar Linga 17640fe45467SPavan Kumar Linga netif_carrier_off(netdev); 17650fe45467SPavan Kumar Linga netif_tx_disable(netdev); 17660fe45467SPavan Kumar Linga } 17670fe45467SPavan Kumar Linga 17688077c727SJoshua Hay /* Prepare for reset */ 17698077c727SJoshua Hay if (test_and_clear_bit(IDPF_HR_DRV_LOAD, adapter->flags)) { 17708077c727SJoshua Hay reg_ops->trigger_reset(adapter, IDPF_HR_DRV_LOAD); 17718077c727SJoshua Hay } else if (test_and_clear_bit(IDPF_HR_FUNC_RESET, adapter->flags)) { 17728077c727SJoshua Hay bool is_reset = idpf_is_reset_detected(adapter); 17738077c727SJoshua Hay 17740fe45467SPavan Kumar Linga idpf_set_vport_state(adapter); 17754930fbf4SPavan Kumar Linga idpf_vc_core_deinit(adapter); 17768077c727SJoshua Hay if (!is_reset) 17778077c727SJoshua Hay reg_ops->trigger_reset(adapter, IDPF_HR_FUNC_RESET); 17788077c727SJoshua Hay idpf_deinit_dflt_mbx(adapter); 17798077c727SJoshua Hay } else { 17808077c727SJoshua Hay dev_err(dev, "Unhandled hard reset cause\n"); 17818077c727SJoshua Hay err = -EBADRQC; 17828077c727SJoshua Hay goto unlock_mutex; 17838077c727SJoshua Hay } 17848077c727SJoshua Hay 17858077c727SJoshua Hay /* Wait for reset to complete */ 17868077c727SJoshua Hay err = idpf_check_reset_complete(&adapter->hw, &adapter->reset_reg); 17878077c727SJoshua Hay if (err) { 17888077c727SJoshua Hay dev_err(dev, "The driver was unable to contact the device's firmware. Check that the FW is running. Driver state= 0x%x\n", 17898077c727SJoshua Hay adapter->state); 17908077c727SJoshua Hay goto unlock_mutex; 17918077c727SJoshua Hay } 17928077c727SJoshua Hay 17938077c727SJoshua Hay /* Reset is complete and so start building the driver resources again */ 17948077c727SJoshua Hay err = idpf_init_dflt_mbx(adapter); 17954930fbf4SPavan Kumar Linga if (err) { 17968077c727SJoshua Hay dev_err(dev, "Failed to initialize default mailbox: %d\n", err); 17974930fbf4SPavan Kumar Linga goto unlock_mutex; 17984930fbf4SPavan Kumar Linga } 17994930fbf4SPavan Kumar Linga 180034c21fa8SAlan Brady queue_delayed_work(adapter->mbx_wq, &adapter->mbx_task, 0); 180134c21fa8SAlan Brady 18024930fbf4SPavan Kumar Linga /* Initialize the state machine, also allocate memory and request 18034930fbf4SPavan Kumar Linga * resources 18044930fbf4SPavan Kumar Linga */ 18054930fbf4SPavan Kumar Linga err = idpf_vc_core_init(adapter); 18064930fbf4SPavan Kumar Linga if (err) { 18074930fbf4SPavan Kumar Linga idpf_deinit_dflt_mbx(adapter); 18084930fbf4SPavan Kumar Linga goto unlock_mutex; 18094930fbf4SPavan Kumar Linga } 18108077c727SJoshua Hay 18110fe45467SPavan Kumar Linga /* Wait till all the vports are initialized to release the reset lock, 18120fe45467SPavan Kumar Linga * else user space callbacks may access uninitialized vports 18130fe45467SPavan Kumar Linga */ 18140fe45467SPavan Kumar Linga while (test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags)) 18150fe45467SPavan Kumar Linga msleep(100); 18160fe45467SPavan Kumar Linga 18178077c727SJoshua Hay unlock_mutex: 18188077c727SJoshua Hay mutex_unlock(&adapter->vport_ctrl_lock); 18198077c727SJoshua Hay 18208077c727SJoshua Hay return err; 18218077c727SJoshua Hay } 18228077c727SJoshua Hay 18238077c727SJoshua Hay /** 18248077c727SJoshua Hay * idpf_vc_event_task - Handle virtchannel event logic 18258077c727SJoshua Hay * @work: work queue struct 18268077c727SJoshua Hay */ 18278077c727SJoshua Hay void idpf_vc_event_task(struct work_struct *work) 18288077c727SJoshua Hay { 18298077c727SJoshua Hay struct idpf_adapter *adapter; 18308077c727SJoshua Hay 18318077c727SJoshua Hay adapter = container_of(work, struct idpf_adapter, vc_event_task.work); 18328077c727SJoshua Hay 18338077c727SJoshua Hay if (test_bit(IDPF_REMOVE_IN_PROG, adapter->flags)) 18348077c727SJoshua Hay return; 18358077c727SJoshua Hay 18368077c727SJoshua Hay if (test_bit(IDPF_HR_FUNC_RESET, adapter->flags) || 18378077c727SJoshua Hay test_bit(IDPF_HR_DRV_LOAD, adapter->flags)) { 18388077c727SJoshua Hay set_bit(IDPF_HR_RESET_IN_PROG, adapter->flags); 18398077c727SJoshua Hay idpf_init_hard_reset(adapter); 18408077c727SJoshua Hay } 18418077c727SJoshua Hay } 18428077c727SJoshua Hay 18438077c727SJoshua Hay /** 184402cbfba1SAlan Brady * idpf_initiate_soft_reset - Initiate a software reset 184502cbfba1SAlan Brady * @vport: virtual port data struct 184602cbfba1SAlan Brady * @reset_cause: reason for the soft reset 184702cbfba1SAlan Brady * 184802cbfba1SAlan Brady * Soft reset only reallocs vport queue resources. Returns 0 on success, 184902cbfba1SAlan Brady * negative on failure. 185002cbfba1SAlan Brady */ 185102cbfba1SAlan Brady int idpf_initiate_soft_reset(struct idpf_vport *vport, 185202cbfba1SAlan Brady enum idpf_vport_reset_cause reset_cause) 185302cbfba1SAlan Brady { 185402cbfba1SAlan Brady struct idpf_netdev_priv *np = netdev_priv(vport->netdev); 185502cbfba1SAlan Brady enum idpf_vport_state current_state = np->state; 185602cbfba1SAlan Brady struct idpf_adapter *adapter = vport->adapter; 185702cbfba1SAlan Brady struct idpf_vport *new_vport; 1858*e4891e46SAlexander Lobakin int err; 185902cbfba1SAlan Brady 186002cbfba1SAlan Brady /* If the system is low on memory, we can end up in bad state if we 186102cbfba1SAlan Brady * free all the memory for queue resources and try to allocate them 186202cbfba1SAlan Brady * again. Instead, we can pre-allocate the new resources before doing 186302cbfba1SAlan Brady * anything and bailing if the alloc fails. 186402cbfba1SAlan Brady * 186502cbfba1SAlan Brady * Make a clone of the existing vport to mimic its current 186602cbfba1SAlan Brady * configuration, then modify the new structure with any requested 186702cbfba1SAlan Brady * changes. Once the allocation of the new resources is done, stop the 186802cbfba1SAlan Brady * existing vport and copy the configuration to the main vport. If an 186902cbfba1SAlan Brady * error occurred, the existing vport will be untouched. 187002cbfba1SAlan Brady * 187102cbfba1SAlan Brady */ 187202cbfba1SAlan Brady new_vport = kzalloc(sizeof(*vport), GFP_KERNEL); 187302cbfba1SAlan Brady if (!new_vport) 187402cbfba1SAlan Brady return -ENOMEM; 187502cbfba1SAlan Brady 187602cbfba1SAlan Brady /* This purposely avoids copying the end of the struct because it 187702cbfba1SAlan Brady * contains wait_queues and mutexes and other stuff we don't want to 187802cbfba1SAlan Brady * mess with. Nothing below should use those variables from new_vport 187902cbfba1SAlan Brady * and should instead always refer to them in vport if they need to. 188002cbfba1SAlan Brady */ 1881bcbedf25SAlan Brady memcpy(new_vport, vport, offsetof(struct idpf_vport, link_speed_mbps)); 188202cbfba1SAlan Brady 188302cbfba1SAlan Brady /* Adjust resource parameters prior to reallocating resources */ 188402cbfba1SAlan Brady switch (reset_cause) { 188502cbfba1SAlan Brady case IDPF_SR_Q_CHANGE: 188602cbfba1SAlan Brady err = idpf_vport_adjust_qs(new_vport); 188702cbfba1SAlan Brady if (err) 188802cbfba1SAlan Brady goto free_vport; 188902cbfba1SAlan Brady break; 189002cbfba1SAlan Brady case IDPF_SR_Q_DESC_CHANGE: 189102cbfba1SAlan Brady /* Update queue parameters before allocating resources */ 189202cbfba1SAlan Brady idpf_vport_calc_num_q_desc(new_vport); 189302cbfba1SAlan Brady break; 1894a251eee6SJoshua Hay case IDPF_SR_MTU_CHANGE: 1895a251eee6SJoshua Hay case IDPF_SR_RSC_CHANGE: 1896a251eee6SJoshua Hay break; 189702cbfba1SAlan Brady default: 189802cbfba1SAlan Brady dev_err(&adapter->pdev->dev, "Unhandled soft reset cause\n"); 189902cbfba1SAlan Brady err = -EINVAL; 190002cbfba1SAlan Brady goto free_vport; 190102cbfba1SAlan Brady } 190202cbfba1SAlan Brady 190302cbfba1SAlan Brady err = idpf_vport_queues_alloc(new_vport); 190402cbfba1SAlan Brady if (err) 190502cbfba1SAlan Brady goto free_vport; 190602cbfba1SAlan Brady if (current_state <= __IDPF_VPORT_DOWN) { 190702cbfba1SAlan Brady idpf_send_delete_queues_msg(vport); 190802cbfba1SAlan Brady } else { 190902cbfba1SAlan Brady set_bit(IDPF_VPORT_DEL_QUEUES, vport->flags); 191002cbfba1SAlan Brady idpf_vport_stop(vport); 191102cbfba1SAlan Brady } 191202cbfba1SAlan Brady 191302cbfba1SAlan Brady idpf_deinit_rss(vport); 191402cbfba1SAlan Brady /* We're passing in vport here because we need its wait_queue 191502cbfba1SAlan Brady * to send a message and it should be getting all the vport 191602cbfba1SAlan Brady * config data out of the adapter but we need to be careful not 191702cbfba1SAlan Brady * to add code to add_queues to change the vport config within 191802cbfba1SAlan Brady * vport itself as it will be wiped with a memcpy later. 191902cbfba1SAlan Brady */ 192002cbfba1SAlan Brady err = idpf_send_add_queues_msg(vport, new_vport->num_txq, 192102cbfba1SAlan Brady new_vport->num_complq, 192202cbfba1SAlan Brady new_vport->num_rxq, 192302cbfba1SAlan Brady new_vport->num_bufq); 192402cbfba1SAlan Brady if (err) 192502cbfba1SAlan Brady goto err_reset; 192602cbfba1SAlan Brady 192702cbfba1SAlan Brady /* Same comment as above regarding avoiding copying the wait_queues and 192802cbfba1SAlan Brady * mutexes applies here. We do not want to mess with those if possible. 192902cbfba1SAlan Brady */ 1930bcbedf25SAlan Brady memcpy(vport, new_vport, offsetof(struct idpf_vport, link_speed_mbps)); 193102cbfba1SAlan Brady 193202cbfba1SAlan Brady if (reset_cause == IDPF_SR_Q_CHANGE) 193302cbfba1SAlan Brady idpf_vport_alloc_vec_indexes(vport); 193402cbfba1SAlan Brady 193502cbfba1SAlan Brady err = idpf_set_real_num_queues(vport); 193602cbfba1SAlan Brady if (err) 193702cbfba1SAlan Brady goto err_reset; 193802cbfba1SAlan Brady 193902cbfba1SAlan Brady if (current_state == __IDPF_VPORT_UP) 194002cbfba1SAlan Brady err = idpf_vport_open(vport, false); 194102cbfba1SAlan Brady 194202cbfba1SAlan Brady kfree(new_vport); 194302cbfba1SAlan Brady 194402cbfba1SAlan Brady return err; 194502cbfba1SAlan Brady 194602cbfba1SAlan Brady err_reset: 194702cbfba1SAlan Brady idpf_vport_queues_rel(new_vport); 194802cbfba1SAlan Brady free_vport: 194902cbfba1SAlan Brady kfree(new_vport); 195002cbfba1SAlan Brady 195102cbfba1SAlan Brady return err; 195202cbfba1SAlan Brady } 195302cbfba1SAlan Brady 195402cbfba1SAlan Brady /** 1955a251eee6SJoshua Hay * idpf_addr_sync - Callback for dev_(mc|uc)_sync to add address 1956a251eee6SJoshua Hay * @netdev: the netdevice 1957a251eee6SJoshua Hay * @addr: address to add 1958a251eee6SJoshua Hay * 1959a251eee6SJoshua Hay * Called by __dev_(mc|uc)_sync when an address needs to be added. We call 1960a251eee6SJoshua Hay * __dev_(uc|mc)_sync from .set_rx_mode. Kernel takes addr_list_lock spinlock 1961a251eee6SJoshua Hay * meaning we cannot sleep in this context. Due to this, we have to add the 1962a251eee6SJoshua Hay * filter and send the virtchnl message asynchronously without waiting for the 1963a251eee6SJoshua Hay * response from the other side. We won't know whether or not the operation 1964a251eee6SJoshua Hay * actually succeeded until we get the message back. Returns 0 on success, 1965a251eee6SJoshua Hay * negative on failure. 1966a251eee6SJoshua Hay */ 1967a251eee6SJoshua Hay static int idpf_addr_sync(struct net_device *netdev, const u8 *addr) 1968a251eee6SJoshua Hay { 1969a251eee6SJoshua Hay struct idpf_netdev_priv *np = netdev_priv(netdev); 1970a251eee6SJoshua Hay 1971a251eee6SJoshua Hay return idpf_add_mac_filter(np->vport, np, addr, true); 1972a251eee6SJoshua Hay } 1973a251eee6SJoshua Hay 1974a251eee6SJoshua Hay /** 1975a251eee6SJoshua Hay * idpf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address 1976a251eee6SJoshua Hay * @netdev: the netdevice 1977a251eee6SJoshua Hay * @addr: address to add 1978a251eee6SJoshua Hay * 1979a251eee6SJoshua Hay * Called by __dev_(mc|uc)_sync when an address needs to be added. We call 1980a251eee6SJoshua Hay * __dev_(uc|mc)_sync from .set_rx_mode. Kernel takes addr_list_lock spinlock 1981a251eee6SJoshua Hay * meaning we cannot sleep in this context. Due to this we have to delete the 1982a251eee6SJoshua Hay * filter and send the virtchnl message asynchronously without waiting for the 1983a251eee6SJoshua Hay * return from the other side. We won't know whether or not the operation 1984a251eee6SJoshua Hay * actually succeeded until we get the message back. Returns 0 on success, 1985a251eee6SJoshua Hay * negative on failure. 1986a251eee6SJoshua Hay */ 1987a251eee6SJoshua Hay static int idpf_addr_unsync(struct net_device *netdev, const u8 *addr) 1988a251eee6SJoshua Hay { 1989a251eee6SJoshua Hay struct idpf_netdev_priv *np = netdev_priv(netdev); 1990a251eee6SJoshua Hay 1991a251eee6SJoshua Hay /* Under some circumstances, we might receive a request to delete 1992a251eee6SJoshua Hay * our own device address from our uc list. Because we store the 1993a251eee6SJoshua Hay * device address in the VSI's MAC filter list, we need to ignore 1994a251eee6SJoshua Hay * such requests and not delete our device address from this list. 1995a251eee6SJoshua Hay */ 1996a251eee6SJoshua Hay if (ether_addr_equal(addr, netdev->dev_addr)) 1997a251eee6SJoshua Hay return 0; 1998a251eee6SJoshua Hay 1999a251eee6SJoshua Hay idpf_del_mac_filter(np->vport, np, addr, true); 2000a251eee6SJoshua Hay 2001a251eee6SJoshua Hay return 0; 2002a251eee6SJoshua Hay } 2003a251eee6SJoshua Hay 2004a251eee6SJoshua Hay /** 2005a251eee6SJoshua Hay * idpf_set_rx_mode - NDO callback to set the netdev filters 2006a251eee6SJoshua Hay * @netdev: network interface device structure 2007a251eee6SJoshua Hay * 2008a251eee6SJoshua Hay * Stack takes addr_list_lock spinlock before calling our .set_rx_mode. We 2009a251eee6SJoshua Hay * cannot sleep in this context. 2010a251eee6SJoshua Hay */ 2011a251eee6SJoshua Hay static void idpf_set_rx_mode(struct net_device *netdev) 2012a251eee6SJoshua Hay { 2013a251eee6SJoshua Hay struct idpf_netdev_priv *np = netdev_priv(netdev); 2014a251eee6SJoshua Hay struct idpf_vport_user_config_data *config_data; 2015a251eee6SJoshua Hay struct idpf_adapter *adapter; 2016a251eee6SJoshua Hay bool changed = false; 2017a251eee6SJoshua Hay struct device *dev; 2018a251eee6SJoshua Hay int err; 2019a251eee6SJoshua Hay 2020a251eee6SJoshua Hay adapter = np->adapter; 2021a251eee6SJoshua Hay dev = &adapter->pdev->dev; 2022a251eee6SJoshua Hay 2023a251eee6SJoshua Hay if (idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_MACFILTER)) { 2024a251eee6SJoshua Hay __dev_uc_sync(netdev, idpf_addr_sync, idpf_addr_unsync); 2025a251eee6SJoshua Hay __dev_mc_sync(netdev, idpf_addr_sync, idpf_addr_unsync); 2026a251eee6SJoshua Hay } 2027a251eee6SJoshua Hay 2028a251eee6SJoshua Hay if (!idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_PROMISC)) 2029a251eee6SJoshua Hay return; 2030a251eee6SJoshua Hay 2031a251eee6SJoshua Hay config_data = &adapter->vport_config[np->vport_idx]->user_config; 2032a251eee6SJoshua Hay /* IFF_PROMISC enables both unicast and multicast promiscuous, 2033a251eee6SJoshua Hay * while IFF_ALLMULTI only enables multicast such that: 2034a251eee6SJoshua Hay * 2035a251eee6SJoshua Hay * promisc + allmulti = unicast | multicast 2036a251eee6SJoshua Hay * promisc + !allmulti = unicast | multicast 2037a251eee6SJoshua Hay * !promisc + allmulti = multicast 2038a251eee6SJoshua Hay */ 2039a251eee6SJoshua Hay if ((netdev->flags & IFF_PROMISC) && 2040a251eee6SJoshua Hay !test_and_set_bit(__IDPF_PROMISC_UC, config_data->user_flags)) { 2041a251eee6SJoshua Hay changed = true; 2042a251eee6SJoshua Hay dev_info(&adapter->pdev->dev, "Entering promiscuous mode\n"); 2043a251eee6SJoshua Hay if (!test_and_set_bit(__IDPF_PROMISC_MC, adapter->flags)) 2044a251eee6SJoshua Hay dev_info(dev, "Entering multicast promiscuous mode\n"); 2045a251eee6SJoshua Hay } 2046a251eee6SJoshua Hay 2047a251eee6SJoshua Hay if (!(netdev->flags & IFF_PROMISC) && 2048a251eee6SJoshua Hay test_and_clear_bit(__IDPF_PROMISC_UC, config_data->user_flags)) { 2049a251eee6SJoshua Hay changed = true; 2050a251eee6SJoshua Hay dev_info(dev, "Leaving promiscuous mode\n"); 2051a251eee6SJoshua Hay } 2052a251eee6SJoshua Hay 2053a251eee6SJoshua Hay if (netdev->flags & IFF_ALLMULTI && 2054a251eee6SJoshua Hay !test_and_set_bit(__IDPF_PROMISC_MC, config_data->user_flags)) { 2055a251eee6SJoshua Hay changed = true; 2056a251eee6SJoshua Hay dev_info(dev, "Entering multicast promiscuous mode\n"); 2057a251eee6SJoshua Hay } 2058a251eee6SJoshua Hay 2059a251eee6SJoshua Hay if (!(netdev->flags & (IFF_ALLMULTI | IFF_PROMISC)) && 2060a251eee6SJoshua Hay test_and_clear_bit(__IDPF_PROMISC_MC, config_data->user_flags)) { 2061a251eee6SJoshua Hay changed = true; 2062a251eee6SJoshua Hay dev_info(dev, "Leaving multicast promiscuous mode\n"); 2063a251eee6SJoshua Hay } 2064a251eee6SJoshua Hay 2065a251eee6SJoshua Hay if (!changed) 2066a251eee6SJoshua Hay return; 2067a251eee6SJoshua Hay 2068a251eee6SJoshua Hay err = idpf_set_promiscuous(adapter, config_data, np->vport_id); 2069a251eee6SJoshua Hay if (err) 2070a251eee6SJoshua Hay dev_err(dev, "Failed to set promiscuous mode: %d\n", err); 2071a251eee6SJoshua Hay } 2072a251eee6SJoshua Hay 2073a251eee6SJoshua Hay /** 2074a251eee6SJoshua Hay * idpf_vport_manage_rss_lut - disable/enable RSS 2075a251eee6SJoshua Hay * @vport: the vport being changed 2076a251eee6SJoshua Hay * 2077a251eee6SJoshua Hay * In the event of disable request for RSS, this function will zero out RSS 2078a251eee6SJoshua Hay * LUT, while in the event of enable request for RSS, it will reconfigure RSS 2079a251eee6SJoshua Hay * LUT with the default LUT configuration. 2080a251eee6SJoshua Hay */ 2081a251eee6SJoshua Hay static int idpf_vport_manage_rss_lut(struct idpf_vport *vport) 2082a251eee6SJoshua Hay { 2083a251eee6SJoshua Hay bool ena = idpf_is_feature_ena(vport, NETIF_F_RXHASH); 2084a251eee6SJoshua Hay struct idpf_rss_data *rss_data; 2085a251eee6SJoshua Hay u16 idx = vport->idx; 2086a251eee6SJoshua Hay int lut_size; 2087a251eee6SJoshua Hay 2088a251eee6SJoshua Hay rss_data = &vport->adapter->vport_config[idx]->user_config.rss_data; 2089a251eee6SJoshua Hay lut_size = rss_data->rss_lut_size * sizeof(u32); 2090a251eee6SJoshua Hay 2091a251eee6SJoshua Hay if (ena) { 2092a251eee6SJoshua Hay /* This will contain the default or user configured LUT */ 2093a251eee6SJoshua Hay memcpy(rss_data->rss_lut, rss_data->cached_lut, lut_size); 2094a251eee6SJoshua Hay } else { 2095a251eee6SJoshua Hay /* Save a copy of the current LUT to be restored later if 2096a251eee6SJoshua Hay * requested. 2097a251eee6SJoshua Hay */ 2098a251eee6SJoshua Hay memcpy(rss_data->cached_lut, rss_data->rss_lut, lut_size); 2099a251eee6SJoshua Hay 2100a251eee6SJoshua Hay /* Zero out the current LUT to disable */ 2101a251eee6SJoshua Hay memset(rss_data->rss_lut, 0, lut_size); 2102a251eee6SJoshua Hay } 2103a251eee6SJoshua Hay 2104a251eee6SJoshua Hay return idpf_config_rss(vport); 2105a251eee6SJoshua Hay } 2106a251eee6SJoshua Hay 2107a251eee6SJoshua Hay /** 2108a251eee6SJoshua Hay * idpf_set_features - set the netdev feature flags 2109a251eee6SJoshua Hay * @netdev: ptr to the netdev being adjusted 2110a251eee6SJoshua Hay * @features: the feature set that the stack is suggesting 2111a251eee6SJoshua Hay */ 2112a251eee6SJoshua Hay static int idpf_set_features(struct net_device *netdev, 2113a251eee6SJoshua Hay netdev_features_t features) 2114a251eee6SJoshua Hay { 2115a251eee6SJoshua Hay netdev_features_t changed = netdev->features ^ features; 2116a251eee6SJoshua Hay struct idpf_adapter *adapter; 2117a251eee6SJoshua Hay struct idpf_vport *vport; 2118a251eee6SJoshua Hay int err = 0; 2119a251eee6SJoshua Hay 2120a251eee6SJoshua Hay idpf_vport_ctrl_lock(netdev); 2121a251eee6SJoshua Hay vport = idpf_netdev_to_vport(netdev); 2122a251eee6SJoshua Hay 2123a251eee6SJoshua Hay adapter = vport->adapter; 2124a251eee6SJoshua Hay 2125a251eee6SJoshua Hay if (idpf_is_reset_in_prog(adapter)) { 2126a251eee6SJoshua Hay dev_err(&adapter->pdev->dev, "Device is resetting, changing netdev features temporarily unavailable.\n"); 2127a251eee6SJoshua Hay err = -EBUSY; 2128a251eee6SJoshua Hay goto unlock_mutex; 2129a251eee6SJoshua Hay } 2130a251eee6SJoshua Hay 2131a251eee6SJoshua Hay if (changed & NETIF_F_RXHASH) { 2132a251eee6SJoshua Hay netdev->features ^= NETIF_F_RXHASH; 2133a251eee6SJoshua Hay err = idpf_vport_manage_rss_lut(vport); 2134a251eee6SJoshua Hay if (err) 2135a251eee6SJoshua Hay goto unlock_mutex; 2136a251eee6SJoshua Hay } 2137a251eee6SJoshua Hay 2138a251eee6SJoshua Hay if (changed & NETIF_F_GRO_HW) { 2139a251eee6SJoshua Hay netdev->features ^= NETIF_F_GRO_HW; 2140a251eee6SJoshua Hay err = idpf_initiate_soft_reset(vport, IDPF_SR_RSC_CHANGE); 2141a251eee6SJoshua Hay if (err) 2142a251eee6SJoshua Hay goto unlock_mutex; 2143a251eee6SJoshua Hay } 2144a251eee6SJoshua Hay 2145a251eee6SJoshua Hay if (changed & NETIF_F_LOOPBACK) { 2146a251eee6SJoshua Hay netdev->features ^= NETIF_F_LOOPBACK; 2147a251eee6SJoshua Hay err = idpf_send_ena_dis_loopback_msg(vport); 2148a251eee6SJoshua Hay } 2149a251eee6SJoshua Hay 2150a251eee6SJoshua Hay unlock_mutex: 2151a251eee6SJoshua Hay idpf_vport_ctrl_unlock(netdev); 2152a251eee6SJoshua Hay 2153a251eee6SJoshua Hay return err; 2154a251eee6SJoshua Hay } 2155a251eee6SJoshua Hay 2156a251eee6SJoshua Hay /** 21571c325aacSAlan Brady * idpf_open - Called when a network interface becomes active 21581c325aacSAlan Brady * @netdev: network interface device structure 21591c325aacSAlan Brady * 21601c325aacSAlan Brady * The open entry point is called when a network interface is made 21611c325aacSAlan Brady * active by the system (IFF_UP). At this point all resources needed 21621c325aacSAlan Brady * for transmit and receive operations are allocated, the interrupt 21631c325aacSAlan Brady * handler is registered with the OS, the netdev watchdog is enabled, 21641c325aacSAlan Brady * and the stack is notified that the interface is ready. 21651c325aacSAlan Brady * 21661c325aacSAlan Brady * Returns 0 on success, negative value on failure 21671c325aacSAlan Brady */ 21681c325aacSAlan Brady static int idpf_open(struct net_device *netdev) 21691c325aacSAlan Brady { 21701c325aacSAlan Brady struct idpf_vport *vport; 21711c325aacSAlan Brady int err; 21721c325aacSAlan Brady 21731c325aacSAlan Brady idpf_vport_ctrl_lock(netdev); 21741c325aacSAlan Brady vport = idpf_netdev_to_vport(netdev); 21751c325aacSAlan Brady 21761c325aacSAlan Brady err = idpf_vport_open(vport, true); 21771c325aacSAlan Brady 21781c325aacSAlan Brady idpf_vport_ctrl_unlock(netdev); 21791c325aacSAlan Brady 21801c325aacSAlan Brady return err; 21811c325aacSAlan Brady } 21821c325aacSAlan Brady 21831c325aacSAlan Brady /** 2184a251eee6SJoshua Hay * idpf_change_mtu - NDO callback to change the MTU 2185a251eee6SJoshua Hay * @netdev: network interface device structure 2186a251eee6SJoshua Hay * @new_mtu: new value for maximum frame size 2187a251eee6SJoshua Hay * 2188a251eee6SJoshua Hay * Returns 0 on success, negative on failure 2189a251eee6SJoshua Hay */ 2190a251eee6SJoshua Hay static int idpf_change_mtu(struct net_device *netdev, int new_mtu) 2191a251eee6SJoshua Hay { 2192a251eee6SJoshua Hay struct idpf_vport *vport; 2193a251eee6SJoshua Hay int err; 2194a251eee6SJoshua Hay 2195a251eee6SJoshua Hay idpf_vport_ctrl_lock(netdev); 2196a251eee6SJoshua Hay vport = idpf_netdev_to_vport(netdev); 2197a251eee6SJoshua Hay 21981eb2cdedSEric Dumazet WRITE_ONCE(netdev->mtu, new_mtu); 2199a251eee6SJoshua Hay 2200a251eee6SJoshua Hay err = idpf_initiate_soft_reset(vport, IDPF_SR_MTU_CHANGE); 2201a251eee6SJoshua Hay 2202a251eee6SJoshua Hay idpf_vport_ctrl_unlock(netdev); 2203a251eee6SJoshua Hay 2204a251eee6SJoshua Hay return err; 2205a251eee6SJoshua Hay } 2206a251eee6SJoshua Hay 2207a251eee6SJoshua Hay /** 2208a251eee6SJoshua Hay * idpf_features_check - Validate packet conforms to limits 2209a251eee6SJoshua Hay * @skb: skb buffer 2210a251eee6SJoshua Hay * @netdev: This port's netdev 2211a251eee6SJoshua Hay * @features: Offload features that the stack believes apply 2212a251eee6SJoshua Hay */ 2213a251eee6SJoshua Hay static netdev_features_t idpf_features_check(struct sk_buff *skb, 2214a251eee6SJoshua Hay struct net_device *netdev, 2215a251eee6SJoshua Hay netdev_features_t features) 2216a251eee6SJoshua Hay { 2217a251eee6SJoshua Hay struct idpf_vport *vport = idpf_netdev_to_vport(netdev); 2218a251eee6SJoshua Hay struct idpf_adapter *adapter = vport->adapter; 2219a251eee6SJoshua Hay size_t len; 2220a251eee6SJoshua Hay 2221a251eee6SJoshua Hay /* No point in doing any of this if neither checksum nor GSO are 2222a251eee6SJoshua Hay * being requested for this frame. We can rule out both by just 2223a251eee6SJoshua Hay * checking for CHECKSUM_PARTIAL 2224a251eee6SJoshua Hay */ 2225a251eee6SJoshua Hay if (skb->ip_summed != CHECKSUM_PARTIAL) 2226a251eee6SJoshua Hay return features; 2227a251eee6SJoshua Hay 2228a251eee6SJoshua Hay /* We cannot support GSO if the MSS is going to be less than 2229a251eee6SJoshua Hay * 88 bytes. If it is then we need to drop support for GSO. 2230a251eee6SJoshua Hay */ 2231a251eee6SJoshua Hay if (skb_is_gso(skb) && 2232a251eee6SJoshua Hay (skb_shinfo(skb)->gso_size < IDPF_TX_TSO_MIN_MSS)) 2233a251eee6SJoshua Hay features &= ~NETIF_F_GSO_MASK; 2234a251eee6SJoshua Hay 2235a251eee6SJoshua Hay /* Ensure MACLEN is <= 126 bytes (63 words) and not an odd size */ 2236a251eee6SJoshua Hay len = skb_network_offset(skb); 2237a251eee6SJoshua Hay if (unlikely(len & ~(126))) 2238a251eee6SJoshua Hay goto unsupported; 2239a251eee6SJoshua Hay 2240a251eee6SJoshua Hay len = skb_network_header_len(skb); 2241a251eee6SJoshua Hay if (unlikely(len > idpf_get_max_tx_hdr_size(adapter))) 2242a251eee6SJoshua Hay goto unsupported; 2243a251eee6SJoshua Hay 2244a251eee6SJoshua Hay if (!skb->encapsulation) 2245a251eee6SJoshua Hay return features; 2246a251eee6SJoshua Hay 2247a251eee6SJoshua Hay /* L4TUNLEN can support 127 words */ 2248a251eee6SJoshua Hay len = skb_inner_network_header(skb) - skb_transport_header(skb); 2249a251eee6SJoshua Hay if (unlikely(len & ~(127 * 2))) 2250a251eee6SJoshua Hay goto unsupported; 2251a251eee6SJoshua Hay 2252a251eee6SJoshua Hay /* IPLEN can support at most 127 dwords */ 2253a251eee6SJoshua Hay len = skb_inner_network_header_len(skb); 2254a251eee6SJoshua Hay if (unlikely(len > idpf_get_max_tx_hdr_size(adapter))) 2255a251eee6SJoshua Hay goto unsupported; 2256a251eee6SJoshua Hay 2257a251eee6SJoshua Hay /* No need to validate L4LEN as TCP is the only protocol with a 2258a251eee6SJoshua Hay * a flexible value and we support all possible values supported 2259a251eee6SJoshua Hay * by TCP, which is at most 15 dwords 2260a251eee6SJoshua Hay */ 2261a251eee6SJoshua Hay 2262a251eee6SJoshua Hay return features; 2263a251eee6SJoshua Hay 2264a251eee6SJoshua Hay unsupported: 2265a251eee6SJoshua Hay return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 2266a251eee6SJoshua Hay } 2267a251eee6SJoshua Hay 2268a251eee6SJoshua Hay /** 2269a251eee6SJoshua Hay * idpf_set_mac - NDO callback to set port mac address 2270a251eee6SJoshua Hay * @netdev: network interface device structure 2271a251eee6SJoshua Hay * @p: pointer to an address structure 2272a251eee6SJoshua Hay * 2273a251eee6SJoshua Hay * Returns 0 on success, negative on failure 2274a251eee6SJoshua Hay **/ 2275a251eee6SJoshua Hay static int idpf_set_mac(struct net_device *netdev, void *p) 2276a251eee6SJoshua Hay { 2277a251eee6SJoshua Hay struct idpf_netdev_priv *np = netdev_priv(netdev); 2278a251eee6SJoshua Hay struct idpf_vport_config *vport_config; 2279a251eee6SJoshua Hay struct sockaddr *addr = p; 2280a251eee6SJoshua Hay struct idpf_vport *vport; 2281a251eee6SJoshua Hay int err = 0; 2282a251eee6SJoshua Hay 2283a251eee6SJoshua Hay idpf_vport_ctrl_lock(netdev); 2284a251eee6SJoshua Hay vport = idpf_netdev_to_vport(netdev); 2285a251eee6SJoshua Hay 2286a251eee6SJoshua Hay if (!idpf_is_cap_ena(vport->adapter, IDPF_OTHER_CAPS, 2287a251eee6SJoshua Hay VIRTCHNL2_CAP_MACFILTER)) { 2288a251eee6SJoshua Hay dev_info(&vport->adapter->pdev->dev, "Setting MAC address is not supported\n"); 2289a251eee6SJoshua Hay err = -EOPNOTSUPP; 2290a251eee6SJoshua Hay goto unlock_mutex; 2291a251eee6SJoshua Hay } 2292a251eee6SJoshua Hay 2293a251eee6SJoshua Hay if (!is_valid_ether_addr(addr->sa_data)) { 2294a251eee6SJoshua Hay dev_info(&vport->adapter->pdev->dev, "Invalid MAC address: %pM\n", 2295a251eee6SJoshua Hay addr->sa_data); 2296a251eee6SJoshua Hay err = -EADDRNOTAVAIL; 2297a251eee6SJoshua Hay goto unlock_mutex; 2298a251eee6SJoshua Hay } 2299a251eee6SJoshua Hay 2300a251eee6SJoshua Hay if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) 2301a251eee6SJoshua Hay goto unlock_mutex; 2302a251eee6SJoshua Hay 2303a251eee6SJoshua Hay vport_config = vport->adapter->vport_config[vport->idx]; 2304a251eee6SJoshua Hay err = idpf_add_mac_filter(vport, np, addr->sa_data, false); 2305a251eee6SJoshua Hay if (err) { 2306a251eee6SJoshua Hay __idpf_del_mac_filter(vport_config, addr->sa_data); 2307a251eee6SJoshua Hay goto unlock_mutex; 2308a251eee6SJoshua Hay } 2309a251eee6SJoshua Hay 2310a251eee6SJoshua Hay if (is_valid_ether_addr(vport->default_mac_addr)) 2311a251eee6SJoshua Hay idpf_del_mac_filter(vport, np, vport->default_mac_addr, false); 2312a251eee6SJoshua Hay 2313a251eee6SJoshua Hay ether_addr_copy(vport->default_mac_addr, addr->sa_data); 2314a251eee6SJoshua Hay eth_hw_addr_set(netdev, addr->sa_data); 2315a251eee6SJoshua Hay 2316a251eee6SJoshua Hay unlock_mutex: 2317a251eee6SJoshua Hay idpf_vport_ctrl_unlock(netdev); 2318a251eee6SJoshua Hay 2319a251eee6SJoshua Hay return err; 2320a251eee6SJoshua Hay } 2321a251eee6SJoshua Hay 2322a251eee6SJoshua Hay /** 23238077c727SJoshua Hay * idpf_alloc_dma_mem - Allocate dma memory 23248077c727SJoshua Hay * @hw: pointer to hw struct 23258077c727SJoshua Hay * @mem: pointer to dma_mem struct 23268077c727SJoshua Hay * @size: size of the memory to allocate 23278077c727SJoshua Hay */ 23288077c727SJoshua Hay void *idpf_alloc_dma_mem(struct idpf_hw *hw, struct idpf_dma_mem *mem, u64 size) 23298077c727SJoshua Hay { 23308077c727SJoshua Hay struct idpf_adapter *adapter = hw->back; 23318077c727SJoshua Hay size_t sz = ALIGN(size, 4096); 23328077c727SJoshua Hay 23338077c727SJoshua Hay mem->va = dma_alloc_coherent(&adapter->pdev->dev, sz, 23348077c727SJoshua Hay &mem->pa, GFP_KERNEL); 23358077c727SJoshua Hay mem->size = sz; 23368077c727SJoshua Hay 23378077c727SJoshua Hay return mem->va; 23388077c727SJoshua Hay } 23398077c727SJoshua Hay 23408077c727SJoshua Hay /** 23418077c727SJoshua Hay * idpf_free_dma_mem - Free the allocated dma memory 23428077c727SJoshua Hay * @hw: pointer to hw struct 23438077c727SJoshua Hay * @mem: pointer to dma_mem struct 23448077c727SJoshua Hay */ 23458077c727SJoshua Hay void idpf_free_dma_mem(struct idpf_hw *hw, struct idpf_dma_mem *mem) 23468077c727SJoshua Hay { 23478077c727SJoshua Hay struct idpf_adapter *adapter = hw->back; 23488077c727SJoshua Hay 23498077c727SJoshua Hay dma_free_coherent(&adapter->pdev->dev, mem->size, 23508077c727SJoshua Hay mem->va, mem->pa); 23518077c727SJoshua Hay mem->size = 0; 23528077c727SJoshua Hay mem->va = NULL; 23538077c727SJoshua Hay mem->pa = 0; 23548077c727SJoshua Hay } 23551c325aacSAlan Brady 23561c325aacSAlan Brady static const struct net_device_ops idpf_netdev_ops_splitq = { 23571c325aacSAlan Brady .ndo_open = idpf_open, 23581c325aacSAlan Brady .ndo_stop = idpf_stop, 23596818c4d5SJoshua Hay .ndo_start_xmit = idpf_tx_splitq_start, 2360a251eee6SJoshua Hay .ndo_features_check = idpf_features_check, 2361a251eee6SJoshua Hay .ndo_set_rx_mode = idpf_set_rx_mode, 2362a251eee6SJoshua Hay .ndo_validate_addr = eth_validate_addr, 2363a251eee6SJoshua Hay .ndo_set_mac_address = idpf_set_mac, 2364a251eee6SJoshua Hay .ndo_change_mtu = idpf_change_mtu, 2365a251eee6SJoshua Hay .ndo_get_stats64 = idpf_get_stats64, 2366a251eee6SJoshua Hay .ndo_set_features = idpf_set_features, 2367a251eee6SJoshua Hay .ndo_tx_timeout = idpf_tx_timeout, 23681c325aacSAlan Brady }; 23691c325aacSAlan Brady 23701c325aacSAlan Brady static const struct net_device_ops idpf_netdev_ops_singleq = { 23711c325aacSAlan Brady .ndo_open = idpf_open, 23721c325aacSAlan Brady .ndo_stop = idpf_stop, 2373a5ab9ee0SJoshua Hay .ndo_start_xmit = idpf_tx_singleq_start, 2374a251eee6SJoshua Hay .ndo_features_check = idpf_features_check, 2375a251eee6SJoshua Hay .ndo_set_rx_mode = idpf_set_rx_mode, 2376a251eee6SJoshua Hay .ndo_validate_addr = eth_validate_addr, 2377a251eee6SJoshua Hay .ndo_set_mac_address = idpf_set_mac, 2378a251eee6SJoshua Hay .ndo_change_mtu = idpf_change_mtu, 2379a251eee6SJoshua Hay .ndo_get_stats64 = idpf_get_stats64, 2380a251eee6SJoshua Hay .ndo_set_features = idpf_set_features, 2381a251eee6SJoshua Hay .ndo_tx_timeout = idpf_tx_timeout, 23821c325aacSAlan Brady }; 2383