Skip to content

Commit bd37d9b

Browse files
niklas88gregkh
authored andcommitted
s390/pci: refactor zpci_create_device()
commit ba764dd upstream. Currently zpci_create_device() is only called in clp_add_pci_device() which allocates the memory for the struct zpci_dev being created. There is little separation of concerns as only both functions together can create a zpci_dev and the only CLP specific code in clp_add_pci_device() is a call to clp_query_pci_fn(). Improve this by removing clp_add_pci_device() and refactor zpci_create_device() such that it alone creates and initializes the zpci_dev given the FID and Function Handle. For this we need to make clp_query_pci_fn() non-static. While at it remove the function handle parameter since we can just take that from the zpci_dev. Also move adding to the zpci_list to after the zdev has been fully created which eliminates a window where a partially initialized zdev can be found by get_zdev_by_fid(). Acked-by: Pierre Morel <pmorel@linux.ibm.com> Signed-off-by: Niklas Schnelle <schnelle@linux.ibm.com> Signed-off-by: Vasily Gorbik <gor@linux.ibm.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 015916c commit bd37d9b

4 files changed

Lines changed: 48 additions & 57 deletions

File tree

‎arch/s390/include/asm/pci.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ extern unsigned int s390_pci_no_rid;
201201
Prototypes
202202
----------------------------------------------------------------------------- */
203203
/* Base stuff */
204-
int zpci_create_device(struct zpci_dev *);
204+
int zpci_create_device(u32 fid, u32 fh, enum zpci_state state);
205205
void zpci_remove_device(struct zpci_dev *zdev);
206206
int zpci_enable_device(struct zpci_dev *);
207207
int zpci_disable_device(struct zpci_dev *);
@@ -212,7 +212,7 @@ void zpci_remove_reserved_devices(void);
212212
/* CLP */
213213
int clp_setup_writeback_mio(void);
214214
int clp_scan_pci_devices(void);
215-
int clp_add_pci_device(u32, u32, int);
215+
int clp_query_pci_fn(struct zpci_dev *zdev);
216216
int clp_enable_fh(struct zpci_dev *, u8);
217217
int clp_disable_fh(struct zpci_dev *);
218218
int clp_get_state(u32 fid, enum zpci_state *state);

‎arch/s390/pci/pci.c‎

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -695,43 +695,68 @@ void zpci_remove_device(struct zpci_dev *zdev)
695695
}
696696
}
697697

698-
int zpci_create_device(struct zpci_dev *zdev)
698+
/**
699+
* zpci_create_device() - Create a new zpci_dev and add it to the zbus
700+
* @fid: Function ID of the device to be created
701+
* @fh: Current Function Handle of the device to be created
702+
* @state: Initial state after creation either Standby or Configured
703+
*
704+
* Creates a new zpci device and adds it to its, possibly newly created, zbus
705+
* as well as zpci_list.
706+
*
707+
* Returns: 0 on success, an error value otherwise
708+
*/
709+
int zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
699710
{
711+
struct zpci_dev *zdev;
700712
int rc;
701713

702-
kref_init(&zdev->kref);
714+
zpci_dbg(3, "add fid:%x, fh:%x, c:%d\n", fid, fh, state);
715+
zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
716+
if (!zdev)
717+
return -ENOMEM;
703718

704-
spin_lock(&zpci_list_lock);
705-
list_add_tail(&zdev->entry, &zpci_list);
706-
spin_unlock(&zpci_list_lock);
719+
/* FID and Function Handle are the static/dynamic identifiers */
720+
zdev->fid = fid;
721+
zdev->fh = fh;
707722

708-
rc = zpci_init_iommu(zdev);
723+
/* Query function properties and update zdev */
724+
rc = clp_query_pci_fn(zdev);
709725
if (rc)
710-
goto out;
726+
goto error;
727+
zdev->state = state;
711728

729+
kref_init(&zdev->kref);
712730
mutex_init(&zdev->lock);
731+
732+
rc = zpci_init_iommu(zdev);
733+
if (rc)
734+
goto error;
735+
713736
if (zdev->state == ZPCI_FN_STATE_CONFIGURED) {
714737
rc = zpci_enable_device(zdev);
715738
if (rc)
716-
goto out_destroy_iommu;
739+
goto error_destroy_iommu;
717740
}
718741

719742
rc = zpci_bus_device_register(zdev, &pci_root_ops);
720743
if (rc)
721-
goto out_disable;
744+
goto error_disable;
745+
746+
spin_lock(&zpci_list_lock);
747+
list_add_tail(&zdev->entry, &zpci_list);
748+
spin_unlock(&zpci_list_lock);
722749

723750
return 0;
724751

725-
out_disable:
752+
error_disable:
726753
if (zdev->state == ZPCI_FN_STATE_ONLINE)
727754
zpci_disable_device(zdev);
728-
729-
out_destroy_iommu:
755+
error_destroy_iommu:
730756
zpci_destroy_iommu(zdev);
731-
out:
732-
spin_lock(&zpci_list_lock);
733-
list_del(&zdev->entry);
734-
spin_unlock(&zpci_list_lock);
757+
error:
758+
zpci_dbg(0, "add fid:%x, rc:%d\n", fid, rc);
759+
kfree(zdev);
735760
return rc;
736761
}
737762

