Skip to content

Commit 0b5e43b

Browse files
yekai123123herbertx
authored andcommitted
crypto: hisilicon/sec2 - Add new create qp process
Combine found device and created qp into one operation instead of found device and create qp both are independent operations. when execute multiple tasks, the different threads may find same device at the same time, but the number of queues is insufficient on the device. causing one of threads fail to create a qp. Now fix this, First find device then create qp, if result failure. the current thread will find next device. Signed-off-by: Kai Ye <[email protected]> Signed-off-by: Shukun Tan <[email protected]> Reviewed-by: Zhou Wang <[email protected]> Reviewed-by: Zaibo Xu <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 0f4146f commit 0b5e43b

File tree

3 files changed

+42
-61
lines changed

3 files changed

+42
-61
lines changed

drivers/crypto/hisilicon/sec2/sec.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ struct sec_ctx {
119119
struct sec_qp_ctx *qp_ctx;
120120
struct sec_dev *sec;
121121
const struct sec_req_op *req_op;
122+
struct hisi_qp **qps;
122123

123124
/* Half queues for encipher, and half for decipher */
124125
u32 hlf_q_num;
@@ -168,15 +169,15 @@ struct sec_debug {
168169

169170
struct sec_dev {
170171
struct hisi_qm qm;
171-
struct list_head list;
172172
struct sec_debug debug;
173173
u32 ctx_q_num;
174174
bool iommu_used;
175175
u32 num_vfs;
176176
unsigned long status;
177177
};
178178

179-
struct sec_dev *sec_find_device(int node);
179+
void sec_destroy_qps(struct hisi_qp **qps, int qp_num);
180+
struct hisi_qp **sec_create_qps(void);
180181
int sec_register_to_crypto(void);
181182
void sec_unregister_from_crypto(void);
182183
#endif

drivers/crypto/hisilicon/sec2/sec_crypto.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,8 @@ static int sec_create_qp_ctx(struct hisi_qm *qm, struct sec_ctx *ctx,
355355
struct hisi_qp *qp;
356356
int ret = -ENOMEM;
357357

358-
qp = hisi_qm_create_qp(qm, alg_type);
359-
if (IS_ERR(qp))
360-
return PTR_ERR(qp);
361-
362358
qp_ctx = &ctx->qp_ctx[qp_ctx_id];
359+
qp = ctx->qps[qp_ctx_id];
363360
qp->req_type = 0;
364361
qp->qp_ctx = qp_ctx;
365362
qp->req_cb = sec_req_cb;
@@ -402,7 +399,6 @@ static int sec_create_qp_ctx(struct hisi_qm *qm, struct sec_ctx *ctx,
402399
hisi_acc_free_sgl_pool(dev, qp_ctx->c_in_pool);
403400
err_destroy_idr:
404401
idr_destroy(&qp_ctx->req_idr);
405-
hisi_qm_release_qp(qp);
406402

407403
return ret;
408404
}
@@ -419,19 +415,20 @@ static void sec_release_qp_ctx(struct sec_ctx *ctx,
419415
hisi_acc_free_sgl_pool(dev, qp_ctx->c_in_pool);
420416

421417
idr_destroy(&qp_ctx->req_idr);
422-
hisi_qm_release_qp(qp_ctx->qp);
423418
}
424419

425420
static int sec_ctx_base_init(struct sec_ctx *ctx)
426421
{
427422
struct sec_dev *sec;
428423
int i, ret;
429424

430-
sec = sec_find_device(cpu_to_node(smp_processor_id()));
431-
if (!sec) {
432-
pr_err("Can not find proper Hisilicon SEC device!\n");
425+
ctx->qps = sec_create_qps();
426+
if (!ctx->qps) {
427+
pr_err("Can not create sec qps!\n");
433428
return -ENODEV;
434429
}
430+
431+
sec = container_of(ctx->qps[0]->qm, struct sec_dev, qm);
435432
ctx->sec = sec;
436433
ctx->hlf_q_num = sec->ctx_q_num >> 1;
437434

@@ -455,6 +452,7 @@ static int sec_ctx_base_init(struct sec_ctx *ctx)
455452
for (i = i - 1; i >= 0; i--)
456453
sec_release_qp_ctx(ctx, &ctx->qp_ctx[i]);
457454

455+
sec_destroy_qps(ctx->qps, sec->ctx_q_num);
458456
kfree(ctx->qp_ctx);
459457
return ret;
460458
}
@@ -466,6 +464,7 @@ static void sec_ctx_base_uninit(struct sec_ctx *ctx)
466464
for (i = 0; i < ctx->sec->ctx_q_num; i++)
467465
sec_release_qp_ctx(ctx, &ctx->qp_ctx[i]);
468466

467+
sec_destroy_qps(ctx->qps, ctx->sec->ctx_q_num);
469468
kfree(ctx->qp_ctx);
470469
}
471470

drivers/crypto/hisilicon/sec2/sec_main.c

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ struct sec_hw_error {
9090

9191
static const char sec_name[] = "hisi_sec2";
9292
static struct dentry *sec_debugfs_root;
93-
static LIST_HEAD(sec_list);
94-
static DEFINE_MUTEX(sec_list_lock);
93+
static struct hisi_qm_list sec_devices;
9594

9695
static const struct sec_hw_error sec_hw_errors[] = {
9796
{.int_msk = BIT(0), .msg = "sec_axi_rresp_err_rint"},
@@ -106,37 +105,6 @@ static const struct sec_hw_error sec_hw_errors[] = {
106105
{ /* sentinel */ }
107106
};
108107

109-
struct sec_dev *sec_find_device(int node)
110-
{
111-
#define SEC_NUMA_MAX_DISTANCE 100
112-
int min_distance = SEC_NUMA_MAX_DISTANCE;
113-
int dev_node = 0, free_qp_num = 0;
114-
struct sec_dev *sec, *ret = NULL;
115-
struct hisi_qm *qm;
116-
struct device *dev;
117-
118-
mutex_lock(&sec_list_lock);
119-
list_for_each_entry(sec, &sec_list, list) {
120-
qm = &sec->qm;
121-
dev = &qm->pdev->dev;
122-
#ifdef CONFIG_NUMA
123-
dev_node = dev->numa_node;
124-
if (dev_node < 0)
125-
dev_node = 0;
126-
#endif
127-
if (node_distance(dev_node, node) < min_distance) {
128-
free_qp_num = hisi_qm_get_free_qp_num(qm);
129-
if (free_qp_num >= sec->ctx_q_num) {
130-
ret = sec;
131-
min_distance = node_distance(dev_node, node);
132-
}
133-
}
134-
}
135-
mutex_unlock(&sec_list_lock);
136-
137-
return ret;
138-
}
139-
140108
static const char * const sec_dbg_file_name[] = {
141109
[SEC_CURRENT_QM] = "current_qm",
142110
[SEC_CLEAR_ENABLE] = "clear_enable",
@@ -239,27 +207,39 @@ static u32 ctx_q_num = SEC_CTX_Q_NUM_DEF;
239207
module_param_cb(ctx_q_num, &sec_ctx_q_num_ops, &ctx_q_num, 0444);
240208
MODULE_PARM_DESC(ctx_q_num, "Queue num in ctx (24 default, 2, 4, ..., 32)");
241209

210+
void sec_destroy_qps(struct hisi_qp **qps, int qp_num)
211+
{
212+
hisi_qm_free_qps(qps, qp_num);
213+
kfree(qps);
214+
}
215+
216+
struct hisi_qp **sec_create_qps(void)
217+
{
218+
int node = cpu_to_node(smp_processor_id());
219+
u32 ctx_num = ctx_q_num;
220+
struct hisi_qp **qps;
221+
int ret;
222+
223+
qps = kcalloc(ctx_num, sizeof(struct hisi_qp *), GFP_KERNEL);
224+
if (!qps)
225+
return NULL;
226+
227+
ret = hisi_qm_alloc_qps_node(&sec_devices, ctx_num, 0, node, qps);
228+
if (!ret)
229+
return qps;
230+
231+
kfree(qps);
232+
return NULL;
233+
}
234+
235+
242236
static const struct pci_device_id sec_dev_ids[] = {
243237
{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, SEC_PF_PCI_DEVICE_ID) },
244238
{ PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, SEC_VF_PCI_DEVICE_ID) },
245239
{ 0, }
246240
};
247241
MODULE_DEVICE_TABLE(pci, sec_dev_ids);
248242

249-
static inline void sec_add_to_list(struct sec_dev *sec)
250-
{
251-
mutex_lock(&sec_list_lock);
252-
list_add_tail(&sec->list, &sec_list);
253-
mutex_unlock(&sec_list_lock);
254-
}
255-
256-
static inline void sec_remove_from_list(struct sec_dev *sec)
257-
{
258-
mutex_lock(&sec_list_lock);
259-
list_del(&sec->list);
260-
mutex_unlock(&sec_list_lock);
261-
}
262-
263243
static u8 sec_get_endian(struct sec_dev *sec)
264244
{
265245
struct hisi_qm *qm = &sec->qm;
@@ -889,7 +869,7 @@ static int sec_probe(struct pci_dev *pdev, const struct pci_device_id *id)
889869
if (ret)
890870
pci_warn(pdev, "Failed to init debugfs!\n");
891871

892-
sec_add_to_list(sec);
872+
hisi_qm_add_to_list(qm, &sec_devices);
893873

894874
ret = sec_register_to_crypto();
895875
if (ret < 0) {
@@ -900,7 +880,7 @@ static int sec_probe(struct pci_dev *pdev, const struct pci_device_id *id)
900880
return 0;
901881

902882
err_remove_from_list:
903-
sec_remove_from_list(sec);
883+
hisi_qm_del_from_list(qm, &sec_devices);
904884
sec_debugfs_exit(sec);
905885
hisi_qm_stop(qm);
906886

@@ -1024,7 +1004,7 @@ static void sec_remove(struct pci_dev *pdev)
10241004

10251005
sec_unregister_from_crypto();
10261006

1027-
sec_remove_from_list(sec);
1007+
hisi_qm_del_from_list(qm, &sec_devices);
10281008

10291009
if (qm->fun_type == QM_HW_PF && sec->num_vfs)
10301010
(void)sec_sriov_disable(pdev);
@@ -1071,6 +1051,7 @@ static int __init sec_init(void)
10711051
{
10721052
int ret;
10731053

1054+
hisi_qm_init_list(&sec_devices);
10741055
sec_register_debugfs();
10751056

10761057
ret = pci_register_driver(&sec_pci_driver);

0 commit comments

Comments
 (0)