Re: [RFC PATCH v2 12/13] ata: sata_sil24: Use flexible arrays for the command block SGEs

From: Damien Le Moal

Date: Wed Dec 02 2026 - 02:10:21 EST


On 12/2/26 12:16, Eric Curtin wrote:
> From: Ryan Roberts <ryan.roberts@xxxxxxx>
>
> SIL24_MAX_SGE is derived from PAGE_SIZE, so with boot-time page size
> selection it is not a compile-time constant and cannot size the sge[]
> arrays in struct sil24_ata_block and struct sil24_atapi_block.
>
> Make sge[] a flexible array in both and move SIL24_MAX_SGT and
> SIL24_MAX_SGE out of the enum into macros, since they are no longer
> constant. Add SIL24_*_BLOCK_SIZE macros that compute the block sizes
> with struct_size_t(), and add sil24_cmd_blk() to index the command block
> array, because sizeof(union sil24_cmd_block) is no longer the stride.
> Use SIL24_CMD_BLOCK_SIZE for the block stride in sil24_qc_issue() and
> for the allocation size in sil24_port_start().
>
> Define sil24_sht with DEFINE_GLOBAL_PAGE_SIZE_VAR_CONST() so that its
> sg_tablesize is initialised at boot.
>
> The link-time check that union sil24_cmd_block is exactly PAGE_SIZE
> cannot work for a run-time value, so replace it with WARN_ON_ONCE()
> checks in sil24_init_one() that fail the probe with -EINVAL.
>
> Signed-off-by: Ryan Roberts <ryan.roberts@xxxxxxx>
> Signed-off-by: Eric Curtin <eric.curtin@xxxxxxxxxx>
> ---
> drivers/ata/sata_sil24.c | 70 ++++++++++++++++++++++++----------------
> 1 file changed, 43 insertions(+), 27 deletions(-)
>
> diff --git a/drivers/ata/sata_sil24.c b/drivers/ata/sata_sil24.c
> index 7acd55b17b18..ec420923d866 100644
> --- a/drivers/ata/sata_sil24.c
> +++ b/drivers/ata/sata_sil24.c
> @@ -42,26 +42,25 @@ struct sil24_sge {
> __le32 flags;
> };
>
> +/*
> + * sil24 fetches in chunks of 64bytes. The first block
> + * contains the PRB and two SGEs. From the second block, it's
> + * consisted of four SGEs and called SGT. Calculate the
> + * number of SGTs that fit into one page.
> + */
> +#define SIL24_PRB_SZ (sizeof(struct sil24_prb) + 2 * sizeof(struct sil24_sge))
> +#define SIL24_MAX_SGT ((PAGE_SIZE - SIL24_PRB_SZ) / (4 * sizeof(struct sil24_sge)))
> +
> +/*
> + * This will give us one unused SGEs for ATA. This extra SGE
> + * will be used to store CDB for ATAPI devices.
> + */
> +#define SIL24_MAX_SGE (4 * SIL24_MAX_SGT + 1)
>
> enum {
> SIL24_HOST_BAR = 0,
> SIL24_PORT_BAR = 2,
>
> - /* sil24 fetches in chunks of 64bytes. The first block
> - * contains the PRB and two SGEs. From the second block, it's
> - * consisted of four SGEs and called SGT. Calculate the
> - * number of SGTs that fit into one page.
> - */
> - SIL24_PRB_SZ = sizeof(struct sil24_prb)
> - + 2 * sizeof(struct sil24_sge),
> - SIL24_MAX_SGT = (PAGE_SIZE - SIL24_PRB_SZ)
> - / (4 * sizeof(struct sil24_sge)),
> -
> - /* This will give us one unused SGEs for ATA. This extra SGE
> - * will be used to store CDB for ATAPI devices.
> - */
> - SIL24_MAX_SGE = 4 * SIL24_MAX_SGT + 1,
> -
> /*
> * Global controller registers (128 bytes @ BAR0)
> */
> @@ -244,13 +243,13 @@ enum {
>
> struct sil24_ata_block {
> struct sil24_prb prb;
> - struct sil24_sge sge[SIL24_MAX_SGE];
> + struct sil24_sge sge[];

This needs a __counted_by() annotation.

> };
>
> struct sil24_atapi_block {
> struct sil24_prb prb;
> u8 cdb[16];
> - struct sil24_sge sge[SIL24_MAX_SGE];
> + struct sil24_sge sge[];

Here too.

> };
>
> union sil24_cmd_block {
> @@ -258,6 +257,10 @@ union sil24_cmd_block {
> struct sil24_atapi_block atapi;
> };
>
> +#define SIL24_ATA_BLOCK_SIZE struct_size_t(struct sil24_ata_block, sge, SIL24_MAX_SGE)
> +#define SIL24_ATAPI_BLOCK_SIZE struct_size_t(struct sil24_atapi_block, sge, SIL24_MAX_SGE)
> +#define SIL24_CMD_BLOCK_SIZE max(SIL24_ATA_BLOCK_SIZE, SIL24_ATAPI_BLOCK_SIZE)
> +
> static const struct sil24_cerr_info {
> unsigned int err_mask, action;
> const char *desc;
> @@ -322,6 +325,13 @@ struct sil24_port_priv {
> int do_port_rst;
> };
>
> +/* sizeof(union sil24_cmd_block) is not the stride between blocks */
> +static inline union sil24_cmd_block *
> +sil24_cmd_blk(struct sil24_port_priv *pp, unsigned int tag)
> +{
> + return (void *)((u8 *)pp->cmd_block + tag * SIL24_CMD_BLOCK_SIZE);
> +}
> +
> static void sil24_dev_config(struct ata_device *dev);
> static int sil24_scr_read(struct ata_link *link, unsigned sc_reg, u32 *val);
> static int sil24_scr_write(struct ata_link *link, unsigned sc_reg, u32 val);
> @@ -372,7 +382,7 @@ static struct pci_driver sil24_pci_driver = {
> #endif
> };
>
> -static const struct scsi_host_template sil24_sht = {
> +static DEFINE_GLOBAL_PAGE_SIZE_VAR_CONST(struct scsi_host_template, sil24_sht, {

I am not familiar with this and a grep in 7.4-rc5 gives nothing about it. What
is this? Is it something new in next?

> __ATA_BASE_SHT(DRV_NAME),
> .can_queue = SIL24_MAX_CMDS,
> .sg_tablesize = SIL24_MAX_SGE,
> @@ -380,7 +390,7 @@ static const struct scsi_host_template sil24_sht = {
> .sdev_groups = ata_ncq_sdev_groups,
> .change_queue_depth = ata_scsi_change_queue_depth,
> .sdev_configure = ata_scsi_sdev_configure
> -};
> +});
>
> static struct ata_port_operations sil24_ops = {
> .inherits = &sata_pmp_port_ops,
> @@ -599,7 +609,7 @@ static int sil24_exec_polled_cmd(struct ata_port *ap, int pmp,
> {
> void __iomem *port = sil24_port_base(ap);
> struct sil24_port_priv *pp = ap->private_data;
> - struct sil24_prb *prb = &pp->cmd_block[0].ata.prb;
> + struct sil24_prb *prb = &sil24_cmd_blk(pp, 0)->ata.prb;
> dma_addr_t paddr = pp->cmd_block_dma;
> u32 irq_enabled, irq_mask, irq_stat;
> int rc;
> @@ -841,7 +851,7 @@ static enum ata_completion_errors sil24_qc_prep(struct ata_queued_cmd *qc)
> struct sil24_sge *sge;
> u16 ctrl = 0;
>
> - cb = &pp->cmd_block[sil24_tag(qc->hw_tag)];
> + cb = sil24_cmd_blk(pp, sil24_tag(qc->hw_tag));
>
> if (!ata_is_atapi(qc->tf.protocol)) {
> prb = &cb->ata.prb;
> @@ -889,7 +899,7 @@ static unsigned int sil24_qc_issue(struct ata_queued_cmd *qc)
> dma_addr_t paddr;
> void __iomem *activate;
>
> - paddr = pp->cmd_block_dma + tag * sizeof(*pp->cmd_block);
> + paddr = pp->cmd_block_dma + tag * SIL24_CMD_BLOCK_SIZE;
> activate = port + PORT_CMD_ACTIVATE + tag * 8;
>
> /*
> @@ -1196,7 +1206,7 @@ static int sil24_port_start(struct ata_port *ap)
> struct device *dev = ap->host->dev;
> struct sil24_port_priv *pp;
> union sil24_cmd_block *cb;
> - size_t cb_size = sizeof(*cb) * SIL24_MAX_CMDS;
> + size_t cb_size = SIL24_CMD_BLOCK_SIZE * SIL24_MAX_CMDS;
> dma_addr_t cb_dma;
>
> pp = devm_kzalloc_obj(dev, *pp);
> @@ -1261,7 +1271,6 @@ static void sil24_init_controller(struct ata_host *host)
>
> static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
> {
> - extern int __MARKER__sil24_cmd_block_is_sized_wrongly;
> struct ata_port_info pi = sil24_port_info[ent->driver_data];
> const struct ata_port_info *ppi[] = { &pi, NULL };
> void __iomem * const *iomap;
> @@ -1269,9 +1278,16 @@ static int sil24_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
> int rc;
> u32 tmp;
>
> - /* cause link error if sil24_cmd_block is sized wrongly */
> - if (sizeof(union sil24_cmd_block) != PAGE_SIZE)
> - __MARKER__sil24_cmd_block_is_sized_wrongly = 1;
> + /*
> + * union sil24_cmd_block must be PAGE_SIZE once taking into account the
> + * 'sge' flexible array members in struct sil24_atapi_block and struct
> + * sil24_ata_block.
> + */
> + if (WARN_ON_ONCE(SIL24_ATAPI_BLOCK_SIZE != PAGE_SIZE))
> + return -EINVAL;
> + if (WARN_ON_ONCE(SIL24_ATA_BLOCK_SIZE !=
> + PAGE_SIZE - sizeof_field(struct sil24_atapi_block, cdb)))
> + return -EINVAL;
>
> ata_print_version_once(&pdev->dev, DRV_VERSION);
>


--
Damien Le Moal
Western Digital Research