1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell OcteonTX CPT driver
4 * Copyright (C) 2019 Marvell International Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/ctype.h>
12 #include <linux/firmware.h>
13 #include "otx_cpt_common.h"
14 #include "otx_cptpf_ucode.h"
15 #include "otx_cptpf.h"
18 /* Tar archive defines */
19 #define TAR_MAGIC "ustar"
20 #define TAR_MAGIC_LEN 6
21 #define TAR_BLOCK_LEN 512
25 /* tar header as defined in POSIX 1003.1-1990. */
48 char block
[TAR_BLOCK_LEN
];
52 struct tar_arch_info_t
{
53 struct list_head ucodes
;
54 const struct firmware
*fw
;
57 static struct otx_cpt_bitmap
get_cores_bmap(struct device
*dev
,
58 struct otx_cpt_eng_grp_info
*eng_grp
)
60 struct otx_cpt_bitmap bmap
= { {0} };
64 if (eng_grp
->g
->engs_num
> OTX_CPT_MAX_ENGINES
) {
65 dev_err(dev
, "unsupported number of engines %d on octeontx\n",
66 eng_grp
->g
->engs_num
);
70 for (i
= 0; i
< OTX_CPT_MAX_ETYPES_PER_GRP
; i
++) {
71 if (eng_grp
->engs
[i
].type
) {
72 bitmap_or(bmap
.bits
, bmap
.bits
,
73 eng_grp
->engs
[i
].bmap
,
74 eng_grp
->g
->engs_num
);
75 bmap
.size
= eng_grp
->g
->engs_num
;
81 dev_err(dev
, "No engines reserved for engine group %d\n",
86 static int is_eng_type(int val
, int eng_type
)
88 return val
& (1 << eng_type
);
91 static int dev_supports_eng_type(struct otx_cpt_eng_grps
*eng_grps
,
94 return is_eng_type(eng_grps
->eng_types_supported
, eng_type
);
97 static void set_ucode_filename(struct otx_cpt_ucode
*ucode
,
100 strlcpy(ucode
->filename
, filename
, OTX_CPT_UCODE_NAME_LENGTH
);
103 static char *get_eng_type_str(int eng_type
)
105 char *str
= "unknown";
108 case OTX_CPT_SE_TYPES
:
112 case OTX_CPT_AE_TYPES
:
119 static char *get_ucode_type_str(int ucode_type
)
121 char *str
= "unknown";
123 switch (ucode_type
) {
124 case (1 << OTX_CPT_SE_TYPES
):
128 case (1 << OTX_CPT_AE_TYPES
):
135 static int get_ucode_type(struct otx_cpt_ucode_hdr
*ucode_hdr
, int *ucode_type
)
137 char tmp_ver_str
[OTX_CPT_UCODE_VER_STR_SZ
];
141 strlcpy(tmp_ver_str
, ucode_hdr
->ver_str
, OTX_CPT_UCODE_VER_STR_SZ
);
142 for (i
= 0; i
< strlen(tmp_ver_str
); i
++)
143 tmp_ver_str
[i
] = tolower(tmp_ver_str
[i
]);
145 nn
= ucode_hdr
->ver_num
.nn
;
146 if (strnstr(tmp_ver_str
, "se-", OTX_CPT_UCODE_VER_STR_SZ
) &&
147 (nn
== OTX_CPT_SE_UC_TYPE1
|| nn
== OTX_CPT_SE_UC_TYPE2
||
148 nn
== OTX_CPT_SE_UC_TYPE3
))
149 val
|= 1 << OTX_CPT_SE_TYPES
;
150 if (strnstr(tmp_ver_str
, "ae", OTX_CPT_UCODE_VER_STR_SZ
) &&
151 nn
== OTX_CPT_AE_UC_TYPE
)
152 val
|= 1 << OTX_CPT_AE_TYPES
;
158 if (is_eng_type(val
, OTX_CPT_AE_TYPES
) &&
159 is_eng_type(val
, OTX_CPT_SE_TYPES
))
164 static int is_mem_zero(const char *ptr
, int size
)
168 for (i
= 0; i
< size
; i
++) {
175 static int cpt_set_ucode_base(struct otx_cpt_eng_grp_info
*eng_grp
, void *obj
)
177 struct otx_cpt_device
*cpt
= (struct otx_cpt_device
*) obj
;
179 struct otx_cpt_bitmap bmap
;
182 bmap
= get_cores_bmap(&cpt
->pdev
->dev
, eng_grp
);
186 if (eng_grp
->mirror
.is_ena
)
188 eng_grp
->g
->grp
[eng_grp
->mirror
.idx
].ucode
[0].align_dma
;
190 dma_addr
= eng_grp
->ucode
[0].align_dma
;
193 * Set UCODE_BASE only for the cores which are not used,
194 * other cores should have already valid UCODE_BASE set
196 for_each_set_bit(i
, bmap
.bits
, bmap
.size
)
197 if (!eng_grp
->g
->eng_ref_cnt
[i
])
198 writeq((u64
) dma_addr
, cpt
->reg_base
+
199 OTX_CPT_PF_ENGX_UCODE_BASE(i
));
203 static int cpt_detach_and_disable_cores(struct otx_cpt_eng_grp_info
*eng_grp
,
206 struct otx_cpt_device
*cpt
= (struct otx_cpt_device
*) obj
;
207 struct otx_cpt_bitmap bmap
= { {0} };
212 bmap
= get_cores_bmap(&cpt
->pdev
->dev
, eng_grp
);
216 /* Detach the cores from group */
217 reg
= readq(cpt
->reg_base
+ OTX_CPT_PF_GX_EN(eng_grp
->idx
));
218 for_each_set_bit(i
, bmap
.bits
, bmap
.size
) {
219 if (reg
& (1ull << i
)) {
220 eng_grp
->g
->eng_ref_cnt
[i
]--;
224 writeq(reg
, cpt
->reg_base
+ OTX_CPT_PF_GX_EN(eng_grp
->idx
));
226 /* Wait for cores to become idle */
229 usleep_range(10000, 20000);
233 reg
= readq(cpt
->reg_base
+ OTX_CPT_PF_EXEC_BUSY
);
234 for_each_set_bit(i
, bmap
.bits
, bmap
.size
)
235 if (reg
& (1ull << i
)) {
241 /* Disable the cores only if they are not used anymore */
242 reg
= readq(cpt
->reg_base
+ OTX_CPT_PF_EXE_CTL
);
243 for_each_set_bit(i
, bmap
.bits
, bmap
.size
)
244 if (!eng_grp
->g
->eng_ref_cnt
[i
])
246 writeq(reg
, cpt
->reg_base
+ OTX_CPT_PF_EXE_CTL
);
251 static int cpt_attach_and_enable_cores(struct otx_cpt_eng_grp_info
*eng_grp
,
254 struct otx_cpt_device
*cpt
= (struct otx_cpt_device
*) obj
;
255 struct otx_cpt_bitmap bmap
;
259 bmap
= get_cores_bmap(&cpt
->pdev
->dev
, eng_grp
);
263 /* Attach the cores to the group */
264 reg
= readq(cpt
->reg_base
+ OTX_CPT_PF_GX_EN(eng_grp
->idx
));
265 for_each_set_bit(i
, bmap
.bits
, bmap
.size
) {
266 if (!(reg
& (1ull << i
))) {
267 eng_grp
->g
->eng_ref_cnt
[i
]++;
271 writeq(reg
, cpt
->reg_base
+ OTX_CPT_PF_GX_EN(eng_grp
->idx
));
273 /* Enable the cores */
274 reg
= readq(cpt
->reg_base
+ OTX_CPT_PF_EXE_CTL
);
275 for_each_set_bit(i
, bmap
.bits
, bmap
.size
)
277 writeq(reg
, cpt
->reg_base
+ OTX_CPT_PF_EXE_CTL
);
282 static int process_tar_file(struct device
*dev
,
283 struct tar_arch_info_t
*tar_arch
, char *filename
,
284 const u8
*data
, u32 size
)
286 struct tar_ucode_info_t
*tar_info
;
287 struct otx_cpt_ucode_hdr
*ucode_hdr
;
288 int ucode_type
, ucode_size
;
291 * If size is less than microcode header size then don't report
292 * an error because it might not be microcode file, just process
293 * next file from archive
295 if (size
< sizeof(struct otx_cpt_ucode_hdr
))
298 ucode_hdr
= (struct otx_cpt_ucode_hdr
*) data
;
300 * If microcode version can't be found don't report an error
301 * because it might not be microcode file, just process next file
303 if (get_ucode_type(ucode_hdr
, &ucode_type
))
306 ucode_size
= ntohl(ucode_hdr
->code_length
) * 2;
307 if (!ucode_size
|| (size
< round_up(ucode_size
, 16) +
308 sizeof(struct otx_cpt_ucode_hdr
) + OTX_CPT_UCODE_SIGN_LEN
)) {
309 dev_err(dev
, "Ucode %s invalid size\n", filename
);
313 tar_info
= kzalloc(sizeof(struct tar_ucode_info_t
), GFP_KERNEL
);
317 tar_info
->ucode_ptr
= data
;
318 set_ucode_filename(&tar_info
->ucode
, filename
);
319 memcpy(tar_info
->ucode
.ver_str
, ucode_hdr
->ver_str
,
320 OTX_CPT_UCODE_VER_STR_SZ
);
321 tar_info
->ucode
.ver_num
= ucode_hdr
->ver_num
;
322 tar_info
->ucode
.type
= ucode_type
;
323 tar_info
->ucode
.size
= ucode_size
;
324 list_add_tail(&tar_info
->list
, &tar_arch
->ucodes
);
329 static void release_tar_archive(struct tar_arch_info_t
*tar_arch
)
331 struct tar_ucode_info_t
*curr
, *temp
;
336 list_for_each_entry_safe(curr
, temp
, &tar_arch
->ucodes
, list
) {
337 list_del(&curr
->list
);
342 release_firmware(tar_arch
->fw
);
346 static struct tar_ucode_info_t
*get_uc_from_tar_archive(
347 struct tar_arch_info_t
*tar_arch
,
350 struct tar_ucode_info_t
*curr
, *uc_found
= NULL
;
352 list_for_each_entry(curr
, &tar_arch
->ucodes
, list
) {
353 if (!is_eng_type(curr
->ucode
.type
, ucode_type
))
361 switch (ucode_type
) {
362 case OTX_CPT_AE_TYPES
:
365 case OTX_CPT_SE_TYPES
:
366 if (uc_found
->ucode
.ver_num
.nn
== OTX_CPT_SE_UC_TYPE2
||
367 (uc_found
->ucode
.ver_num
.nn
== OTX_CPT_SE_UC_TYPE3
368 && curr
->ucode
.ver_num
.nn
== OTX_CPT_SE_UC_TYPE1
))
377 static void print_tar_dbg_info(struct tar_arch_info_t
*tar_arch
,
380 struct tar_ucode_info_t
*curr
;
382 pr_debug("Tar archive filename %s\n", tar_filename
);
383 pr_debug("Tar archive pointer %p, size %ld\n", tar_arch
->fw
->data
,
385 list_for_each_entry(curr
, &tar_arch
->ucodes
, list
) {
386 pr_debug("Ucode filename %s\n", curr
->ucode
.filename
);
387 pr_debug("Ucode version string %s\n", curr
->ucode
.ver_str
);
388 pr_debug("Ucode version %d.%d.%d.%d\n",
389 curr
->ucode
.ver_num
.nn
, curr
->ucode
.ver_num
.xx
,
390 curr
->ucode
.ver_num
.yy
, curr
->ucode
.ver_num
.zz
);
391 pr_debug("Ucode type (%d) %s\n", curr
->ucode
.type
,
392 get_ucode_type_str(curr
->ucode
.type
));
393 pr_debug("Ucode size %d\n", curr
->ucode
.size
);
394 pr_debug("Ucode ptr %p\n", curr
->ucode_ptr
);
398 static struct tar_arch_info_t
*load_tar_archive(struct device
*dev
,
401 struct tar_arch_info_t
*tar_arch
= NULL
;
402 struct tar_blk_t
*tar_blk
;
403 unsigned int cur_size
;
408 tar_arch
= kzalloc(sizeof(struct tar_arch_info_t
), GFP_KERNEL
);
412 INIT_LIST_HEAD(&tar_arch
->ucodes
);
414 /* Load tar archive */
415 ret
= request_firmware(&tar_arch
->fw
, tar_filename
, dev
);
417 goto release_tar_arch
;
419 if (tar_arch
->fw
->size
< TAR_BLOCK_LEN
) {
420 dev_err(dev
, "Invalid tar archive %s\n", tar_filename
);
421 goto release_tar_arch
;
424 tar_size
= tar_arch
->fw
->size
;
425 tar_blk
= (struct tar_blk_t
*) tar_arch
->fw
->data
;
426 if (strncmp(tar_blk
->hdr
.magic
, TAR_MAGIC
, TAR_MAGIC_LEN
- 1)) {
427 dev_err(dev
, "Unsupported format of tar archive %s\n",
429 goto release_tar_arch
;
433 /* Read current file size */
434 ret
= kstrtouint(tar_blk
->hdr
.size
, 8, &cur_size
);
436 goto release_tar_arch
;
438 if (tar_offs
+ cur_size
> tar_size
||
439 tar_offs
+ 2*TAR_BLOCK_LEN
> tar_size
) {
440 dev_err(dev
, "Invalid tar archive %s\n", tar_filename
);
441 goto release_tar_arch
;
444 tar_offs
+= TAR_BLOCK_LEN
;
445 if (tar_blk
->hdr
.typeflag
== REGTYPE
||
446 tar_blk
->hdr
.typeflag
== AREGTYPE
) {
447 ret
= process_tar_file(dev
, tar_arch
,
449 &tar_arch
->fw
->data
[tar_offs
],
452 goto release_tar_arch
;
455 tar_offs
+= (cur_size
/TAR_BLOCK_LEN
) * TAR_BLOCK_LEN
;
456 if (cur_size
% TAR_BLOCK_LEN
)
457 tar_offs
+= TAR_BLOCK_LEN
;
459 /* Check for the end of the archive */
460 if (tar_offs
+ 2*TAR_BLOCK_LEN
> tar_size
) {
461 dev_err(dev
, "Invalid tar archive %s\n", tar_filename
);
462 goto release_tar_arch
;
465 if (is_mem_zero(&tar_arch
->fw
->data
[tar_offs
],
469 /* Read next block from tar archive */
470 tar_blk
= (struct tar_blk_t
*) &tar_arch
->fw
->data
[tar_offs
];
473 print_tar_dbg_info(tar_arch
, tar_filename
);
476 release_tar_archive(tar_arch
);
480 static struct otx_cpt_engs_rsvd
*find_engines_by_type(
481 struct otx_cpt_eng_grp_info
*eng_grp
,
486 for (i
= 0; i
< OTX_CPT_MAX_ETYPES_PER_GRP
; i
++) {
487 if (!eng_grp
->engs
[i
].type
)
490 if (eng_grp
->engs
[i
].type
== eng_type
)
491 return &eng_grp
->engs
[i
];
496 int otx_cpt_uc_supports_eng_type(struct otx_cpt_ucode
*ucode
, int eng_type
)
498 return is_eng_type(ucode
->type
, eng_type
);
500 EXPORT_SYMBOL_GPL(otx_cpt_uc_supports_eng_type
);
502 int otx_cpt_eng_grp_has_eng_type(struct otx_cpt_eng_grp_info
*eng_grp
,
505 struct otx_cpt_engs_rsvd
*engs
;
507 engs
= find_engines_by_type(eng_grp
, eng_type
);
509 return (engs
!= NULL
? 1 : 0);
511 EXPORT_SYMBOL_GPL(otx_cpt_eng_grp_has_eng_type
);
513 static void print_ucode_info(struct otx_cpt_eng_grp_info
*eng_grp
,
516 if (eng_grp
->mirror
.is_ena
) {
517 scnprintf(buf
, size
, "%s (shared with engine_group%d)",
518 eng_grp
->g
->grp
[eng_grp
->mirror
.idx
].ucode
[0].ver_str
,
519 eng_grp
->mirror
.idx
);
521 scnprintf(buf
, size
, "%s", eng_grp
->ucode
[0].ver_str
);
525 static void print_engs_info(struct otx_cpt_eng_grp_info
*eng_grp
,
526 char *buf
, int size
, int idx
)
528 struct otx_cpt_engs_rsvd
*mirrored_engs
= NULL
;
529 struct otx_cpt_engs_rsvd
*engs
;
533 for (i
= 0; i
< OTX_CPT_MAX_ETYPES_PER_GRP
; i
++) {
534 engs
= &eng_grp
->engs
[i
];
537 if (idx
!= -1 && idx
!= i
)
540 if (eng_grp
->mirror
.is_ena
)
541 mirrored_engs
= find_engines_by_type(
542 &eng_grp
->g
->grp
[eng_grp
->mirror
.idx
],
544 if (i
> 0 && idx
== -1) {
546 scnprintf(buf
+len
, size
-len
, ", ");
550 scnprintf(buf
+len
, size
-len
, "%d %s ", mirrored_engs
?
551 engs
->count
+ mirrored_engs
->count
: engs
->count
,
552 get_eng_type_str(engs
->type
));
555 scnprintf(buf
+len
, size
-len
,
556 "(%d shared with engine_group%d) ",
557 engs
->count
<= 0 ? engs
->count
+
558 mirrored_engs
->count
: mirrored_engs
->count
,
559 eng_grp
->mirror
.idx
);
564 static void print_ucode_dbg_info(struct otx_cpt_ucode
*ucode
)
566 pr_debug("Ucode info\n");
567 pr_debug("Ucode version string %s\n", ucode
->ver_str
);
568 pr_debug("Ucode version %d.%d.%d.%d\n", ucode
->ver_num
.nn
,
569 ucode
->ver_num
.xx
, ucode
->ver_num
.yy
, ucode
->ver_num
.zz
);
570 pr_debug("Ucode type %s\n", get_ucode_type_str(ucode
->type
));
571 pr_debug("Ucode size %d\n", ucode
->size
);
572 pr_debug("Ucode virt address %16.16llx\n", (u64
)ucode
->align_va
);
573 pr_debug("Ucode phys address %16.16llx\n", ucode
->align_dma
);
576 static void cpt_print_engines_mask(struct otx_cpt_eng_grp_info
*eng_grp
,
577 struct device
*dev
, char *buf
, int size
)
579 struct otx_cpt_bitmap bmap
;
582 bmap
= get_cores_bmap(dev
, eng_grp
);
584 scnprintf(buf
, size
, "unknown");
587 bitmap_to_arr32(mask
, bmap
.bits
, bmap
.size
);
588 scnprintf(buf
, size
, "%8.8x %8.8x", mask
[1], mask
[0]);
592 static void print_dbg_info(struct device
*dev
,
593 struct otx_cpt_eng_grps
*eng_grps
)
595 char engs_info
[2*OTX_CPT_UCODE_NAME_LENGTH
];
596 struct otx_cpt_eng_grp_info
*mirrored_grp
;
597 char engs_mask
[OTX_CPT_UCODE_NAME_LENGTH
];
598 struct otx_cpt_eng_grp_info
*grp
;
599 struct otx_cpt_engs_rsvd
*engs
;
603 pr_debug("Engine groups global info\n");
604 pr_debug("max SE %d, max AE %d\n",
605 eng_grps
->avail
.max_se_cnt
, eng_grps
->avail
.max_ae_cnt
);
606 pr_debug("free SE %d\n", eng_grps
->avail
.se_cnt
);
607 pr_debug("free AE %d\n", eng_grps
->avail
.ae_cnt
);
609 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++) {
610 grp
= &eng_grps
->grp
[i
];
611 pr_debug("engine_group%d, state %s\n", i
, grp
->is_enabled
?
612 "enabled" : "disabled");
613 if (grp
->is_enabled
) {
614 mirrored_grp
= &eng_grps
->grp
[grp
->mirror
.idx
];
615 pr_debug("Ucode0 filename %s, version %s\n",
617 mirrored_grp
->ucode
[0].filename
:
618 grp
->ucode
[0].filename
,
620 mirrored_grp
->ucode
[0].ver_str
:
621 grp
->ucode
[0].ver_str
);
624 for (j
= 0; j
< OTX_CPT_MAX_ETYPES_PER_GRP
; j
++) {
625 engs
= &grp
->engs
[j
];
627 print_engs_info(grp
, engs_info
,
628 2*OTX_CPT_UCODE_NAME_LENGTH
, j
);
629 pr_debug("Slot%d: %s\n", j
, engs_info
);
630 bitmap_to_arr32(mask
, engs
->bmap
,
632 pr_debug("Mask: %8.8x %8.8x %8.8x %8.8x\n",
633 mask
[3], mask
[2], mask
[1], mask
[0]);
635 pr_debug("Slot%d not used\n", j
);
637 if (grp
->is_enabled
) {
638 cpt_print_engines_mask(grp
, dev
, engs_mask
,
639 OTX_CPT_UCODE_NAME_LENGTH
);
640 pr_debug("Cmask: %s\n", engs_mask
);
645 static int update_engines_avail_count(struct device
*dev
,
646 struct otx_cpt_engs_available
*avail
,
647 struct otx_cpt_engs_rsvd
*engs
, int val
)
649 switch (engs
->type
) {
650 case OTX_CPT_SE_TYPES
:
651 avail
->se_cnt
+= val
;
654 case OTX_CPT_AE_TYPES
:
655 avail
->ae_cnt
+= val
;
659 dev_err(dev
, "Invalid engine type %d\n", engs
->type
);
666 static int update_engines_offset(struct device
*dev
,
667 struct otx_cpt_engs_available
*avail
,
668 struct otx_cpt_engs_rsvd
*engs
)
670 switch (engs
->type
) {
671 case OTX_CPT_SE_TYPES
:
675 case OTX_CPT_AE_TYPES
:
676 engs
->offset
= avail
->max_se_cnt
;
680 dev_err(dev
, "Invalid engine type %d\n", engs
->type
);
687 static int release_engines(struct device
*dev
, struct otx_cpt_eng_grp_info
*grp
)
691 for (i
= 0; i
< OTX_CPT_MAX_ETYPES_PER_GRP
; i
++) {
692 if (!grp
->engs
[i
].type
)
695 if (grp
->engs
[i
].count
> 0) {
696 ret
= update_engines_avail_count(dev
, &grp
->g
->avail
,
703 grp
->engs
[i
].type
= 0;
704 grp
->engs
[i
].count
= 0;
705 grp
->engs
[i
].offset
= 0;
706 grp
->engs
[i
].ucode
= NULL
;
707 bitmap_zero(grp
->engs
[i
].bmap
, grp
->g
->engs_num
);
713 static int do_reserve_engines(struct device
*dev
,
714 struct otx_cpt_eng_grp_info
*grp
,
715 struct otx_cpt_engines
*req_engs
)
717 struct otx_cpt_engs_rsvd
*engs
= NULL
;
720 for (i
= 0; i
< OTX_CPT_MAX_ETYPES_PER_GRP
; i
++) {
721 if (!grp
->engs
[i
].type
) {
722 engs
= &grp
->engs
[i
];
730 engs
->type
= req_engs
->type
;
731 engs
->count
= req_engs
->count
;
733 ret
= update_engines_offset(dev
, &grp
->g
->avail
, engs
);
737 if (engs
->count
> 0) {
738 ret
= update_engines_avail_count(dev
, &grp
->g
->avail
, engs
,
747 static int check_engines_availability(struct device
*dev
,
748 struct otx_cpt_eng_grp_info
*grp
,
749 struct otx_cpt_engines
*req_eng
)
753 switch (req_eng
->type
) {
754 case OTX_CPT_SE_TYPES
:
755 avail_cnt
= grp
->g
->avail
.se_cnt
;
758 case OTX_CPT_AE_TYPES
:
759 avail_cnt
= grp
->g
->avail
.ae_cnt
;
763 dev_err(dev
, "Invalid engine type %d\n", req_eng
->type
);
767 if (avail_cnt
< req_eng
->count
) {
769 "Error available %s engines %d < than requested %d\n",
770 get_eng_type_str(req_eng
->type
),
771 avail_cnt
, req_eng
->count
);
778 static int reserve_engines(struct device
*dev
, struct otx_cpt_eng_grp_info
*grp
,
779 struct otx_cpt_engines
*req_engs
, int req_cnt
)
783 /* Validate if a number of requested engines is available */
784 for (i
= 0; i
< req_cnt
; i
++) {
785 ret
= check_engines_availability(dev
, grp
, &req_engs
[i
]);
790 /* Reserve requested engines for this engine group */
791 for (i
= 0; i
< req_cnt
; i
++) {
792 ret
= do_reserve_engines(dev
, grp
, &req_engs
[i
]);
799 static ssize_t
eng_grp_info_show(struct device
*dev
,
800 struct device_attribute
*attr
,
803 char ucode_info
[2*OTX_CPT_UCODE_NAME_LENGTH
];
804 char engs_info
[2*OTX_CPT_UCODE_NAME_LENGTH
];
805 char engs_mask
[OTX_CPT_UCODE_NAME_LENGTH
];
806 struct otx_cpt_eng_grp_info
*eng_grp
;
809 eng_grp
= container_of(attr
, struct otx_cpt_eng_grp_info
, info_attr
);
810 mutex_lock(&eng_grp
->g
->lock
);
812 print_engs_info(eng_grp
, engs_info
, 2*OTX_CPT_UCODE_NAME_LENGTH
, -1);
813 print_ucode_info(eng_grp
, ucode_info
, 2*OTX_CPT_UCODE_NAME_LENGTH
);
814 cpt_print_engines_mask(eng_grp
, dev
, engs_mask
,
815 OTX_CPT_UCODE_NAME_LENGTH
);
816 ret
= scnprintf(buf
, PAGE_SIZE
,
817 "Microcode : %s\nEngines: %s\nEngines mask: %s\n",
818 ucode_info
, engs_info
, engs_mask
);
820 mutex_unlock(&eng_grp
->g
->lock
);
824 static int create_sysfs_eng_grps_info(struct device
*dev
,
825 struct otx_cpt_eng_grp_info
*eng_grp
)
827 eng_grp
->info_attr
.show
= eng_grp_info_show
;
828 eng_grp
->info_attr
.store
= NULL
;
829 eng_grp
->info_attr
.attr
.name
= eng_grp
->sysfs_info_name
;
830 eng_grp
->info_attr
.attr
.mode
= 0440;
831 sysfs_attr_init(&eng_grp
->info_attr
.attr
);
832 return device_create_file(dev
, &eng_grp
->info_attr
);
835 static void ucode_unload(struct device
*dev
, struct otx_cpt_ucode
*ucode
)
838 dma_free_coherent(dev
, ucode
->size
+ OTX_CPT_UCODE_ALIGNMENT
,
839 ucode
->va
, ucode
->dma
);
841 ucode
->align_va
= NULL
;
843 ucode
->align_dma
= 0;
847 memset(&ucode
->ver_str
, 0, OTX_CPT_UCODE_VER_STR_SZ
);
848 memset(&ucode
->ver_num
, 0, sizeof(struct otx_cpt_ucode_ver_num
));
849 set_ucode_filename(ucode
, "");
853 static int copy_ucode_to_dma_mem(struct device
*dev
,
854 struct otx_cpt_ucode
*ucode
,
855 const u8
*ucode_data
)
859 /* Allocate DMAable space */
860 ucode
->va
= dma_alloc_coherent(dev
, ucode
->size
+
861 OTX_CPT_UCODE_ALIGNMENT
,
862 &ucode
->dma
, GFP_KERNEL
);
864 dev_err(dev
, "Unable to allocate space for microcode\n");
867 ucode
->align_va
= PTR_ALIGN(ucode
->va
, OTX_CPT_UCODE_ALIGNMENT
);
868 ucode
->align_dma
= PTR_ALIGN(ucode
->dma
, OTX_CPT_UCODE_ALIGNMENT
);
870 memcpy((void *) ucode
->align_va
, (void *) ucode_data
+
871 sizeof(struct otx_cpt_ucode_hdr
), ucode
->size
);
873 /* Byte swap 64-bit */
874 for (i
= 0; i
< (ucode
->size
/ 8); i
++)
875 ((__be64
*)ucode
->align_va
)[i
] =
876 cpu_to_be64(((u64
*)ucode
->align_va
)[i
]);
877 /* Ucode needs 16-bit swap */
878 for (i
= 0; i
< (ucode
->size
/ 2); i
++)
879 ((__be16
*)ucode
->align_va
)[i
] =
880 cpu_to_be16(((u16
*)ucode
->align_va
)[i
]);
884 static int ucode_load(struct device
*dev
, struct otx_cpt_ucode
*ucode
,
885 const char *ucode_filename
)
887 struct otx_cpt_ucode_hdr
*ucode_hdr
;
888 const struct firmware
*fw
;
891 set_ucode_filename(ucode
, ucode_filename
);
892 ret
= request_firmware(&fw
, ucode
->filename
, dev
);
896 ucode_hdr
= (struct otx_cpt_ucode_hdr
*) fw
->data
;
897 memcpy(ucode
->ver_str
, ucode_hdr
->ver_str
, OTX_CPT_UCODE_VER_STR_SZ
);
898 ucode
->ver_num
= ucode_hdr
->ver_num
;
899 ucode
->size
= ntohl(ucode_hdr
->code_length
) * 2;
900 if (!ucode
->size
|| (fw
->size
< round_up(ucode
->size
, 16)
901 + sizeof(struct otx_cpt_ucode_hdr
) + OTX_CPT_UCODE_SIGN_LEN
)) {
902 dev_err(dev
, "Ucode %s invalid size\n", ucode_filename
);
907 ret
= get_ucode_type(ucode_hdr
, &ucode
->type
);
909 dev_err(dev
, "Microcode %s unknown type 0x%x\n",
910 ucode
->filename
, ucode
->type
);
914 ret
= copy_ucode_to_dma_mem(dev
, ucode
, fw
->data
);
918 print_ucode_dbg_info(ucode
);
920 release_firmware(fw
);
924 static int enable_eng_grp(struct otx_cpt_eng_grp_info
*eng_grp
,
929 ret
= cpt_set_ucode_base(eng_grp
, obj
);
933 ret
= cpt_attach_and_enable_cores(eng_grp
, obj
);
937 static int disable_eng_grp(struct device
*dev
,
938 struct otx_cpt_eng_grp_info
*eng_grp
,
943 ret
= cpt_detach_and_disable_cores(eng_grp
, obj
);
947 /* Unload ucode used by this engine group */
948 ucode_unload(dev
, &eng_grp
->ucode
[0]);
950 for (i
= 0; i
< OTX_CPT_MAX_ETYPES_PER_GRP
; i
++) {
951 if (!eng_grp
->engs
[i
].type
)
954 eng_grp
->engs
[i
].ucode
= &eng_grp
->ucode
[0];
957 ret
= cpt_set_ucode_base(eng_grp
, obj
);
962 static void setup_eng_grp_mirroring(struct otx_cpt_eng_grp_info
*dst_grp
,
963 struct otx_cpt_eng_grp_info
*src_grp
)
965 /* Setup fields for engine group which is mirrored */
966 src_grp
->mirror
.is_ena
= false;
967 src_grp
->mirror
.idx
= 0;
968 src_grp
->mirror
.ref_count
++;
970 /* Setup fields for mirroring engine group */
971 dst_grp
->mirror
.is_ena
= true;
972 dst_grp
->mirror
.idx
= src_grp
->idx
;
973 dst_grp
->mirror
.ref_count
= 0;
976 static void remove_eng_grp_mirroring(struct otx_cpt_eng_grp_info
*dst_grp
)
978 struct otx_cpt_eng_grp_info
*src_grp
;
980 if (!dst_grp
->mirror
.is_ena
)
983 src_grp
= &dst_grp
->g
->grp
[dst_grp
->mirror
.idx
];
985 src_grp
->mirror
.ref_count
--;
986 dst_grp
->mirror
.is_ena
= false;
987 dst_grp
->mirror
.idx
= 0;
988 dst_grp
->mirror
.ref_count
= 0;
991 static void update_requested_engs(struct otx_cpt_eng_grp_info
*mirrored_eng_grp
,
992 struct otx_cpt_engines
*engs
, int engs_cnt
)
994 struct otx_cpt_engs_rsvd
*mirrored_engs
;
997 for (i
= 0; i
< engs_cnt
; i
++) {
998 mirrored_engs
= find_engines_by_type(mirrored_eng_grp
,
1004 * If mirrored group has this type of engines attached then
1005 * there are 3 scenarios possible:
1006 * 1) mirrored_engs.count == engs[i].count then all engines
1007 * from mirrored engine group will be shared with this engine
1009 * 2) mirrored_engs.count > engs[i].count then only a subset of
1010 * engines from mirrored engine group will be shared with this
1012 * 3) mirrored_engs.count < engs[i].count then all engines
1013 * from mirrored engine group will be shared with this group
1014 * and additional engines will be reserved for exclusively use
1015 * by this engine group
1017 engs
[i
].count
-= mirrored_engs
->count
;
1021 static struct otx_cpt_eng_grp_info
*find_mirrored_eng_grp(
1022 struct otx_cpt_eng_grp_info
*grp
)
1024 struct otx_cpt_eng_grps
*eng_grps
= grp
->g
;
1027 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++) {
1028 if (!eng_grps
->grp
[i
].is_enabled
)
1030 if (eng_grps
->grp
[i
].ucode
[0].type
)
1034 if (!strncasecmp(eng_grps
->grp
[i
].ucode
[0].ver_str
,
1035 grp
->ucode
[0].ver_str
,
1036 OTX_CPT_UCODE_VER_STR_SZ
))
1037 return &eng_grps
->grp
[i
];
1043 static struct otx_cpt_eng_grp_info
*find_unused_eng_grp(
1044 struct otx_cpt_eng_grps
*eng_grps
)
1048 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++) {
1049 if (!eng_grps
->grp
[i
].is_enabled
)
1050 return &eng_grps
->grp
[i
];
1055 static int eng_grp_update_masks(struct device
*dev
,
1056 struct otx_cpt_eng_grp_info
*eng_grp
)
1058 struct otx_cpt_engs_rsvd
*engs
, *mirrored_engs
;
1059 struct otx_cpt_bitmap tmp_bmap
= { {0} };
1060 int i
, j
, cnt
, max_cnt
;
1063 for (i
= 0; i
< OTX_CPT_MAX_ETYPES_PER_GRP
; i
++) {
1064 engs
= &eng_grp
->engs
[i
];
1067 if (engs
->count
<= 0)
1070 switch (engs
->type
) {
1071 case OTX_CPT_SE_TYPES
:
1072 max_cnt
= eng_grp
->g
->avail
.max_se_cnt
;
1075 case OTX_CPT_AE_TYPES
:
1076 max_cnt
= eng_grp
->g
->avail
.max_ae_cnt
;
1080 dev_err(dev
, "Invalid engine type %d\n", engs
->type
);
1085 WARN_ON(engs
->offset
+ max_cnt
> OTX_CPT_MAX_ENGINES
);
1086 bitmap_zero(tmp_bmap
.bits
, eng_grp
->g
->engs_num
);
1087 for (j
= engs
->offset
; j
< engs
->offset
+ max_cnt
; j
++) {
1088 if (!eng_grp
->g
->eng_ref_cnt
[j
]) {
1089 bitmap_set(tmp_bmap
.bits
, j
, 1);
1099 bitmap_copy(engs
->bmap
, tmp_bmap
.bits
, eng_grp
->g
->engs_num
);
1102 if (!eng_grp
->mirror
.is_ena
)
1105 for (i
= 0; i
< OTX_CPT_MAX_ETYPES_PER_GRP
; i
++) {
1106 engs
= &eng_grp
->engs
[i
];
1110 mirrored_engs
= find_engines_by_type(
1111 &eng_grp
->g
->grp
[eng_grp
->mirror
.idx
],
1113 WARN_ON(!mirrored_engs
&& engs
->count
<= 0);
1117 bitmap_copy(tmp_bmap
.bits
, mirrored_engs
->bmap
,
1118 eng_grp
->g
->engs_num
);
1119 if (engs
->count
< 0) {
1120 bit
= find_first_bit(mirrored_engs
->bmap
,
1121 eng_grp
->g
->engs_num
);
1122 bitmap_clear(tmp_bmap
.bits
, bit
, -engs
->count
);
1124 bitmap_or(engs
->bmap
, engs
->bmap
, tmp_bmap
.bits
,
1125 eng_grp
->g
->engs_num
);
1130 static int delete_engine_group(struct device
*dev
,
1131 struct otx_cpt_eng_grp_info
*eng_grp
)
1135 if (!eng_grp
->is_enabled
)
1138 if (eng_grp
->mirror
.ref_count
) {
1139 dev_err(dev
, "Can't delete engine_group%d as it is used by engine_group(s):",
1141 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++) {
1142 if (eng_grp
->g
->grp
[i
].mirror
.is_ena
&&
1143 eng_grp
->g
->grp
[i
].mirror
.idx
== eng_grp
->idx
)
1150 /* Removing engine group mirroring if enabled */
1151 remove_eng_grp_mirroring(eng_grp
);
1153 /* Disable engine group */
1154 ret
= disable_eng_grp(dev
, eng_grp
, eng_grp
->g
->obj
);
1158 /* Release all engines held by this engine group */
1159 ret
= release_engines(dev
, eng_grp
);
1163 device_remove_file(dev
, &eng_grp
->info_attr
);
1164 eng_grp
->is_enabled
= false;
1169 static int validate_1_ucode_scenario(struct device
*dev
,
1170 struct otx_cpt_eng_grp_info
*eng_grp
,
1171 struct otx_cpt_engines
*engs
, int engs_cnt
)
1175 /* Verify that ucode loaded supports requested engine types */
1176 for (i
= 0; i
< engs_cnt
; i
++) {
1177 if (!otx_cpt_uc_supports_eng_type(&eng_grp
->ucode
[0],
1180 "Microcode %s does not support %s engines\n",
1181 eng_grp
->ucode
[0].filename
,
1182 get_eng_type_str(engs
[i
].type
));
1189 static void update_ucode_ptrs(struct otx_cpt_eng_grp_info
*eng_grp
)
1191 struct otx_cpt_ucode
*ucode
;
1193 if (eng_grp
->mirror
.is_ena
)
1194 ucode
= &eng_grp
->g
->grp
[eng_grp
->mirror
.idx
].ucode
[0];
1196 ucode
= &eng_grp
->ucode
[0];
1197 WARN_ON(!eng_grp
->engs
[0].type
);
1198 eng_grp
->engs
[0].ucode
= ucode
;
1201 static int create_engine_group(struct device
*dev
,
1202 struct otx_cpt_eng_grps
*eng_grps
,
1203 struct otx_cpt_engines
*engs
, int engs_cnt
,
1204 void *ucode_data
[], int ucodes_cnt
,
1205 bool use_uc_from_tar_arch
)
1207 struct otx_cpt_eng_grp_info
*mirrored_eng_grp
;
1208 struct tar_ucode_info_t
*tar_info
;
1209 struct otx_cpt_eng_grp_info
*eng_grp
;
1212 if (ucodes_cnt
> OTX_CPT_MAX_ETYPES_PER_GRP
)
1215 /* Validate if requested engine types are supported by this device */
1216 for (i
= 0; i
< engs_cnt
; i
++)
1217 if (!dev_supports_eng_type(eng_grps
, engs
[i
].type
)) {
1218 dev_err(dev
, "Device does not support %s engines\n",
1219 get_eng_type_str(engs
[i
].type
));
1223 /* Find engine group which is not used */
1224 eng_grp
= find_unused_eng_grp(eng_grps
);
1226 dev_err(dev
, "Error all engine groups are being used\n");
1231 for (i
= 0; i
< ucodes_cnt
; i
++) {
1232 if (use_uc_from_tar_arch
) {
1233 tar_info
= (struct tar_ucode_info_t
*) ucode_data
[i
];
1234 eng_grp
->ucode
[i
] = tar_info
->ucode
;
1235 ret
= copy_ucode_to_dma_mem(dev
, &eng_grp
->ucode
[i
],
1236 tar_info
->ucode_ptr
);
1238 ret
= ucode_load(dev
, &eng_grp
->ucode
[i
],
1239 (char *) ucode_data
[i
]);
1241 goto err_ucode_unload
;
1244 /* Validate scenario where 1 ucode is used */
1245 ret
= validate_1_ucode_scenario(dev
, eng_grp
, engs
, engs_cnt
);
1247 goto err_ucode_unload
;
1249 /* Check if this group mirrors another existing engine group */
1250 mirrored_eng_grp
= find_mirrored_eng_grp(eng_grp
);
1251 if (mirrored_eng_grp
) {
1252 /* Setup mirroring */
1253 setup_eng_grp_mirroring(eng_grp
, mirrored_eng_grp
);
1256 * Update count of requested engines because some
1257 * of them might be shared with mirrored group
1259 update_requested_engs(mirrored_eng_grp
, engs
, engs_cnt
);
1262 /* Reserve engines */
1263 ret
= reserve_engines(dev
, eng_grp
, engs
, engs_cnt
);
1265 goto err_ucode_unload
;
1267 /* Update ucode pointers used by engines */
1268 update_ucode_ptrs(eng_grp
);
1270 /* Update engine masks used by this group */
1271 ret
= eng_grp_update_masks(dev
, eng_grp
);
1273 goto err_release_engs
;
1275 /* Create sysfs entry for engine group info */
1276 ret
= create_sysfs_eng_grps_info(dev
, eng_grp
);
1278 goto err_release_engs
;
1280 /* Enable engine group */
1281 ret
= enable_eng_grp(eng_grp
, eng_grps
->obj
);
1283 goto err_release_engs
;
1286 * If this engine group mirrors another engine group
1287 * then we need to unload ucode as we will use ucode
1288 * from mirrored engine group
1290 if (eng_grp
->mirror
.is_ena
)
1291 ucode_unload(dev
, &eng_grp
->ucode
[0]);
1293 eng_grp
->is_enabled
= true;
1294 if (eng_grp
->mirror
.is_ena
)
1296 "Engine_group%d: reuse microcode %s from group %d\n",
1297 eng_grp
->idx
, mirrored_eng_grp
->ucode
[0].ver_str
,
1298 mirrored_eng_grp
->idx
);
1300 dev_info(dev
, "Engine_group%d: microcode loaded %s\n",
1301 eng_grp
->idx
, eng_grp
->ucode
[0].ver_str
);
1306 release_engines(dev
, eng_grp
);
1308 ucode_unload(dev
, &eng_grp
->ucode
[0]);
1312 static ssize_t
ucode_load_store(struct device
*dev
,
1313 struct device_attribute
*attr
,
1314 const char *buf
, size_t count
)
1316 struct otx_cpt_engines engs
[OTX_CPT_MAX_ETYPES_PER_GRP
] = { {0} };
1317 char *ucode_filename
[OTX_CPT_MAX_ETYPES_PER_GRP
];
1318 char tmp_buf
[OTX_CPT_UCODE_NAME_LENGTH
] = { 0 };
1319 char *start
, *val
, *err_msg
, *tmp
;
1320 struct otx_cpt_eng_grps
*eng_grps
;
1321 int grp_idx
= 0, ret
= -EINVAL
;
1322 bool has_se
, has_ie
, has_ae
;
1323 int del_grp_idx
= -1;
1326 if (strlen(buf
) > OTX_CPT_UCODE_NAME_LENGTH
)
1329 eng_grps
= container_of(attr
, struct otx_cpt_eng_grps
, ucode_load_attr
);
1330 err_msg
= "Invalid engine group format";
1331 strlcpy(tmp_buf
, buf
, OTX_CPT_UCODE_NAME_LENGTH
);
1334 has_se
= has_ie
= has_ae
= false;
1337 val
= strsep(&start
, ";");
1344 if (!strncasecmp(val
, "engine_group", 12)) {
1345 if (del_grp_idx
!= -1)
1347 tmp
= strim(strsep(&val
, ":"));
1350 if (strlen(tmp
) != 13)
1352 if (kstrtoint((tmp
+ 12), 10, &del_grp_idx
))
1355 if (strncasecmp(val
, "null", 4))
1357 if (strlen(val
) != 4)
1359 } else if (!strncasecmp(val
, "se", 2) && strchr(val
, ':')) {
1360 if (has_se
|| ucode_idx
)
1362 tmp
= strim(strsep(&val
, ":"));
1365 if (strlen(tmp
) != 2)
1367 if (kstrtoint(strim(val
), 10, &engs
[grp_idx
].count
))
1369 engs
[grp_idx
++].type
= OTX_CPT_SE_TYPES
;
1371 } else if (!strncasecmp(val
, "ae", 2) && strchr(val
, ':')) {
1372 if (has_ae
|| ucode_idx
)
1374 tmp
= strim(strsep(&val
, ":"));
1377 if (strlen(tmp
) != 2)
1379 if (kstrtoint(strim(val
), 10, &engs
[grp_idx
].count
))
1381 engs
[grp_idx
++].type
= OTX_CPT_AE_TYPES
;
1388 if (strnstr(val
, " ", strlen(val
)))
1390 ucode_filename
[ucode_idx
++] = val
;
1394 /* Validate input parameters */
1395 if (del_grp_idx
== -1) {
1396 if (!(grp_idx
&& ucode_idx
))
1399 if (ucode_idx
> 1 && grp_idx
< 2)
1402 if (grp_idx
> OTX_CPT_MAX_ETYPES_PER_GRP
) {
1403 err_msg
= "Error max 2 engine types can be attached";
1408 if (del_grp_idx
< 0 ||
1409 del_grp_idx
>= OTX_CPT_MAX_ENGINE_GROUPS
) {
1410 dev_err(dev
, "Invalid engine group index %d\n",
1416 if (!eng_grps
->grp
[del_grp_idx
].is_enabled
) {
1417 dev_err(dev
, "Error engine_group%d is not configured\n",
1423 if (grp_idx
|| ucode_idx
)
1427 mutex_lock(&eng_grps
->lock
);
1429 if (eng_grps
->is_rdonly
) {
1430 dev_err(dev
, "Disable VFs before modifying engine groups\n");
1435 if (del_grp_idx
== -1)
1436 /* create engine group */
1437 ret
= create_engine_group(dev
, eng_grps
, engs
, grp_idx
,
1438 (void **) ucode_filename
,
1441 /* delete engine group */
1442 ret
= delete_engine_group(dev
, &eng_grps
->grp
[del_grp_idx
]);
1446 print_dbg_info(dev
, eng_grps
);
1448 mutex_unlock(&eng_grps
->lock
);
1449 return ret
? ret
: count
;
1451 dev_err(dev
, "%s\n", err_msg
);
1456 int otx_cpt_try_create_default_eng_grps(struct pci_dev
*pdev
,
1457 struct otx_cpt_eng_grps
*eng_grps
,
1460 struct tar_ucode_info_t
*tar_info
[OTX_CPT_MAX_ETYPES_PER_GRP
] = {};
1461 struct otx_cpt_engines engs
[OTX_CPT_MAX_ETYPES_PER_GRP
] = {};
1462 struct tar_arch_info_t
*tar_arch
= NULL
;
1466 mutex_lock(&eng_grps
->lock
);
1469 * We don't create engine group for kernel crypto if attempt to create
1470 * it was already made (when user enabled VFs for the first time)
1472 if (eng_grps
->is_first_try
)
1474 eng_grps
->is_first_try
= true;
1476 /* We create group for kcrypto only if no groups are configured */
1477 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++)
1478 if (eng_grps
->grp
[i
].is_enabled
)
1484 tar_filename
= OTX_CPT_UCODE_TAR_FILE_NAME
;
1488 dev_err(&pdev
->dev
, "Unknown PF type %d\n", pf_type
);
1493 tar_arch
= load_tar_archive(&pdev
->dev
, tar_filename
);
1498 * If device supports SE engines and there is SE microcode in tar
1499 * archive try to create engine group with SE engines for kernel
1500 * crypto functionality (symmetric crypto)
1502 tar_info
[0] = get_uc_from_tar_archive(tar_arch
, OTX_CPT_SE_TYPES
);
1504 dev_supports_eng_type(eng_grps
, OTX_CPT_SE_TYPES
)) {
1506 engs
[0].type
= OTX_CPT_SE_TYPES
;
1507 engs
[0].count
= eng_grps
->avail
.max_se_cnt
;
1509 ret
= create_engine_group(&pdev
->dev
, eng_grps
, engs
, 1,
1510 (void **) tar_info
, 1, true);
1512 goto release_tar_arch
;
1515 * If device supports AE engines and there is AE microcode in tar
1516 * archive try to create engine group with AE engines for asymmetric
1517 * crypto functionality.
1519 tar_info
[0] = get_uc_from_tar_archive(tar_arch
, OTX_CPT_AE_TYPES
);
1521 dev_supports_eng_type(eng_grps
, OTX_CPT_AE_TYPES
)) {
1523 engs
[0].type
= OTX_CPT_AE_TYPES
;
1524 engs
[0].count
= eng_grps
->avail
.max_ae_cnt
;
1526 ret
= create_engine_group(&pdev
->dev
, eng_grps
, engs
, 1,
1527 (void **) tar_info
, 1, true);
1529 goto release_tar_arch
;
1532 print_dbg_info(&pdev
->dev
, eng_grps
);
1534 release_tar_archive(tar_arch
);
1536 mutex_unlock(&eng_grps
->lock
);
1540 void otx_cpt_set_eng_grps_is_rdonly(struct otx_cpt_eng_grps
*eng_grps
,
1543 mutex_lock(&eng_grps
->lock
);
1545 eng_grps
->is_rdonly
= is_rdonly
;
1547 mutex_unlock(&eng_grps
->lock
);
1550 void otx_cpt_disable_all_cores(struct otx_cpt_device
*cpt
)
1552 int grp
, timeout
= 100;
1555 /* Disengage the cores from groups */
1556 for (grp
= 0; grp
< OTX_CPT_MAX_ENGINE_GROUPS
; grp
++) {
1557 writeq(0, cpt
->reg_base
+ OTX_CPT_PF_GX_EN(grp
));
1561 reg
= readq(cpt
->reg_base
+ OTX_CPT_PF_EXEC_BUSY
);
1564 reg
= readq(cpt
->reg_base
+ OTX_CPT_PF_EXEC_BUSY
);
1566 dev_warn(&cpt
->pdev
->dev
, "Cores still busy\n");
1571 /* Disable the cores */
1572 writeq(0, cpt
->reg_base
+ OTX_CPT_PF_EXE_CTL
);
1575 void otx_cpt_cleanup_eng_grps(struct pci_dev
*pdev
,
1576 struct otx_cpt_eng_grps
*eng_grps
)
1578 struct otx_cpt_eng_grp_info
*grp
;
1581 mutex_lock(&eng_grps
->lock
);
1582 if (eng_grps
->is_ucode_load_created
) {
1583 device_remove_file(&pdev
->dev
,
1584 &eng_grps
->ucode_load_attr
);
1585 eng_grps
->is_ucode_load_created
= false;
1588 /* First delete all mirroring engine groups */
1589 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++)
1590 if (eng_grps
->grp
[i
].mirror
.is_ena
)
1591 delete_engine_group(&pdev
->dev
, &eng_grps
->grp
[i
]);
1593 /* Delete remaining engine groups */
1594 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++)
1595 delete_engine_group(&pdev
->dev
, &eng_grps
->grp
[i
]);
1597 /* Release memory */
1598 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++) {
1599 grp
= &eng_grps
->grp
[i
];
1600 for (j
= 0; j
< OTX_CPT_MAX_ETYPES_PER_GRP
; j
++) {
1601 kfree(grp
->engs
[j
].bmap
);
1602 grp
->engs
[j
].bmap
= NULL
;
1606 mutex_unlock(&eng_grps
->lock
);
1609 int otx_cpt_init_eng_grps(struct pci_dev
*pdev
,
1610 struct otx_cpt_eng_grps
*eng_grps
, int pf_type
)
1612 struct otx_cpt_eng_grp_info
*grp
;
1615 mutex_init(&eng_grps
->lock
);
1616 eng_grps
->obj
= pci_get_drvdata(pdev
);
1617 eng_grps
->avail
.se_cnt
= eng_grps
->avail
.max_se_cnt
;
1618 eng_grps
->avail
.ae_cnt
= eng_grps
->avail
.max_ae_cnt
;
1620 eng_grps
->engs_num
= eng_grps
->avail
.max_se_cnt
+
1621 eng_grps
->avail
.max_ae_cnt
;
1622 if (eng_grps
->engs_num
> OTX_CPT_MAX_ENGINES
) {
1624 "Number of engines %d > than max supported %d\n",
1625 eng_grps
->engs_num
, OTX_CPT_MAX_ENGINES
);
1630 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++) {
1631 grp
= &eng_grps
->grp
[i
];
1635 snprintf(grp
->sysfs_info_name
, OTX_CPT_UCODE_NAME_LENGTH
,
1636 "engine_group%d", i
);
1637 for (j
= 0; j
< OTX_CPT_MAX_ETYPES_PER_GRP
; j
++) {
1639 kcalloc(BITS_TO_LONGS(eng_grps
->engs_num
),
1640 sizeof(long), GFP_KERNEL
);
1641 if (!grp
->engs
[j
].bmap
) {
1650 /* OcteonTX 83XX SE CPT PF has only SE engines attached */
1651 eng_grps
->eng_types_supported
= 1 << OTX_CPT_SE_TYPES
;
1655 /* OcteonTX 83XX AE CPT PF has only AE engines attached */
1656 eng_grps
->eng_types_supported
= 1 << OTX_CPT_AE_TYPES
;
1660 dev_err(&pdev
->dev
, "Unknown PF type %d\n", pf_type
);
1665 eng_grps
->ucode_load_attr
.show
= NULL
;
1666 eng_grps
->ucode_load_attr
.store
= ucode_load_store
;
1667 eng_grps
->ucode_load_attr
.attr
.name
= "ucode_load";
1668 eng_grps
->ucode_load_attr
.attr
.mode
= 0220;
1669 sysfs_attr_init(&eng_grps
->ucode_load_attr
.attr
);
1670 ret
= device_create_file(&pdev
->dev
,
1671 &eng_grps
->ucode_load_attr
);
1674 eng_grps
->is_ucode_load_created
= true;
1676 print_dbg_info(&pdev
->dev
, eng_grps
);
1679 otx_cpt_cleanup_eng_grps(pdev
, eng_grps
);