]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
PCI/IDE: Initialize an ID for all IDE streams
authorDan Williams <dan.j.williams@intel.com>
Thu, 13 Nov 2025 02:14:43 +0000 (18:14 -0800)
committerDan Williams <dan.j.williams@intel.com>
Fri, 14 Nov 2025 23:06:57 +0000 (15:06 -0800)
The PCIe spec defines two types of streams - selective and link.  Each
stream has an ID from the same bucket so a stream ID does not tell the
type.  The spec defines an "enable" bit for every stream and required
stream IDs to be unique among all enabled stream but there is no such
requirement for disabled streams.

However, when IDE_KM is programming keys, an IDE-capable device needs
to know the type of stream being programmed to write it directly to
the hardware as keys are relatively large, possibly many of them and
devices often struggle with keeping around rather big data not being
used.

Walk through all streams on a device and initialise the IDs to some
unique number, both link and selective.

The weakest part of this proposal is the host bridge ide_stream_ids_ida.
Technically, a Stream ID only needs to be unique within a given partner
pair. However, with "anonymous" / unassigned streams there is no convenient
place to track the available ids. Proceed with an ida in the host bridge
for now, but consider moving this tracking to be an ide_stream_ids_ida per
device.

Co-developed-by: Alexey Kardashevskiy <aik@amd.com>
Signed-off-by: Alexey Kardashevskiy <aik@amd.com>
Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
Link: https://patch.msgid.link/20251113021446.436830-6-dan.j.williams@intel.com
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
drivers/pci/ide.c
drivers/pci/pci.h
drivers/pci/remove.c
include/linux/pci-ide.h
include/linux/pci.h

index 2e0856a4307be39486e7511360ec8d3893d8e082..f0ef474e1a0da7cba43422a259c5551639a56fb6 100644 (file)
@@ -35,8 +35,50 @@ static int sel_ide_offset(struct pci_dev *pdev,
                                settings->stream_index, pdev->nr_ide_mem);
 }
 
+static bool reserve_stream_index(struct pci_dev *pdev, u8 idx)
+{
+       int ret;
+
+       ret = ida_alloc_range(&pdev->ide_stream_ida, idx, idx, GFP_KERNEL);
+       return ret >= 0;
+}
+
+static bool reserve_stream_id(struct pci_host_bridge *hb, u8 id)
+{
+       int ret;
+
+       ret = ida_alloc_range(&hb->ide_stream_ids_ida, id, id, GFP_KERNEL);
+       return ret >= 0;
+}
+
+static bool claim_stream(struct pci_host_bridge *hb, u8 stream_id,
+                        struct pci_dev *pdev, u8 stream_idx)
+{
+       dev_info(&hb->dev, "Stream ID %d active at init\n", stream_id);
+       if (!reserve_stream_id(hb, stream_id)) {
+               dev_info(&hb->dev, "Failed to claim %s Stream ID %d\n",
+                        stream_id == PCI_IDE_RESERVED_STREAM_ID ? "reserved" :
+                                                                  "active",
+                        stream_id);
+               return false;
+       }
+
+       /* No stream index to reserve in the Link IDE case */
+       if (!pdev)
+               return true;
+
+       if (!reserve_stream_index(pdev, stream_idx)) {
+               pci_info(pdev, "Failed to claim active Selective Stream %d\n",
+                        stream_idx);
+               return false;
+       }
+
+       return true;
+}
+
 void pci_ide_init(struct pci_dev *pdev)
 {
+       struct pci_host_bridge *hb = pci_find_host_bridge(pdev->bus);
        u16 nr_link_ide, nr_ide_mem, nr_streams;
        u16 ide_cap;
        u32 val;
@@ -83,6 +125,7 @@ void pci_ide_init(struct pci_dev *pdev)
                int pos = __sel_ide_offset(ide_cap, nr_link_ide, i, nr_ide_mem);
                int nr_assoc;
                u32 val;
+               u8 id;
 
                pci_read_config_dword(pdev, pos + PCI_IDE_SEL_CAP, &val);
 
@@ -98,6 +141,51 @@ void pci_ide_init(struct pci_dev *pdev)
                }
 
                nr_ide_mem = nr_assoc;