‎arch/s390/pci/pci_clp.c‎

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static int clp_store_query_pci_fn(struct zpci_dev *zdev,
181181
return 0;
182182
}
183183

184-
static int clp_query_pci_fn(struct zpci_dev *zdev, u32 fh)
184+
int clp_query_pci_fn(struct zpci_dev *zdev)
185185
{
186186
struct clp_req_rsp_query_pci *rrb;
187187
int rc;
@@ -194,7 +194,7 @@ static int clp_query_pci_fn(struct zpci_dev *zdev, u32 fh)
194194
rrb->request.hdr.len = sizeof(rrb->request);
195195
rrb->request.hdr.cmd = CLP_QUERY_PCI_FN;
196196
rrb->response.hdr.len = sizeof(rrb->response);
197-
rrb->request.fh = fh;
197+
rrb->request.fh = zdev->fh;
198198

199199
rc = clp_req(rrb, CLP_LPS_PCI);
200200
if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
@@ -212,40 +212,6 @@ static int clp_query_pci_fn(struct zpci_dev *zdev, u32 fh)
212212
return rc;
213213
}
214214

215-
int clp_add_pci_device(u32 fid, u32 fh, int configured)
216-
{
217-
struct zpci_dev *zdev;
218-
int rc = -ENOMEM;
219-
220-
zpci_dbg(3, "add fid:%x, fh:%x, c:%d\n", fid, fh, configured);
221-
zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
222-
if (!zdev)
223-
goto error;
224-
225-
zdev->fh = fh;
226-
zdev->fid = fid;
227-
228-
/* Query function properties and update zdev */
229-
rc = clp_query_pci_fn(zdev, fh);
230-
if (rc)
231-
goto error;
232-
233-
if (configured)
234-
zdev->state = ZPCI_FN_STATE_CONFIGURED;
235-
else
236-
zdev->state = ZPCI_FN_STATE_STANDBY;
237-
238-
rc = zpci_create_device(zdev);
239-
if (rc)
240-
goto error;
241-
return 0;
242-
243-
error:
244-
zpci_dbg(0, "add fid:%x, rc:%d\n", fid, rc);
245-
kfree(zdev);
246-
return rc;
247-
}
248-
249215
static int clp_refresh_fh(u32 fid);
250216
/*
251217
* Enable/Disable a given PCI function and update its function handle if
@@ -408,7 +374,7 @@ static void __clp_add(struct clp_fh_list_entry *entry, void *data)
408374

409375
zdev = get_zdev_by_fid(entry->fid);
410376
if (!zdev)
411-
clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
377+
zpci_create_device(entry->fid, entry->fh, entry->config_state);
412378
}
413379

414380
int clp_scan_pci_devices(void)

‎arch/s390/pci/pci_event.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
8989
switch (ccdf->pec) {
9090
case 0x0301: /* Reserved|Standby -> Configured */
9191
if (!zdev) {
92-
ret = clp_add_pci_device(ccdf->fid, ccdf->fh, 1);
92+
zpci_create_device(ccdf->fid, ccdf->fh, ZPCI_FN_STATE_CONFIGURED);
9393
break;
9494
}
9595
/* the configuration request may be stale */
@@ -116,7 +116,7 @@ static void __zpci_event_availability(struct zpci_ccdf_avail *ccdf)
116116
break;
117117
case 0x0302: /* Reserved -> Standby */
118118
if (!zdev) {
119-
clp_add_pci_device(ccdf->fid, ccdf->fh, 0);
119+
zpci_create_device(ccdf->fid, ccdf->fh, ZPCI_FN_STATE_STANDBY);
120120
break;
121121
}
122122
zdev->fh = ccdf->fh;

0 commit comments

Comments
 (0)