From nobody Tue Feb 11 14:12:23 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Ysk0l3rQlz5ndZg; Tue, 11 Feb 2025 14:12:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Ysk0l1tsgz4KJ0; Tue, 11 Feb 2025 14:12:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1739283143; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Jysj1QY3rytVMgcgClky78JtOdEK+8WChO8SlvPnpUQ=; b=pAqRpq2rPXoQQ5dnaribYx02yu9wj8R+/pNkgGVaWtpaxkR5PW8ZHk/W/eONLpXvJky0x7 Umg7UIG9tXwMPd0l20lHbVvNoH9nGfs/5E7y4JDGEGH8na4c9owNZPqK4TxQzCsjZG7P3P kFfUwkrNIgdfZsPgs3tx6pgrCq+NLyfmuEtLrjYyKMryERIhVCgcy1glh7iuyHpqqzc26+ XQShYpUaBVhw6OyuTdjSR02+oAsGGkh/iIH6bV+LfJodKIOEbcgZiShN8Z5oaFG+INpoi5 WmsaSdm46mr3YPL4pDvhy+dQxmMnAM50J1Brsxdg7UpSsDw9u5WAd+4hNEZEsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1739283143; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Jysj1QY3rytVMgcgClky78JtOdEK+8WChO8SlvPnpUQ=; b=FSPTB5MClxDF13Rh2yxo17Bp5LGUjVh0iXCs4a5OLnWr7FEmSsqg5hO+Z/dkl+FHOxzIzv G1WxGh3GOgCT7InEEKgzwQW8heUbqK4DyGXaDbM+Dfhm3qBGbbffSQf27nSoQpxRSkKbF8 8wGfjLkw8LwXWYLPtGWoxPXch2uX+OtkDSlEZcNcG3wVNclNLt8u4ledLdHgA+d4z+oFE3 VAiJ9/75CXKeIs34Q1jjH1F8h0VwlUNc257kju4lsvIAtfxHE8UK5SKkC0J3+GSNI+STk0 tCbAQ8loBa8rdZjykSpTYHnMUaep6AB2OXN4ghJ/KD9++MDX57tZNhouowYJOg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1739283143; a=rsa-sha256; cv=none; b=FTWh1htEVNEyTrMlPqNrtZmUT4BlHBEb1P4E83QGP6Fm/07MIDYUKxQ/iZMocgmh76U8YU jTGNfCK9MbX/eMmIzVIup0l+m2Artho8FuQ7AINomQopMVvuDBhCpauUMK5K11DPfB+FeK +wwpYWl00zVkclmq++2LBlt/seacZbnJ543UhlSkYEJ8n6NLDjxMpUPwkyyzkn0gjrfzMu BZeeu7KcofnyYWinDnPdFaz9F0kId25BCgjS8LRm+zQGLVHT7FpSpEJr4oBknkLCLxdbmh xfjiJy9CJQao4tcGIaWzgJKOUTPJDs+ajcuDYFaKUB2t46YjciIAa6ke/D5cqg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Ysk0l1RJRz1RX; Tue, 11 Feb 2025 14:12:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 51BECNVh058720; Tue, 11 Feb 2025 14:12:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 51BECNQw058717; Tue, 11 Feb 2025 14:12:23 GMT (envelope-from git) Date: Tue, 11 Feb 2025 14:12:23 GMT Message-Id: <202502111412.51BECNQw058717@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: John Baldwin Subject: git: a3835cceb088 - main - pci: Cosmetic cleanups to MSI/MSI-X routines List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: a3835cceb0882494628d5d075f99a86bd3464672 Auto-Submitted: auto-generated The branch main has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=a3835cceb0882494628d5d075f99a86bd3464672 commit a3835cceb0882494628d5d075f99a86bd3464672 Author: John Baldwin AuthorDate: 2025-02-11 14:11:10 +0000 Commit: John Baldwin CommitDate: 2025-02-11 14:11:10 +0000 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 Differential Revision: https://reviews.freebsd.org/D48889 --- sys/dev/pci/pci.c | 63 ++++++++++++++++++++++++++++------------------------ sys/dev/pci/pcivar.h | 4 ++-- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/sys/dev/pci/pci.c b/sys/dev/pci/pci.c index 84db16587bde..06bff2e96e89 100644 --- a/sys/dev/pci/pci.c +++ b/sys/dev/pci/pci.c @@ -1790,7 +1790,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. */ @@ -1823,10 +1823,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. */ @@ -1886,7 +1887,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 @@ -1895,14 +1896,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; } @@ -1910,7 +1911,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. */ @@ -1930,10 +1931,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; @@ -1998,14 +1999,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. */ @@ -2018,17 +2020,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); } @@ -2061,7 +2063,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]; @@ -2069,15 +2071,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)); @@ -2119,7 +2121,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) @@ -2440,7 +2442,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 @@ -2605,11 +2608,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. */ @@ -2670,7 +2674,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 @@ -2678,18 +2682,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. */ @@ -2724,7 +2728,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); diff --git a/sys/dev/pci/pcivar.h b/sys/dev/pci/pcivar.h index 1c7b772afea9..0041c5a22b49 100644 --- a/sys/dev/pci/pcivar.h +++ b/sys/dev/pci/pcivar.h @@ -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. */