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

usb: xhci: simplify handling of Structural Parameters 1 values

The 32-bit read-only HCSPARAMS1 register contains the following fields:
Bits 7:0 - Number of Device Slots (MaxSlots)
Bits 18:8 - Number of Interrupters (MaxIntrs)
Bits 23:19 - Reserved
Bits 31:24 - Number of Ports (MaxPorts)

Since the register value is constant for the lifetime of the controller,
it is cached in 'xhci->hcs_params1'. However, platform drivers may
override the number of interrupters through a separate variable,
'xhci->max_interrupters', leaving only the maximum slots and ports values
still derived from the cached register.

To simplify the code and improve readability, replace 'xhci->hcs_params1'
with two dedicated 'u8' fields: 'xhci->max_slots' and 'xhci->max_ports'.
These values are initialized once and used directly instead of calling
'HCS_MAX_SLOTS()' and 'HCS_MAX_PORTS()' macros.

This change reduces code clutter without increasing memory usage.

Signed-off-by: Niklas Neronin <niklas.neronin@linux.intel.com>
Signed-off-by: Mathias Nyman <mathias.nyman@linux.intel.com>
Link: https://patch.msgid.link/20251119142417.2820519-16-mathias.nyman@linux.intel.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Niklas Neronin and committed by
Greg Kroah-Hartman
df089735 70651cc3

