@@ -1028,46 +1028,56 @@ static bool hns3_can_use_tx_sgl(struct hns3_enet_ring *ring,
10281028
10291029static void hns3_init_tx_spare_buffer (struct hns3_enet_ring * ring )
10301030{
1031+ u32 alloc_size = ring -> tqp -> handle -> kinfo .tx_spare_buf_size ;
10311032 struct hns3_tx_spare * tx_spare ;
10321033 struct page * page ;
1033- u32 alloc_size ;
10341034 dma_addr_t dma ;
10351035 int order ;
10361036
1037- alloc_size = ring -> tqp -> handle -> kinfo .tx_spare_buf_size ;
10381037 if (!alloc_size )
10391038 return ;
10401039
10411040 order = get_order (alloc_size );
1041+ if (order >= MAX_ORDER ) {
1042+ if (net_ratelimit ())
1043+ dev_warn (ring_to_dev (ring ), "failed to allocate tx spare buffer, exceed to max order\n" );
1044+ return ;
1045+ }
1046+
10421047 tx_spare = devm_kzalloc (ring_to_dev (ring ), sizeof (* tx_spare ),
10431048 GFP_KERNEL );
10441049 if (!tx_spare ) {
10451050 /* The driver still work without the tx spare buffer */
10461051 dev_warn (ring_to_dev (ring ), "failed to allocate hns3_tx_spare\n" );
1047- return ;
1052+ goto devm_kzalloc_error ;
10481053 }
10491054
10501055 page = alloc_pages_node (dev_to_node (ring_to_dev (ring )),
10511056 GFP_KERNEL , order );
10521057 if (!page ) {
10531058 dev_warn (ring_to_dev (ring ), "failed to allocate tx spare pages\n" );
1054- devm_kfree (ring_to_dev (ring ), tx_spare );
1055- return ;
1059+ goto alloc_pages_error ;
10561060 }
10571061
10581062 dma = dma_map_page (ring_to_dev (ring ), page , 0 ,
10591063 PAGE_SIZE << order , DMA_TO_DEVICE );
10601064 if (dma_mapping_error (ring_to_dev (ring ), dma )) {
10611065 dev_warn (ring_to_dev (ring ), "failed to map pages for tx spare\n" );
1062- put_page (page );
1063- devm_kfree (ring_to_dev (ring ), tx_spare );
1064- return ;
1066+ goto dma_mapping_error ;
10651067 }
10661068
10671069 tx_spare -> dma = dma ;
10681070 tx_spare -> buf = page_address (page );
10691071 tx_spare -> len = PAGE_SIZE << order ;
10701072 ring -> tx_spare = tx_spare ;
1073+ return ;
1074+
1075+ dma_mapping_error :
1076+ put_page (page );
1077+ alloc_pages_error :
1078+ devm_kfree (ring_to_dev (ring ), tx_spare );
1079+ devm_kzalloc_error :
1080+ ring -> tqp -> handle -> kinfo .tx_spare_buf_size = 0 ;
10711081}
10721082
10731083/* Use hns3_tx_spare_space() to make sure there is enough buffer
@@ -3050,6 +3060,21 @@ static int hns3_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
30503060 return ret ;
30513061}
30523062
3063+ /**
3064+ * hns3_clean_vf_config
3065+ * @pdev: pointer to a pci_dev structure
3066+ * @num_vfs: number of VFs allocated
3067+ *
3068+ * Clean residual vf config after disable sriov
3069+ **/
3070+ static void hns3_clean_vf_config (struct pci_dev * pdev , int num_vfs )
3071+ {
3072+ struct hnae3_ae_dev * ae_dev = pci_get_drvdata (pdev );
3073+
3074+ if (ae_dev -> ops -> clean_vf_config )
3075+ ae_dev -> ops -> clean_vf_config (ae_dev , num_vfs );
3076+ }
3077+
30533078/* hns3_remove - Device removal routine
30543079 * @pdev: PCI device information struct
30553080 */
@@ -3088,7 +3113,10 @@ static int hns3_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
30883113 else
30893114 return num_vfs ;
30903115 } else if (!pci_vfs_assigned (pdev )) {
3116+ int num_vfs_pre = pci_num_vf (pdev );
3117+
30913118 pci_disable_sriov (pdev );
3119+ hns3_clean_vf_config (pdev , num_vfs_pre );
30923120 } else {
30933121 dev_warn (& pdev -> dev ,
30943122 "Unable to free VFs because some are assigned to VMs.\n" );
0 commit comments