Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

PCI: endpoint: Introduce pci_epc_function_is_valid()

Introduce the epc core helper function pci_epc_function_is_valid() to
verify that an epc pointer, a physical function number and a virtual
function number are all valid. This avoids repeating the code pattern:

if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
return err;

if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
return err;

in many functions of the endpoint controller core code.

Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Reviewed-by: Niklas Cassel <cassel@kernel.org>
Link: https://lore.kernel.org/r/20241012113246.95634-2-dlemoal@kernel.org
Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>

authored by

Damien Le Moal and committed by
Manivannan Sadhasivam
ca3c342f 9852d85e

+31 -48
+31 -48
drivers/pci/endpoint/pci-epc-core.c
··· 128 128 } 129 129 EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar); 130 130 131 + static bool pci_epc_function_is_valid(struct pci_epc *epc, 132 + u8 func_no, u8 vfunc_no) 133 + { 134 + if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 135 + return false; 136 + 137 + if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 138 + return false; 139 + 140 + return true; 141 + } 142 + 131 143 /** 132 144 * pci_epc_get_features() - get the features supported by EPC 133 145 * @epc: the features supported by *this* EPC device will be returned ··· 157 145 { 158 146 const struct pci_epc_features *epc_features; 159 147 160 - if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 161 - return NULL; 162 - 163 - if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 148 + if (!pci_epc_function_is_valid(epc, func_no, vfunc_no)) 164 149 return NULL; 165 150 166 151 if (!epc->ops->get_features) ··· 227 218 { 228 219 int ret; 229 220 230 - if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 231 - return -EINVAL; 232 - 233 - if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 221 + if (!pci_epc_function_is_valid(epc, func_no, vfunc_no)) 234 222 return -EINVAL; 235 223 236 224 if (!epc->ops->raise_irq) ··· 268 262 { 269 263 int ret; 270 264 271 - if (IS_ERR_OR_NULL(epc)) 272 - return -EINVAL; 273 - 274 - if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 265 + if (!pci_epc_function_is_valid(epc, func_no, vfunc_no)) 275 266 return -EINVAL; 276 267 277 268 if (!epc->ops->map_msi_irq) ··· 296 293 { 297 294 int interrupt; 298 295 299 - if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 300 - return 0; 301 - 302 - if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 296 + if (!pci_epc_function_is_valid(epc, func_no, vfunc_no)) 303 297 return 0; 304 298 305 299 if (!epc->ops->get_msi) ··· 329 329 int ret; 330 330 u8 encode_int; 331 331 332 - if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions || 333 - interrupts < 1 || interrupts > 32) 332 + if (!pci_epc_function_is_valid(epc, func_no, vfunc_no)) 334 333 return -EINVAL; 335 334 336 - if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 335 + if (interrupts < 1 || interrupts > 32) 337 336 return -EINVAL; 338 337 339 338 if (!epc->ops->set_msi) ··· 360 361 { 361 362 int interrupt; 362 363 363 - if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 364 - return 0; 365 - 366 - if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 364 + if (!pci_epc_function_is_valid(epc, func_no, vfunc_no)) 367 365 return 0; 368 366 369 367 if (!epc->ops->get_msix) ··· 393 397 { 394 398 int ret; 395 399 396 - if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions || 397 - interrupts < 1 || interrupts > 2048) 400 + if (!pci_epc_function_is_valid(epc, func_no, vfunc_no)) 398 401 return -EINVAL; 399 402 400 - if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 403 + if (interrupts < 1 || interrupts > 2048) 401 404 return -EINVAL; 402 405 403 406 if (!epc->ops->set_msix) ··· 423 428 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no, 424 429 phys_addr_t phys_addr) 425 430 { 426 - if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 427 - return; 428 - 429 - if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 431 + if (!pci_epc_function_is_valid(epc, func_no, vfunc_no)) 430 432 return; 431 433 432 434 if (!epc->ops->unmap_addr) ··· 451 459 { 452 460 int ret; 453 461 454 - if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 455 - return -EINVAL; 456 - 457 - if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 462 + if (!pci_epc_function_is_valid(epc, func_no, vfunc_no)) 458 463 return -EINVAL; 459 464 460 465 if (!epc->ops->map_addr) ··· 478 489 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no, 479 490 struct pci_epf_bar *epf_bar) 480 491 { 481 - if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions || 482 - (epf_bar->barno == BAR_5 && 483 - epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64)) 492 + if (!pci_epc_function_is_valid(epc, func_no, vfunc_no)) 484 493 return; 485 494 486 - if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 495 + if (epf_bar->barno == BAR_5 && 496 + epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64) 487 497 return; 488 498 489 499 if (!epc->ops->clear_bar) ··· 509 521 int ret; 510 522 int flags = epf_bar->flags; 511 523 512 - if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions || 513 - (epf_bar->barno == BAR_5 && 514 - flags & PCI_BASE_ADDRESS_MEM_TYPE_64) || 524 + if (!pci_epc_function_is_valid(epc, func_no, vfunc_no)) 525 + return -EINVAL; 526 + 527 + if ((epf_bar->barno == BAR_5 && flags & PCI_BASE_ADDRESS_MEM_TYPE_64) || 515 528 (flags & PCI_BASE_ADDRESS_SPACE_IO && 516 529 flags & PCI_BASE_ADDRESS_IO_MASK) || 517 530 (upper_32_bits(epf_bar->size) && 518 531 !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64))) 519 - return -EINVAL; 520 - 521 - if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 522 532 return -EINVAL; 523 533 524 534 if (!epc->ops->set_bar) ··· 547 561 { 548 562 int ret; 549 563 550 - if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions) 551 - return -EINVAL; 552 - 553 - if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no])) 564 + if (!pci_epc_function_is_valid(epc, func_no, vfunc_no)) 554 565 return -EINVAL; 555 566 556 567 /* Only Virtual Function #1 has deviceID */