+
+               /*
+                * Claim Stream IDs and Selective Stream blocks that are already
+                * active on the device
+                */
+               pci_read_config_dword(pdev, pos + PCI_IDE_SEL_CTL, &val);
+               id = FIELD_GET(PCI_IDE_SEL_CTL_ID, val);
+               if ((val & PCI_IDE_SEL_CTL_EN) &&
+                   !claim_stream(hb, id, pdev, i))
+                       return;
+       }
+
+       /* Reserve link stream-ids that are already active on the device */
+       for (u16 i = 0; i < nr_link_ide; ++i) {
+               int pos = ide_cap + PCI_IDE_LINK_STREAM_0 + i * PCI_IDE_LINK_BLOCK_SIZE;
+               u8 id;
+
+               pci_read_config_dword(pdev, pos + PCI_IDE_LINK_CTL_0, &val);
+               id = FIELD_GET(PCI_IDE_LINK_CTL_ID, val);
+               if ((val & PCI_IDE_LINK_CTL_EN) &&
+                   !claim_stream(hb, id, NULL, -1))
+                       return;
+       }
+
+       for (u16 i = 0; i < nr_streams; i++) {
+               int pos = __sel_ide_offset(ide_cap, nr_link_ide, i, nr_ide_mem);
+
+               pci_read_config_dword(pdev, pos + PCI_IDE_SEL_CAP, &val);
+               if (val & PCI_IDE_SEL_CTL_EN)
+                       continue;
+               val &= ~PCI_IDE_SEL_CTL_ID;
+               val |= FIELD_PREP(PCI_IDE_SEL_CTL_ID, PCI_IDE_RESERVED_STREAM_ID);
+               pci_write_config_dword(pdev, pos + PCI_IDE_SEL_CTL, val);
+       }
+
+       for (u16 i = 0; i < nr_link_ide; ++i) {
+               int pos = ide_cap + PCI_IDE_LINK_STREAM_0 +
+                         i * PCI_IDE_LINK_BLOCK_SIZE;
+
+               pci_read_config_dword(pdev, pos, &val);
+               if (val & PCI_IDE_LINK_CTL_EN)
+                       continue;
+               val &= ~PCI_IDE_LINK_CTL_ID;
+               val |= FIELD_PREP(PCI_IDE_LINK_CTL_ID, PCI_IDE_RESERVED_STREAM_ID);
+               pci_write_config_dword(pdev, pos, val);
        }
 
        pdev->ide_cap = ide_cap;
@@ -301,6 +389,28 @@ void pci_ide_stream_release(struct pci_ide *ide)
 }
 EXPORT_SYMBOL_GPL(pci_ide_stream_release);
 
+struct pci_ide_stream_id {
+       struct pci_host_bridge *hb;
+       u8 stream_id;
+};
+
+static struct pci_ide_stream_id *
+request_stream_id(struct pci_host_bridge *hb, u8 stream_id,
+                 struct pci_ide_stream_id *sid)
+{
+       if (!reserve_stream_id(hb, stream_id))
+               return NULL;
+
+       *sid = (struct pci_ide_stream_id) {
+               .hb = hb,
+               .stream_id = stream_id,
+       };
+
+       return sid;
+}
+DEFINE_FREE(free_stream_id, struct pci_ide_stream_id *,
+           if (_T) ida_free(&_T->hb->ide_stream_ids_ida, _T->stream_id))
+
 /**
  * pci_ide_stream_register() - Prepare to activate an IDE Stream
  * @ide: IDE settings descriptor
@@ -313,6 +423,7 @@ int pci_ide_stream_register(struct pci_ide *ide)
 {
        struct pci_dev *pdev = ide->pdev;
        struct pci_host_bridge *hb = pci_find_host_bridge(pdev->bus);
+       struct pci_ide_stream_id __sid;
        u8 ep_stream, rp_stream;
        int rc;
 
@@ -321,6 +432,13 @@ int pci_ide_stream_register(struct pci_ide *ide)
                return -ENXIO;
        }
 
+       struct pci_ide_stream_id *sid __free(free_stream_id) =
+               request_stream_id(hb, ide->stream_id, &__sid);
+       if (!sid) {
+               pci_err(pdev, "Setup fail: Stream ID %d in use\n", ide->stream_id);
+               return -EBUSY;
+       }
+
        ep_stream = ide->partner[PCI_IDE_EP].stream_index;
        rp_stream = ide->partner[PCI_IDE_RP].stream_index;
        const char *name __free(kfree) = kasprintf(GFP_KERNEL, "stream%d.%d.%d",
@@ -335,6 +453,9 @@ int pci_ide_stream_register(struct pci_ide *ide)
 
        ide->name = no_free_ptr(name);
 
+       /* Stream ID reservation recorded in @ide is now successfully registered */
+       retain_and_null_ptr(sid);
+
        return 0;
 }
 EXPORT_SYMBOL_GPL(pci_ide_stream_register);