+33 -47
+4 -11
drivers/usb/host/xhci-debugfs.c
··· 613 613 static void xhci_debugfs_create_ports(struct xhci_hcd *xhci, 614 614 struct dentry *parent) 615 615 { 616 - unsigned int num_ports; 617 616 char port_name[8]; 618 617 struct xhci_port *port; 619 618 struct dentry *dir; 620 619 621 - num_ports = HCS_MAX_PORTS(xhci->hcs_params1); 622 - 623 620 parent = debugfs_create_dir("ports", parent); 624 621 625 - while (num_ports--) { 626 - scnprintf(port_name, sizeof(port_name), "port%02d", 627 - num_ports + 1); 622 + for (int i = 0; i < xhci->max_ports; i++) { 623 + scnprintf(port_name, sizeof(port_name), "port%02d", i + 1); 628 624 dir = debugfs_create_dir(port_name, parent); 629 - port = &xhci->hw_ports[num_ports]; 625 + port = &xhci->hw_ports[i]; 630 626 debugfs_create_file("portsc", 0644, dir, port, &port_fops); 631 627 } 632 628 } ··· 630 634 static int xhci_port_bw_show(struct xhci_hcd *xhci, u8 dev_speed, 631 635 struct seq_file *s) 632 636 { 633 - unsigned int num_ports; 634 637 unsigned int i; 635 638 int ret; 636 639 struct xhci_container_ctx *ctx; ··· 639 644 ret = pm_runtime_get_sync(dev); 640 645 if (ret < 0) 641 646 return ret; 642 - 643 - num_ports = HCS_MAX_PORTS(xhci->hcs_params1); 644 647 645 648 ctx = xhci_alloc_port_bw_ctx(xhci, 0); 646 649 if (!ctx) { ··· 654 661 /* print all roothub ports available bandwidth 655 662 * refer to xhci rev1_2 protocol 6.2.6 , byte 0 is reserved 656 663 */ 657 - for (i = 1; i < num_ports+1; i++) 664 + for (i = 1; i <= xhci->max_ports; i++) 658 665 seq_printf(s, "port[%d] available bw: %d%%.\n", i, 659 666 ctx->bytes[i]); 660 667 err_out:
+1 -1
drivers/usb/host/xhci-hub.c
··· 700 700 /* Disable all Device Slots */ 701 701 xhci_dbg(xhci, "Disable all slots\n"); 702 702 spin_unlock_irqrestore(&xhci->lock, *flags); 703 - for (i = 1; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) { 703 + for (i = 1; i <= xhci->max_slots; i++) { 704 704 if (!xhci->devs[i]) 705 705 continue; 706 706
+13 -18
drivers/usb/host/xhci-mem.c
··· 951 951 /* is this a hub device that added a tt_info to the tts list */ 952 952 if (tt_info->slot_id == slot_id) { 953 953 /* are any devices using this tt_info? */ 954 - for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) { 954 + for (i = 1; i < xhci->max_slots; i++) { 955 955 vdev = xhci->devs[i]; 956 956 if (vdev && (vdev->tt_info == tt_info)) 957 957 xhci_free_virt_devices_depth_first( ··· 1899 1899 void xhci_mem_cleanup(struct xhci_hcd *xhci) 1900 1900 { 1901 1901 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 1902 - int i, j, num_ports; 1902 + int i, j; 1903 1903 1904 1904 cancel_delayed_work_sync(&xhci->cmd_timer); 1905 1905 ··· 1918 1918 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed command ring"); 1919 1919 xhci_cleanup_command_queue(xhci); 1920 1920 1921 - num_ports = HCS_MAX_PORTS(xhci->hcs_params1); 1922 - for (i = 0; i < num_ports && xhci->rh_bw; i++) { 1921 + for (i = 0; i < xhci->max_ports && xhci->rh_bw; i++) { 1923 1922 struct xhci_interval_bw_table *bwt = &xhci->rh_bw[i].bw_table; 1924 1923 for (j = 0; j < XHCI_MAX_INTERVAL; j++) { 1925 1924 struct list_head *ep = &bwt->interval_bw[j].endpoints; ··· 1927 1928 } 1928 1929 } 1929 1930 1930 - for (i = HCS_MAX_SLOTS(xhci->hcs_params1); i > 0; i--) 1931 + for (i = xhci->max_slots; i > 0; i--) 1931 1932 xhci_free_virt_devices_depth_first(xhci, i); 1932 1933 1933 1934 dma_pool_destroy(xhci->segment_pool); ··· 1963 1964 if (!xhci->rh_bw) 1964 1965 goto no_bw; 1965 1966 1966 - for (i = 0; i < num_ports; i++) { 1967 + for (i = 0; i < xhci->max_ports; i++) { 1967 1968 struct xhci_tt_bw_info *tt, *n; 1968 1969 list_for_each_entry_safe(tt, n, &xhci->rh_bw[i].tts, tt_list) { 1969 1970 list_del(&tt->tt_list); ··· 2164 2165 if (!rhub->ports) 2165 2166 return; 2166 2167 2167 - for (i = 0; i < HCS_MAX_PORTS(xhci->hcs_params1); i++) { 2168 + for (i = 0; i < xhci->max_ports; i++) { 2168 2169 if (xhci->hw_ports[i].rhub != rhub || 2169 2170 xhci->hw_ports[i].hcd_portnum == DUPLICATE_ENTRY) 2170 2171 continue; ··· 2187 2188 { 2188 2189 void __iomem *base; 2189 2190 u32 offset; 2190 - unsigned int num_ports; 2191 2191 int i, j; 2192 2192 int cap_count = 0; 2193 2193 u32 cap_start; 2194 2194 struct device *dev = xhci_to_hcd(xhci)->self.sysdev; 2195 2195 2196 - num_ports = HCS_MAX_PORTS(xhci->hcs_params1); 2197 - xhci->hw_ports = kcalloc_node(num_ports, sizeof(*xhci->hw_ports), 2198 - flags, dev_to_node(dev)); 2196 + xhci->hw_ports = kcalloc_node(xhci->max_ports, sizeof(*xhci->hw_ports), 2197 + flags, dev_to_node(dev)); 2199 2198 if (!xhci->hw_ports) 2200 2199 return -ENOMEM; 2201 2200 2202 - for (i = 0; i < num_ports; i++) { 2201 + for (i = 0; i < xhci->max_ports; i++) { 2203 2202 xhci->hw_ports[i].port_reg = &xhci->op_regs->port_regs[i]; 2204 2203 xhci->hw_ports[i].hw_portnum = i; 2205 2204 ··· 2205 2208 init_completion(&xhci->hw_ports[i].u3exit_done); 2206 2209 } 2207 2210 2208 - xhci->rh_bw = kcalloc_node(num_ports, sizeof(*xhci->rh_bw), flags, 2209 - dev_to_node(dev)); 2211 + xhci->rh_bw = kcalloc_node(xhci->max_ports, sizeof(*xhci->rh_bw), flags, dev_to_node(dev)); 2210 2212 if (!xhci->rh_bw) 2211 2213 return -ENOMEM; 2212 - for (i = 0; i < num_ports; i++) { 2214 + for (i = 0; i < xhci->max_ports; i++) { 2213 2215 struct xhci_interval_bw_table *bw_table; 2214 2216 2215 2217 INIT_LIST_HEAD(&xhci->rh_bw[i].tts); ··· 2240 2244 offset = cap_start; 2241 2245 2242 2246 while (offset) { 2243 - xhci_add_in_port(xhci, num_ports, base + offset, cap_count); 2244 - if (xhci->usb2_rhub.num_ports + xhci->usb3_rhub.num_ports == 2245 - num_ports) 2247 + xhci_add_in_port(xhci, xhci->max_ports, base + offset, cap_count); 2248 + if (xhci->usb2_rhub.num_ports + xhci->usb3_rhub.num_ports == xhci->max_ports) 2246 2249 break; 2247 2250 offset = xhci_find_next_ext_cap(base, offset, 2248 2251 XHCI_EXT_CAPS_PROTOCOL);
+1 -1
drivers/usb/host/xhci-pci.c
··· 896 896 if (!(xhci->quirks & XHCI_RESET_TO_DEFAULT)) 897 897 return 0; 898 898 899 - for (i = 0; i < HCS_MAX_PORTS(xhci->hcs_params1); i++) { 899 + for (i = 0; i < xhci->max_ports; i++) { 900 900 port = &xhci->hw_ports[i]; 901 901 portsc = xhci_portsc_readl(port); 902 902
+2 -4
drivers/usb/host/xhci-ring.c
··· 1394 1394 xhci_cleanup_command_queue(xhci); 1395 1395 1396 1396 /* return any pending urbs, remove may be waiting for them */ 1397 - for (i = 0; i <= HCS_MAX_SLOTS(xhci->hcs_params1); i++) { 1397 + for (i = 0; i <= xhci->max_slots; i++) { 1398 1398 if (!xhci->devs[i]) 1399 1399 continue; 1400 1400 for (j = 0; j < 31; j++) ··· 1994 1994 struct usb_hcd *hcd; 1995 1995 u32 port_id; 1996 1996 u32 portsc, cmd_reg; 1997 - int max_ports; 1998 1997 unsigned int hcd_portnum; 1999 1998 struct xhci_bus_state *bus_state; 2000 1999 bool bogus_port_status = false; ··· 2005 2006 "WARN: xHC returned failed port status event\n"); 2006 2007 2007 2008 port_id = GET_PORT_ID(le32_to_cpu(event->generic.field[0])); 2008 - max_ports = HCS_MAX_PORTS(xhci->hcs_params1); 2009 2009 2010 - if ((port_id <= 0) || (port_id > max_ports)) { 2010 + if ((port_id <= 0) || (port_id > xhci->max_ports)) { 2011 2011 xhci_warn(xhci, "Port change event with invalid port ID %d\n", 2012 2012 port_id); 2013 2013 return;
+10 -11
drivers/usb/host/xhci.c
··· 291 291 if (upper_32_bits(val)) 292 292 xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring); 293 293 294 - intrs = min_t(u32, HCS_MAX_INTRS(xhci->hcs_params1), 295 - ARRAY_SIZE(xhci->run_regs->ir_set)); 294 + intrs = min_t(u32, xhci->max_interrupters, ARRAY_SIZE(xhci->run_regs->ir_set)); 296 295 297 296 for (i = 0; i < intrs; i++) { 298 297 struct xhci_intr_reg __iomem *ir; ··· 483 484 static void xhci_enable_max_dev_slots(struct xhci_hcd *xhci) 484 485 { 485 486 u32 config_reg; 486 - u32 max_slots; 487 487 488 - max_slots = HCS_MAX_SLOTS(xhci->hcs_params1); 489 488 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xHC can handle at most %d device slots", 490 - max_slots); 489 + xhci->max_slots); 491 490 492 491 config_reg = readl(&xhci->op_regs->config_reg); 493 492 config_reg &= ~HCS_SLOTS_MASK; 494 - config_reg |= max_slots; 493 + config_reg |= xhci->max_slots; 495 494 496 495 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Setting Max device slots reg = 0x%x", 497 496 config_reg); ··· 4232 4235 xhci_err(xhci, "Error while assigning device slot ID: %s\n", 4233 4236 xhci_trb_comp_code_string(command->status)); 4234 4237 xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n", 4235 - HCS_MAX_SLOTS(xhci->hcs_params1)); 4238 + xhci->max_slots); 4236 4239 xhci_free_command(xhci, command); 4237 4240 return 0; 4238 4241 } ··· 5413 5416 */ 5414 5417 struct device *dev = hcd->self.sysdev; 5415 5418 int retval; 5419 + u32 hcs_params1; 5416 5420 5417 5421 /* Accept arbitrarily long scatter-gather lists */ 5418 5422 hcd->self.sg_tablesize = ~0; ··· 5439 5441 xhci->run_regs = hcd->regs + 5440 5442 (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK); 5441 5443 /* Cache read-only capability registers */ 5442 - xhci->hcs_params1 = readl(&xhci->cap_regs->hcs_params1); 5444 + hcs_params1 = readl(&xhci->cap_regs->hcs_params1); 5443 5445 xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2); 5444 5446 xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3); 5445 5447 xhci->hci_version = HC_VERSION(readl(&xhci->cap_regs->hc_capbase)); ··· 5447 5449 if (xhci->hci_version > 0x100) 5448 5450 xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2); 5449 5451 5452 + xhci->max_slots = HCS_MAX_SLOTS(hcs_params1); 5453 + xhci->max_ports = HCS_MAX_PORTS(hcs_params1); 5450 5454 /* xhci-plat or xhci-pci might have set max_interrupters already */ 5451 - if ((!xhci->max_interrupters) || 5452 - xhci->max_interrupters > HCS_MAX_INTRS(xhci->hcs_params1)) 5453 - xhci->max_interrupters = HCS_MAX_INTRS(xhci->hcs_params1); 5455 + if ((!xhci->max_interrupters) || xhci->max_interrupters > HCS_MAX_INTRS(hcs_params1)) 5456 + xhci->max_interrupters = HCS_MAX_INTRS(hcs_params1); 5454 5457 5455 5458 xhci->quirks |= quirks; 5456 5459
+2 -1
drivers/usb/host/xhci.h
··· 1500 1500 struct xhci_doorbell_array __iomem *dba; 1501 1501 1502 1502 /* Cached register copies of read-only HC data */ 1503 - __u32 hcs_params1; 1504 1503 __u32 hcs_params2; 1505 1504 __u32 hcs_params3; 1506 1505 __u32 hcc_params; ··· 1510 1511 /* packed release number */ 1511 1512 u16 hci_version; 1512 1513 u16 max_interrupters; 1514 + u8 max_slots; 1515 + u8 max_ports; 1513 1516 /* imod_interval in ns (I * 250ns) */ 1514 1517 u32 imod_interval; 1515 1518 u32 page_size;