pci: Cosmetic cleanups to MSI/MSI-X routines

- Use unsigned integers for various variables.  The count argument
  to the alloc method as well as the IRQ values used with the
  pcib_if.m methods should also be unsigned eventually.

- Use mallocarray to allocate arrays

- Use bool in a few places

Reviewed by:	Krzysztof Galazka <krzysztof.galazka@intel.com>
Differential Revision:	https://reviews.freebsd.org/D48889

(cherry picked from commit a3835cceb0882494628d5d075f99a86bd3464672)
This commit is contained in:
John Baldwin 2025-02-11 09:11:10 -05:00
parent cbaa32c28a
commit 14749f40c1
2 changed files with 36 additions and 31 deletions

View file

@ -1775,7 +1775,7 @@ pci_resume_msix(device_t dev)
struct pcicfg_msix *msix = &dinfo->cfg.msix;
struct msix_table_entry *mte;
struct msix_vector *mv;
int i;
u_int i;
if (msix->msix_alloc > 0) {
/* First, mask all vectors. */
@ -1808,10 +1808,11 @@ pci_alloc_msix_method(device_t dev, device_t child, int *count)
struct pci_devinfo *dinfo = device_get_ivars(child);
pcicfgregs *cfg = &dinfo->cfg;
struct resource_list_entry *rle;
int actual, error, i, irq, max;
u_int actual, i, max;
int error, irq;
/* Don't let count == 0 get us into trouble. */
if (*count == 0)
if (*count < 1)
return (EINVAL);
/* If rid 0 is allocated, then fail. */
@ -1871,7 +1872,7 @@ pci_alloc_msix_method(device_t dev, device_t child, int *count)
device_printf(child, "using IRQ %ju for MSI-X\n",
rle->start);
else {
int run;
bool run;
/*
* Be fancy and try to print contiguous runs of
@ -1880,14 +1881,14 @@ pci_alloc_msix_method(device_t dev, device_t child, int *count)
*/
device_printf(child, "using IRQs %ju", rle->start);
irq = rle->start;
run = 0;
run = false;
for (i = 1; i < actual; i++) {
rle = resource_list_find(&dinfo->resources,
SYS_RES_IRQ, i + 1);
/* Still in a run? */
if (rle->start == irq + 1) {
run = 1;
run = true;
irq++;
continue;
}
@ -1895,7 +1896,7 @@ pci_alloc_msix_method(device_t dev, device_t child, int *count)
/* Finish previous range. */
if (run) {
printf("-%d", irq);
run = 0;
run = false;
}
/* Start new range. */
@ -1915,10 +1916,10 @@ pci_alloc_msix_method(device_t dev, device_t child, int *count)
pci_mask_msix(child, i);
/* Allocate and initialize vector data and virtual table. */
cfg->msix.msix_vectors = malloc(sizeof(struct msix_vector) * actual,
M_DEVBUF, M_WAITOK | M_ZERO);
cfg->msix.msix_table = malloc(sizeof(struct msix_table_entry) * actual,
cfg->msix.msix_vectors = mallocarray(actual, sizeof(struct msix_vector),
M_DEVBUF, M_WAITOK | M_ZERO);
cfg->msix.msix_table = mallocarray(actual,
sizeof(struct msix_table_entry), M_DEVBUF, M_WAITOK | M_ZERO);
for (i = 0; i < actual; i++) {
rle = resource_list_find(&dinfo->resources, SYS_RES_IRQ, i + 1);
cfg->msix.msix_vectors[i].mv_irq = rle->start;
@ -1983,14 +1984,15 @@ pci_remap_msix_method(device_t dev, device_t child, int count,
struct pci_devinfo *dinfo = device_get_ivars(child);
struct pcicfg_msix *msix = &dinfo->cfg.msix;
struct resource_list_entry *rle;
int i, irq, j, *used;
u_int i, irq, j;
bool *used;
/*
* Have to have at least one message in the table but the
* table can't be bigger than the actual MSI-X table in the
* device.
*/
if (count == 0 || count > msix->msix_msgnum)
if (count < 1 || count > msix->msix_msgnum)
return (EINVAL);
/* Sanity check the vectors. */
@ -2003,17 +2005,17 @@ pci_remap_msix_method(device_t dev, device_t child, int count,
* It's a big pain to support it, and it doesn't really make
* sense anyway. Also, at least one vector must be used.
*/
used = malloc(sizeof(int) * msix->msix_alloc, M_DEVBUF, M_WAITOK |
used = mallocarray(msix->msix_alloc, sizeof(*used), M_DEVBUF, M_WAITOK |
M_ZERO);
for (i = 0; i < count; i++)
if (vectors[i] != 0)
used[vectors[i] - 1] = 1;
used[vectors[i] - 1] = true;
for (i = 0; i < msix->msix_alloc - 1; i++)
if (used[i] == 0 && used[i + 1] == 1) {
if (!used[i] && used[i + 1]) {
free(used, M_DEVBUF);
return (EINVAL);
}
if (used[0] != 1) {
if (!used[0]) {
free(used, M_DEVBUF);
return (EINVAL);
}
@ -2046,7 +2048,7 @@ pci_remap_msix_method(device_t dev, device_t child, int count,
* used.
*/
free(msix->msix_table, M_DEVBUF);
msix->msix_table = malloc(sizeof(struct msix_table_entry) * count,
msix->msix_table = mallocarray(count, sizeof(struct msix_table_entry),
M_DEVBUF, M_WAITOK | M_ZERO);
for (i = 0; i < count; i++)
msix->msix_table[i].mte_vector = vectors[i];
@ -2054,15 +2056,15 @@ pci_remap_msix_method(device_t dev, device_t child, int count,
/* Free any unused IRQs and resize the vectors array if necessary. */
j = msix->msix_alloc - 1;
if (used[j] == 0) {
if (!used[j]) {
struct msix_vector *vec;
while (used[j] == 0) {
while (!used[j]) {
PCIB_RELEASE_MSIX(device_get_parent(dev), child,
msix->msix_vectors[j].mv_irq);
j--;
}
vec = malloc(sizeof(struct msix_vector) * (j + 1), M_DEVBUF,
vec = mallocarray(j + 1, sizeof(struct msix_vector), M_DEVBUF,
M_WAITOK);
bcopy(msix->msix_vectors, vec, sizeof(struct msix_vector) *
(j + 1));
@ -2104,7 +2106,7 @@ pci_release_msix(device_t dev, device_t child)
struct pci_devinfo *dinfo = device_get_ivars(child);
struct pcicfg_msix *msix = &dinfo->cfg.msix;
struct resource_list_entry *rle;
int i;
u_int i;
/* Do we have any messages to release? */
if (msix->msix_alloc == 0)
@ -2425,7 +2427,8 @@ pci_remap_intr_method(device_t bus, device_t dev, u_int irq)
struct msix_vector *mv;
uint64_t addr;
uint32_t data;
int error, i, j;
u_int i, j;
int error;
/*
* Handle MSI first. We try to find this IRQ among our list
@ -2590,11 +2593,12 @@ pci_alloc_msi_method(device_t dev, device_t child, int *count)
struct pci_devinfo *dinfo = device_get_ivars(child);
pcicfgregs *cfg = &dinfo->cfg;
struct resource_list_entry *rle;
int actual, error, i, irqs[32];
u_int actual, i;
int error, irqs[32];
uint16_t ctrl;
/* Don't let count == 0 get us into trouble. */
if (*count == 0)
if (*count < 1)
return (EINVAL);
/* If rid 0 is allocated, then fail. */
@ -2655,7 +2659,7 @@ pci_alloc_msi_method(device_t dev, device_t child, int *count)
if (actual == 1)
device_printf(child, "using IRQ %d for MSI\n", irqs[0]);
else {
int run;
bool run;
/*
* Be fancy and try to print contiguous runs
@ -2663,18 +2667,18 @@ pci_alloc_msi_method(device_t dev, device_t child, int *count)
* we are in a range.
*/
device_printf(child, "using IRQs %d", irqs[0]);
run = 0;
run = false;
for (i = 1; i < actual; i++) {
/* Still in a run? */
if (irqs[i] == irqs[i - 1] + 1) {
run = 1;
run = true;
continue;
}
/* Finish previous range. */
if (run) {
printf("-%d", irqs[i - 1]);
run = 0;
run = false;
}
/* Start new range. */
@ -2709,7 +2713,8 @@ pci_release_msi_method(device_t dev, device_t child)
struct pci_devinfo *dinfo = device_get_ivars(child);
struct pcicfg_msi *msi = &dinfo->cfg.msi;
struct resource_list_entry *rle;
int error, i, irqs[32];
u_int i, irqs[32];
int error;
/* Try MSI-X first. */
error = pci_release_msix(dev, child);

View file

@ -117,8 +117,8 @@ struct pcicfg_msix {
uint8_t msix_pba_bar; /* BAR containing PBA. */
uint32_t msix_table_offset;
uint32_t msix_pba_offset;
int msix_alloc; /* Number of allocated vectors. */
int msix_table_len; /* Length of virtual table. */
u_int msix_alloc; /* Number of allocated vectors. */
u_int msix_table_len; /* Length of virtual table. */
struct msix_table_entry *msix_table; /* Virtual table. */
struct msix_vector *msix_vectors; /* Array of allocated vectors. */
struct resource *msix_table_res; /* Resource containing vector table. */