Skip to content

Commit ca3c342

Browse files
damien-lemoalMani-Sadhasivam
authored andcommitted
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>
1 parent 9852d85 commit ca3c342

File tree

1 file changed

+31
-48
lines changed

1 file changed

+31
-48
lines changed

drivers/pci/endpoint/pci-epc-core.c

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
128128
}
129129
EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar);
130130

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+
131143
/**
132144
* pci_epc_get_features() - get the features supported by EPC
133145
* @epc: the features supported by *this* EPC device will be returned
@@ -145,10 +157,7 @@ const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
145157
{
146158
const struct pci_epc_features *epc_features;
147159

148-
if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
149-
return NULL;
150-
151-
if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
160+
if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
152161
return NULL;
153162

154163
if (!epc->ops->get_features)
@@ -218,10 +227,7 @@ int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
218227
{
219228
int ret;
220229

221-
if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
222-
return -EINVAL;
223-
224-
if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
230+
if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
225231
return -EINVAL;
226232

227233
if (!epc->ops->raise_irq)
@@ -262,10 +268,7 @@ int pci_epc_map_msi_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
262268
{
263269
int ret;
264270

265-
if (IS_ERR_OR_NULL(epc))
266-
return -EINVAL;
267-
268-
if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
271+
if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
269272
return -EINVAL;
270273

271274
if (!epc->ops->map_msi_irq)
@@ -293,10 +296,7 @@ int pci_epc_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
293296
{
294297
int interrupt;
295298

296-
if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
297-
return 0;
298-
299-
if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
299+
if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
300300
return 0;
301301

302302
if (!epc->ops->get_msi)
@@ -329,11 +329,10 @@ int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no, u8 interrupts)
329329
int ret;
330330
u8 encode_int;
331331

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))
334333
return -EINVAL;
335334

336-
if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
335+
if (interrupts < 1 || interrupts > 32)
337336
return -EINVAL;
338337

339338
if (!epc->ops->set_msi)
@@ -361,10 +360,7 @@ int pci_epc_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
361360
{
362361
int interrupt;
363362

364-
if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
365-
return 0;
366-
367-
if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
363+
if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
368364
return 0;
369365

370366
if (!epc->ops->get_msix)
@@ -397,11 +393,10 @@ int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
397393
{
398394
int ret;
399395

400-
if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
401-
interrupts < 1 || interrupts > 2048)
396+
if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
402397
return -EINVAL;
403398

404-
if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
399+
if (interrupts < 1 || interrupts > 2048)
405400
return -EINVAL;
406401

407402
if (!epc->ops->set_msix)
@@ -428,10 +423,7 @@ EXPORT_SYMBOL_GPL(pci_epc_set_msix);
428423
void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
429424
phys_addr_t phys_addr)
430425
{
431-
if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
432-
return;
433-
434-
if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
426+
if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
435427
return;
436428

437429
if (!epc->ops->unmap_addr)
@@ -459,10 +451,7 @@ int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
459451
{
460452
int ret;
461453

462-
if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
463-
return -EINVAL;
464-
465-
if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
454+
if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
466455
return -EINVAL;
467456

468457
if (!epc->ops->map_addr)
@@ -489,12 +478,11 @@ EXPORT_SYMBOL_GPL(pci_epc_map_addr);
489478
void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
490479
struct pci_epf_bar *epf_bar)
491480
{
492-
if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
493-
(epf_bar->barno == BAR_5 &&
494-
epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
481+
if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
495482
return;
496483

497-
if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
484+
if (epf_bar->barno == BAR_5 &&
485+
epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64)
498486
return;
499487

500488
if (!epc->ops->clear_bar)
@@ -521,18 +509,16 @@ int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
521509
int ret;
522510
int flags = epf_bar->flags;
523511

524-
if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
525-
(epf_bar->barno == BAR_5 &&
526-
flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
512+
if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
513+
return -EINVAL;
514+
515+
if ((epf_bar->barno == BAR_5 && flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
527516
(flags & PCI_BASE_ADDRESS_SPACE_IO &&
528517
flags & PCI_BASE_ADDRESS_IO_MASK) ||
529518
(upper_32_bits(epf_bar->size) &&
530519
!(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
531520
return -EINVAL;
532521

533-
if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
534-
return -EINVAL;
535-
536522
if (!epc->ops->set_bar)
537523
return 0;
538524

@@ -561,10 +547,7 @@ int pci_epc_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
561547
{
562548
int ret;
563549

564-
if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
565-
return -EINVAL;
566-
567-
if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
550+
if (!pci_epc_function_is_valid(epc, func_no, vfunc_no))
568551
return -EINVAL;
569552

570553
/* Only Virtual Function #1 has deviceID */

0 commit comments

Comments
 (0)