@@ -353,6 +474,7 @@ void pci_ide_stream_unregister(struct pci_ide *ide)
 
        sysfs_remove_link(&hb->dev.kobj, ide->name);
        kfree(ide->name);
+       ida_free(&hb->ide_stream_ids_ida, ide->stream_id);
        ide->name = NULL;
 }
 EXPORT_SYMBOL_GPL(pci_ide_stream_unregister);
@@ -616,6 +738,8 @@ void pci_ide_init_host_bridge(struct pci_host_bridge *hb)
 {
        hb->nr_ide_streams = 256;
        ida_init(&hb->ide_stream_ida);
+       ida_init(&hb->ide_stream_ids_ida);
+       reserve_stream_id(hb, PCI_IDE_RESERVED_STREAM_ID);
 }
 
 static ssize_t available_secure_streams_show(struct device *dev,
@@ -684,3 +808,8 @@ void pci_ide_set_nr_streams(struct pci_host_bridge *hb, u16 nr)
        sysfs_update_group(&hb->dev.kobj, &pci_ide_attr_group);
 }
 EXPORT_SYMBOL_NS_GPL(pci_ide_set_nr_streams, "PCI_IDE");
+
+void pci_ide_destroy(struct pci_dev *pdev)
+{
+       ida_destroy(&pdev->ide_stream_ida);
+}
index f6ffe5ee4717863deece68212f2223459ee8c9e9..641c0b53c4e3592336262af9be2e8920cb48efee 100644 (file)
@@ -616,10 +616,12 @@ static inline void pci_doe_sysfs_teardown(struct pci_dev *pdev) { }
 #ifdef CONFIG_PCI_IDE
 void pci_ide_init(struct pci_dev *dev);
 void pci_ide_init_host_bridge(struct pci_host_bridge *hb);
+void pci_ide_destroy(struct pci_dev *dev);
 extern const struct attribute_group pci_ide_attr_group;
 #else
 static inline void pci_ide_init(struct pci_dev *dev) { }
 static inline void pci_ide_init_host_bridge(struct pci_host_bridge *hb) { }
+static inline void pci_ide_destroy(struct pci_dev *dev) { }
 #endif
 
 #ifdef CONFIG_PCI_TSM
index 803391892c4a121b84ecfe8253c5a5374fbade98..417a9ea59117d940a43039a4a83dd12348a14a3f 100644 (file)
@@ -70,6 +70,7 @@ static void pci_destroy_dev(struct pci_dev *dev)
        up_write(&pci_bus_sem);
 
        pci_doe_destroy(dev);
+       pci_ide_destroy(dev);
        pcie_aspm_exit_link_state(dev);
        pci_bridge_d3_update(dev);
        pci_pwrctrl_unregister(&dev->dev);
index 93194338e4d08ca37155d9bf96dcc7b988b88389..37a1ad9501b05d6638a6cf8a218bf8d31f6da53e 100644 (file)
@@ -97,6 +97,12 @@ struct pci_ide {
        struct tsm_dev *tsm_dev;
 };
 
+/*
+ * Some devices need help with aliased stream-ids even for idle streams. Use
+ * this id as the "never enabled" place holder.
+ */
+#define PCI_IDE_RESERVED_STREAM_ID 255
+
 void pci_ide_set_nr_streams(struct pci_host_bridge *hb, u16 nr);
 struct pci_ide_partner *pci_ide_to_settings(struct pci_dev *pdev,
                                            struct pci_ide *ide);
index ba39ca78b38213f128491e2b4863c72fc0f156d6..52a235c6102356b6059257b4c34119035b424781 100644 (file)
@@ -619,6 +619,7 @@ struct pci_host_bridge {
 #ifdef CONFIG_PCI_IDE
        u16 nr_ide_streams; /* Max streams possibly active in @ide_stream_ida */
        struct ida ide_stream_ida;
+       struct ida ide_stream_ids_ida; /* track unique ids per domain */
 #endif
        u8 (*swizzle_irq)(struct pci_dev *, u8 *); /* Platform IRQ swizzler */
        int (*map_irq)(const struct pci_dev *, u8, u8);