Skip to content

Commit d5d2861

Browse files
nehebfloatious
authored andcommitted
ata: libahci_platform: use flex array for platform PHYs
Modify struct ahci_host_priv to use a flexible array member for an adapter port PHYs and use struct_size to combine the allocation of this array together with the adapter private data structure. __counted_by() annotation is added for the phys field to support runtime analysis. Signed-off-by: Rosen Penev <rosenp@gmail.com> Reviewed-by: Damien Le Moal <dlemoal@kernel.org> Signed-off-by: Niklas Cassel <cassel@kernel.org>
1 parent 78ec069 commit d5d2861

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

drivers/ata/ahci.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ struct ahci_host_priv {
357357
* If platform uses PHYs. There is a 1:1 relation between the port number and
358358
* the PHY position in this array.
359359
*/
360-
struct phy **phys;
361360
unsigned nports; /* Number of ports */
362361
void *plat_data; /* Other platform data */
363362
unsigned int irq; /* interrupt line */
@@ -379,6 +378,8 @@ struct ahci_host_priv {
379378
/* only required for per-port MSI(-X) support */
380379
int (*get_irq_vector)(struct ata_host *host,
381380
int port);
381+
382+
struct phy *phys[] __counted_by(nports);
382383
};
383384

384385
/*

drivers/ata/libahci_platform.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -482,15 +482,29 @@ struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev,
482482
struct ahci_host_priv *hpriv;
483483
u32 mask_port_map = 0;
484484
u32 max_port;
485+
int nports;
485486

486487
if (!devres_open_group(dev, NULL, GFP_KERNEL))
487488
return ERR_PTR(-ENOMEM);
488489

489-
hpriv = devres_alloc(ahci_platform_put_resources, sizeof(*hpriv),
490+
/* find maximum port id for allocating structures */
491+
max_port = ahci_platform_find_max_port_id(dev);
492+
/*
493+
* Set nports according to maximum port id. Clamp at
494+
* AHCI_MAX_PORTS, warning message for invalid port id
495+
* is generated later.
496+
* When DT has no sub-nodes max_port is 0, nports is 1,
497+
* in order to be able to use the
498+
* ahci_platform_[en|dis]able_[phys|regulators] functions.
499+
*/
500+
nports = min(AHCI_MAX_PORTS, max_port + 1);
501+
hpriv = devres_alloc(ahci_platform_put_resources, struct_size(hpriv, phys, nports),
490502
GFP_KERNEL);
491503
if (!hpriv)
492504
goto err_out;
493505

506+
hpriv->nports = nports;
507+
494508
devres_add(dev, hpriv);
495509

496510
/*
@@ -573,23 +587,6 @@ struct ahci_host_priv *ahci_platform_get_resources(struct platform_device *pdev,
573587
goto err_out;
574588
}
575589

576-
/* find maximum port id for allocating structures */
577-
max_port = ahci_platform_find_max_port_id(dev);
578-
/*
579-
* Set nports according to maximum port id. Clamp at
580-
* AHCI_MAX_PORTS, warning message for invalid port id
581-
* is generated later.
582-
* When DT has no sub-nodes max_port is 0, nports is 1,
583-
* in order to be able to use the
584-
* ahci_platform_[en|dis]able_[phys|regulators] functions.
585-
*/
586-
hpriv->nports = min(AHCI_MAX_PORTS, max_port + 1);
587-
588-
hpriv->phys = devm_kcalloc(dev, hpriv->nports, sizeof(*hpriv->phys), GFP_KERNEL);
589-
if (!hpriv->phys) {
590-
rc = -ENOMEM;
591-
goto err_out;
592-
}
593590
/*
594591
* We cannot use devm_ here, since ahci_platform_put_resources() uses
595592
* target_pwrs after devm_ have freed memory

0 commit comments

Comments
 (0)