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",
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",
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", 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", tar_filename
);
383 pr_debug("Tar archive pointer %p, size %ld", tar_arch
->fw
->data
,
385 list_for_each_entry(curr
, &tar_arch
->ucodes
, list
) {
386 pr_debug("Ucode filename %s", curr
->ucode
.filename
);
387 pr_debug("Ucode version string %s", curr
->ucode
.ver_str
);
388 pr_debug("Ucode version %d.%d.%d.%d",
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", curr
->ucode
.type
,
392 get_ucode_type_str(curr
->ucode
.type
));
393 pr_debug("Ucode size %d", 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 ", 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",
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 ", 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 ", 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");
567 pr_debug("Ucode version string %s", ucode
->ver_str
);
568 pr_debug("Ucode version %d.%d.%d.%d", ucode
->ver_num
.nn
,
569 ucode
->ver_num
.xx
, ucode
->ver_num
.yy
, ucode
->ver_num
.zz
);
570 pr_debug("Ucode type %s", get_ucode_type_str(ucode
->type
));
571 pr_debug("Ucode size %d", ucode
->size
);
572 pr_debug("Ucode virt address %16.16llx", (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");
604 pr_debug("max SE %d, max AE %d",
605 eng_grps
->avail
.max_se_cnt
, eng_grps
->avail
.max_ae_cnt
);
606 pr_debug("free SE %d", eng_grps
->avail
.se_cnt
);
607 pr_debug("free AE %d", 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", 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",
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", j
, engs_info
);
630 bitmap_to_arr32(mask
, engs
->bmap
,
632 pr_debug("Mask: %8.8x %8.8x %8.8x %8.8x",
633 mask
[3], mask
[2], mask
[1], mask
[0]);
635 pr_debug("Slot%d not used", 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", 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",
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
)
829 eng_grp
->info_attr
.show
= eng_grp_info_show
;
830 eng_grp
->info_attr
.store
= NULL
;
831 eng_grp
->info_attr
.attr
.name
= eng_grp
->sysfs_info_name
;
832 eng_grp
->info_attr
.attr
.mode
= 0440;
833 sysfs_attr_init(&eng_grp
->info_attr
.attr
);
834 ret
= device_create_file(dev
, &eng_grp
->info_attr
);
841 static void ucode_unload(struct device
*dev
, struct otx_cpt_ucode
*ucode
)
844 dma_free_coherent(dev
, ucode
->size
+ OTX_CPT_UCODE_ALIGNMENT
,
845 ucode
->va
, ucode
->dma
);
847 ucode
->align_va
= NULL
;
849 ucode
->align_dma
= 0;
853 memset(&ucode
->ver_str
, 0, OTX_CPT_UCODE_VER_STR_SZ
);
854 memset(&ucode
->ver_num
, 0, sizeof(struct otx_cpt_ucode_ver_num
));
855 set_ucode_filename(ucode
, "");
859 static int copy_ucode_to_dma_mem(struct device
*dev
,
860 struct otx_cpt_ucode
*ucode
,
861 const u8
*ucode_data
)
865 /* Allocate DMAable space */
866 ucode
->va
= dma_alloc_coherent(dev
, ucode
->size
+
867 OTX_CPT_UCODE_ALIGNMENT
,
868 &ucode
->dma
, GFP_KERNEL
);
870 dev_err(dev
, "Unable to allocate space for microcode");
873 ucode
->align_va
= PTR_ALIGN(ucode
->va
, OTX_CPT_UCODE_ALIGNMENT
);
874 ucode
->align_dma
= PTR_ALIGN(ucode
->dma
, OTX_CPT_UCODE_ALIGNMENT
);
876 memcpy((void *) ucode
->align_va
, (void *) ucode_data
+
877 sizeof(struct otx_cpt_ucode_hdr
), ucode
->size
);
879 /* Byte swap 64-bit */
880 for (i
= 0; i
< (ucode
->size
/ 8); i
++)
881 ((u64
*)ucode
->align_va
)[i
] =
882 cpu_to_be64(((u64
*)ucode
->align_va
)[i
]);
883 /* Ucode needs 16-bit swap */
884 for (i
= 0; i
< (ucode
->size
/ 2); i
++)
885 ((u16
*)ucode
->align_va
)[i
] =
886 cpu_to_be16(((u16
*)ucode
->align_va
)[i
]);
890 static int ucode_load(struct device
*dev
, struct otx_cpt_ucode
*ucode
,
891 const char *ucode_filename
)
893 struct otx_cpt_ucode_hdr
*ucode_hdr
;
894 const struct firmware
*fw
;
897 set_ucode_filename(ucode
, ucode_filename
);
898 ret
= request_firmware(&fw
, ucode
->filename
, dev
);
902 ucode_hdr
= (struct otx_cpt_ucode_hdr
*) fw
->data
;
903 memcpy(ucode
->ver_str
, ucode_hdr
->ver_str
, OTX_CPT_UCODE_VER_STR_SZ
);
904 ucode
->ver_num
= ucode_hdr
->ver_num
;
905 ucode
->size
= ntohl(ucode_hdr
->code_length
) * 2;
906 if (!ucode
->size
|| (fw
->size
< round_up(ucode
->size
, 16)
907 + sizeof(struct otx_cpt_ucode_hdr
) + OTX_CPT_UCODE_SIGN_LEN
)) {
908 dev_err(dev
, "Ucode %s invalid size", ucode_filename
);
913 ret
= get_ucode_type(ucode_hdr
, &ucode
->type
);
915 dev_err(dev
, "Microcode %s unknown type 0x%x", ucode
->filename
,
920 ret
= copy_ucode_to_dma_mem(dev
, ucode
, fw
->data
);
924 print_ucode_dbg_info(ucode
);
926 release_firmware(fw
);
930 static int enable_eng_grp(struct otx_cpt_eng_grp_info
*eng_grp
,
935 ret
= cpt_set_ucode_base(eng_grp
, obj
);
939 ret
= cpt_attach_and_enable_cores(eng_grp
, obj
);
943 static int disable_eng_grp(struct device
*dev
,
944 struct otx_cpt_eng_grp_info
*eng_grp
,
949 ret
= cpt_detach_and_disable_cores(eng_grp
, obj
);
953 /* Unload ucode used by this engine group */
954 ucode_unload(dev
, &eng_grp
->ucode
[0]);
956 for (i
= 0; i
< OTX_CPT_MAX_ETYPES_PER_GRP
; i
++) {
957 if (!eng_grp
->engs
[i
].type
)
960 eng_grp
->engs
[i
].ucode
= &eng_grp
->ucode
[0];
963 ret
= cpt_set_ucode_base(eng_grp
, obj
);
968 static void setup_eng_grp_mirroring(struct otx_cpt_eng_grp_info
*dst_grp
,
969 struct otx_cpt_eng_grp_info
*src_grp
)
971 /* Setup fields for engine group which is mirrored */
972 src_grp
->mirror
.is_ena
= false;
973 src_grp
->mirror
.idx
= 0;
974 src_grp
->mirror
.ref_count
++;
976 /* Setup fields for mirroring engine group */
977 dst_grp
->mirror
.is_ena
= true;
978 dst_grp
->mirror
.idx
= src_grp
->idx
;
979 dst_grp
->mirror
.ref_count
= 0;
982 static void remove_eng_grp_mirroring(struct otx_cpt_eng_grp_info
*dst_grp
)
984 struct otx_cpt_eng_grp_info
*src_grp
;
986 if (!dst_grp
->mirror
.is_ena
)
989 src_grp
= &dst_grp
->g
->grp
[dst_grp
->mirror
.idx
];
991 src_grp
->mirror
.ref_count
--;
992 dst_grp
->mirror
.is_ena
= false;
993 dst_grp
->mirror
.idx
= 0;
994 dst_grp
->mirror
.ref_count
= 0;
997 static void update_requested_engs(struct otx_cpt_eng_grp_info
*mirrored_eng_grp
,
998 struct otx_cpt_engines
*engs
, int engs_cnt
)
1000 struct otx_cpt_engs_rsvd
*mirrored_engs
;
1003 for (i
= 0; i
< engs_cnt
; i
++) {
1004 mirrored_engs
= find_engines_by_type(mirrored_eng_grp
,
1010 * If mirrored group has this type of engines attached then
1011 * there are 3 scenarios possible:
1012 * 1) mirrored_engs.count == engs[i].count then all engines
1013 * from mirrored engine group will be shared with this engine
1015 * 2) mirrored_engs.count > engs[i].count then only a subset of
1016 * engines from mirrored engine group will be shared with this
1018 * 3) mirrored_engs.count < engs[i].count then all engines
1019 * from mirrored engine group will be shared with this group
1020 * and additional engines will be reserved for exclusively use
1021 * by this engine group
1023 engs
[i
].count
-= mirrored_engs
->count
;
1027 static struct otx_cpt_eng_grp_info
*find_mirrored_eng_grp(
1028 struct otx_cpt_eng_grp_info
*grp
)
1030 struct otx_cpt_eng_grps
*eng_grps
= grp
->g
;
1033 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++) {
1034 if (!eng_grps
->grp
[i
].is_enabled
)
1036 if (eng_grps
->grp
[i
].ucode
[0].type
)
1040 if (!strncasecmp(eng_grps
->grp
[i
].ucode
[0].ver_str
,
1041 grp
->ucode
[0].ver_str
,
1042 OTX_CPT_UCODE_VER_STR_SZ
))
1043 return &eng_grps
->grp
[i
];
1049 static struct otx_cpt_eng_grp_info
*find_unused_eng_grp(
1050 struct otx_cpt_eng_grps
*eng_grps
)
1054 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++) {
1055 if (!eng_grps
->grp
[i
].is_enabled
)
1056 return &eng_grps
->grp
[i
];
1061 static int eng_grp_update_masks(struct device
*dev
,
1062 struct otx_cpt_eng_grp_info
*eng_grp
)
1064 struct otx_cpt_engs_rsvd
*engs
, *mirrored_engs
;
1065 struct otx_cpt_bitmap tmp_bmap
= { {0} };
1066 int i
, j
, cnt
, max_cnt
;
1069 for (i
= 0; i
< OTX_CPT_MAX_ETYPES_PER_GRP
; i
++) {
1070 engs
= &eng_grp
->engs
[i
];
1073 if (engs
->count
<= 0)
1076 switch (engs
->type
) {
1077 case OTX_CPT_SE_TYPES
:
1078 max_cnt
= eng_grp
->g
->avail
.max_se_cnt
;
1081 case OTX_CPT_AE_TYPES
:
1082 max_cnt
= eng_grp
->g
->avail
.max_ae_cnt
;
1086 dev_err(dev
, "Invalid engine type %d", engs
->type
);
1091 WARN_ON(engs
->offset
+ max_cnt
> OTX_CPT_MAX_ENGINES
);
1092 bitmap_zero(tmp_bmap
.bits
, eng_grp
->g
->engs_num
);
1093 for (j
= engs
->offset
; j
< engs
->offset
+ max_cnt
; j
++) {
1094 if (!eng_grp
->g
->eng_ref_cnt
[j
]) {
1095 bitmap_set(tmp_bmap
.bits
, j
, 1);
1105 bitmap_copy(engs
->bmap
, tmp_bmap
.bits
, eng_grp
->g
->engs_num
);
1108 if (!eng_grp
->mirror
.is_ena
)
1111 for (i
= 0; i
< OTX_CPT_MAX_ETYPES_PER_GRP
; i
++) {
1112 engs
= &eng_grp
->engs
[i
];
1116 mirrored_engs
= find_engines_by_type(
1117 &eng_grp
->g
->grp
[eng_grp
->mirror
.idx
],
1119 WARN_ON(!mirrored_engs
&& engs
->count
<= 0);
1123 bitmap_copy(tmp_bmap
.bits
, mirrored_engs
->bmap
,
1124 eng_grp
->g
->engs_num
);
1125 if (engs
->count
< 0) {
1126 bit
= find_first_bit(mirrored_engs
->bmap
,
1127 eng_grp
->g
->engs_num
);
1128 bitmap_clear(tmp_bmap
.bits
, bit
, -engs
->count
);
1130 bitmap_or(engs
->bmap
, engs
->bmap
, tmp_bmap
.bits
,
1131 eng_grp
->g
->engs_num
);
1136 static int delete_engine_group(struct device
*dev
,
1137 struct otx_cpt_eng_grp_info
*eng_grp
)
1141 if (!eng_grp
->is_enabled
)
1144 if (eng_grp
->mirror
.ref_count
) {
1145 dev_err(dev
, "Can't delete engine_group%d as it is used by:",
1147 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++) {
1148 if (eng_grp
->g
->grp
[i
].mirror
.is_ena
&&
1149 eng_grp
->g
->grp
[i
].mirror
.idx
== eng_grp
->idx
)
1150 dev_err(dev
, "engine_group%d", i
);
1155 /* Removing engine group mirroring if enabled */
1156 remove_eng_grp_mirroring(eng_grp
);
1158 /* Disable engine group */
1159 ret
= disable_eng_grp(dev
, eng_grp
, eng_grp
->g
->obj
);
1163 /* Release all engines held by this engine group */
1164 ret
= release_engines(dev
, eng_grp
);
1168 device_remove_file(dev
, &eng_grp
->info_attr
);
1169 eng_grp
->is_enabled
= false;
1174 static int validate_1_ucode_scenario(struct device
*dev
,
1175 struct otx_cpt_eng_grp_info
*eng_grp
,
1176 struct otx_cpt_engines
*engs
, int engs_cnt
)
1180 /* Verify that ucode loaded supports requested engine types */
1181 for (i
= 0; i
< engs_cnt
; i
++) {
1182 if (!otx_cpt_uc_supports_eng_type(&eng_grp
->ucode
[0],
1185 "Microcode %s does not support %s engines",
1186 eng_grp
->ucode
[0].filename
,
1187 get_eng_type_str(engs
[i
].type
));
1194 static void update_ucode_ptrs(struct otx_cpt_eng_grp_info
*eng_grp
)
1196 struct otx_cpt_ucode
*ucode
;
1198 if (eng_grp
->mirror
.is_ena
)
1199 ucode
= &eng_grp
->g
->grp
[eng_grp
->mirror
.idx
].ucode
[0];
1201 ucode
= &eng_grp
->ucode
[0];
1202 WARN_ON(!eng_grp
->engs
[0].type
);
1203 eng_grp
->engs
[0].ucode
= ucode
;
1206 static int create_engine_group(struct device
*dev
,
1207 struct otx_cpt_eng_grps
*eng_grps
,
1208 struct otx_cpt_engines
*engs
, int engs_cnt
,
1209 void *ucode_data
[], int ucodes_cnt
,
1210 bool use_uc_from_tar_arch
)
1212 struct otx_cpt_eng_grp_info
*mirrored_eng_grp
;
1213 struct tar_ucode_info_t
*tar_info
;
1214 struct otx_cpt_eng_grp_info
*eng_grp
;
1217 if (ucodes_cnt
> OTX_CPT_MAX_ETYPES_PER_GRP
)
1220 /* Validate if requested engine types are supported by this device */
1221 for (i
= 0; i
< engs_cnt
; i
++)
1222 if (!dev_supports_eng_type(eng_grps
, engs
[i
].type
)) {
1223 dev_err(dev
, "Device does not support %s engines",
1224 get_eng_type_str(engs
[i
].type
));
1228 /* Find engine group which is not used */
1229 eng_grp
= find_unused_eng_grp(eng_grps
);
1231 dev_err(dev
, "Error all engine groups are being used");
1236 for (i
= 0; i
< ucodes_cnt
; i
++) {
1237 if (use_uc_from_tar_arch
) {
1238 tar_info
= (struct tar_ucode_info_t
*) ucode_data
[i
];
1239 eng_grp
->ucode
[i
] = tar_info
->ucode
;
1240 ret
= copy_ucode_to_dma_mem(dev
, &eng_grp
->ucode
[i
],
1241 tar_info
->ucode_ptr
);
1243 ret
= ucode_load(dev
, &eng_grp
->ucode
[i
],
1244 (char *) ucode_data
[i
]);
1246 goto err_ucode_unload
;
1249 /* Validate scenario where 1 ucode is used */
1250 ret
= validate_1_ucode_scenario(dev
, eng_grp
, engs
, engs_cnt
);
1252 goto err_ucode_unload
;
1254 /* Check if this group mirrors another existing engine group */
1255 mirrored_eng_grp
= find_mirrored_eng_grp(eng_grp
);
1256 if (mirrored_eng_grp
) {
1257 /* Setup mirroring */
1258 setup_eng_grp_mirroring(eng_grp
, mirrored_eng_grp
);
1261 * Update count of requested engines because some
1262 * of them might be shared with mirrored group
1264 update_requested_engs(mirrored_eng_grp
, engs
, engs_cnt
);
1267 /* Reserve engines */
1268 ret
= reserve_engines(dev
, eng_grp
, engs
, engs_cnt
);
1270 goto err_ucode_unload
;
1272 /* Update ucode pointers used by engines */
1273 update_ucode_ptrs(eng_grp
);
1275 /* Update engine masks used by this group */
1276 ret
= eng_grp_update_masks(dev
, eng_grp
);
1278 goto err_release_engs
;
1280 /* Create sysfs entry for engine group info */
1281 ret
= create_sysfs_eng_grps_info(dev
, eng_grp
);
1283 goto err_release_engs
;
1285 /* Enable engine group */
1286 ret
= enable_eng_grp(eng_grp
, eng_grps
->obj
);
1288 goto err_release_engs
;
1291 * If this engine group mirrors another engine group
1292 * then we need to unload ucode as we will use ucode
1293 * from mirrored engine group
1295 if (eng_grp
->mirror
.is_ena
)
1296 ucode_unload(dev
, &eng_grp
->ucode
[0]);
1298 eng_grp
->is_enabled
= true;
1299 if (eng_grp
->mirror
.is_ena
)
1301 "Engine_group%d: reuse microcode %s from group %d",
1302 eng_grp
->idx
, mirrored_eng_grp
->ucode
[0].ver_str
,
1303 mirrored_eng_grp
->idx
);
1305 dev_info(dev
, "Engine_group%d: microcode loaded %s",
1306 eng_grp
->idx
, eng_grp
->ucode
[0].ver_str
);
1311 release_engines(dev
, eng_grp
);
1313 ucode_unload(dev
, &eng_grp
->ucode
[0]);
1317 static ssize_t
ucode_load_store(struct device
*dev
,
1318 struct device_attribute
*attr
,
1319 const char *buf
, size_t count
)
1321 struct otx_cpt_engines engs
[OTX_CPT_MAX_ETYPES_PER_GRP
] = { {0} };
1322 char *ucode_filename
[OTX_CPT_MAX_ETYPES_PER_GRP
];
1323 char tmp_buf
[OTX_CPT_UCODE_NAME_LENGTH
] = { 0 };
1324 char *start
, *val
, *err_msg
, *tmp
;
1325 struct otx_cpt_eng_grps
*eng_grps
;
1326 int grp_idx
= 0, ret
= -EINVAL
;
1327 bool has_se
, has_ie
, has_ae
;
1328 int del_grp_idx
= -1;
1331 if (strlen(buf
) > OTX_CPT_UCODE_NAME_LENGTH
)
1334 eng_grps
= container_of(attr
, struct otx_cpt_eng_grps
, ucode_load_attr
);
1335 err_msg
= "Invalid engine group format";
1336 strlcpy(tmp_buf
, buf
, OTX_CPT_UCODE_NAME_LENGTH
);
1339 has_se
= has_ie
= has_ae
= false;
1342 val
= strsep(&start
, ";");
1349 if (!strncasecmp(val
, "engine_group", 12)) {
1350 if (del_grp_idx
!= -1)
1352 tmp
= strim(strsep(&val
, ":"));
1355 if (strlen(tmp
) != 13)
1357 if (kstrtoint((tmp
+ 12), 10, &del_grp_idx
))
1360 if (strncasecmp(val
, "null", 4))
1362 if (strlen(val
) != 4)
1364 } else if (!strncasecmp(val
, "se", 2) && strchr(val
, ':')) {
1365 if (has_se
|| ucode_idx
)
1367 tmp
= strim(strsep(&val
, ":"));
1370 if (strlen(tmp
) != 2)
1372 if (kstrtoint(strim(val
), 10, &engs
[grp_idx
].count
))
1374 engs
[grp_idx
++].type
= OTX_CPT_SE_TYPES
;
1376 } else if (!strncasecmp(val
, "ae", 2) && strchr(val
, ':')) {
1377 if (has_ae
|| ucode_idx
)
1379 tmp
= strim(strsep(&val
, ":"));
1382 if (strlen(tmp
) != 2)
1384 if (kstrtoint(strim(val
), 10, &engs
[grp_idx
].count
))
1386 engs
[grp_idx
++].type
= OTX_CPT_AE_TYPES
;
1393 if (strnstr(val
, " ", strlen(val
)))
1395 ucode_filename
[ucode_idx
++] = val
;
1399 /* Validate input parameters */
1400 if (del_grp_idx
== -1) {
1401 if (!(grp_idx
&& ucode_idx
))
1404 if (ucode_idx
> 1 && grp_idx
< 2)
1407 if (grp_idx
> OTX_CPT_MAX_ETYPES_PER_GRP
) {
1408 err_msg
= "Error max 2 engine types can be attached";
1413 if (del_grp_idx
< 0 ||
1414 del_grp_idx
>= OTX_CPT_MAX_ENGINE_GROUPS
) {
1415 dev_err(dev
, "Invalid engine group index %d",
1421 if (!eng_grps
->grp
[del_grp_idx
].is_enabled
) {
1422 dev_err(dev
, "Error engine_group%d is not configured",
1428 if (grp_idx
|| ucode_idx
)
1432 mutex_lock(&eng_grps
->lock
);
1434 if (eng_grps
->is_rdonly
) {
1435 dev_err(dev
, "Disable VFs before modifying engine groups\n");
1440 if (del_grp_idx
== -1)
1441 /* create engine group */
1442 ret
= create_engine_group(dev
, eng_grps
, engs
, grp_idx
,
1443 (void **) ucode_filename
,
1446 /* delete engine group */
1447 ret
= delete_engine_group(dev
, &eng_grps
->grp
[del_grp_idx
]);
1451 print_dbg_info(dev
, eng_grps
);
1453 mutex_unlock(&eng_grps
->lock
);
1454 return ret
? ret
: count
;
1456 dev_err(dev
, "%s\n", err_msg
);
1461 int otx_cpt_try_create_default_eng_grps(struct pci_dev
*pdev
,
1462 struct otx_cpt_eng_grps
*eng_grps
,
1465 struct tar_ucode_info_t
*tar_info
[OTX_CPT_MAX_ETYPES_PER_GRP
] = { 0 };
1466 struct otx_cpt_engines engs
[OTX_CPT_MAX_ETYPES_PER_GRP
] = { {0} };
1467 struct tar_arch_info_t
*tar_arch
= NULL
;
1471 mutex_lock(&eng_grps
->lock
);
1474 * We don't create engine group for kernel crypto if attempt to create
1475 * it was already made (when user enabled VFs for the first time)
1477 if (eng_grps
->is_first_try
)
1479 eng_grps
->is_first_try
= true;
1481 /* We create group for kcrypto only if no groups are configured */
1482 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++)
1483 if (eng_grps
->grp
[i
].is_enabled
)
1489 tar_filename
= OTX_CPT_UCODE_TAR_FILE_NAME
;
1493 dev_err(&pdev
->dev
, "Unknown PF type %d\n", pf_type
);
1498 tar_arch
= load_tar_archive(&pdev
->dev
, tar_filename
);
1503 * If device supports SE engines and there is SE microcode in tar
1504 * archive try to create engine group with SE engines for kernel
1505 * crypto functionality (symmetric crypto)
1507 tar_info
[0] = get_uc_from_tar_archive(tar_arch
, OTX_CPT_SE_TYPES
);
1509 dev_supports_eng_type(eng_grps
, OTX_CPT_SE_TYPES
)) {
1511 engs
[0].type
= OTX_CPT_SE_TYPES
;
1512 engs
[0].count
= eng_grps
->avail
.max_se_cnt
;
1514 ret
= create_engine_group(&pdev
->dev
, eng_grps
, engs
, 1,
1515 (void **) tar_info
, 1, true);
1517 goto release_tar_arch
;
1520 * If device supports AE engines and there is AE microcode in tar
1521 * archive try to create engine group with AE engines for asymmetric
1522 * crypto functionality.
1524 tar_info
[0] = get_uc_from_tar_archive(tar_arch
, OTX_CPT_AE_TYPES
);
1526 dev_supports_eng_type(eng_grps
, OTX_CPT_AE_TYPES
)) {
1528 engs
[0].type
= OTX_CPT_AE_TYPES
;
1529 engs
[0].count
= eng_grps
->avail
.max_ae_cnt
;
1531 ret
= create_engine_group(&pdev
->dev
, eng_grps
, engs
, 1,
1532 (void **) tar_info
, 1, true);
1534 goto release_tar_arch
;
1537 print_dbg_info(&pdev
->dev
, eng_grps
);
1539 release_tar_archive(tar_arch
);
1541 mutex_unlock(&eng_grps
->lock
);
1545 void otx_cpt_set_eng_grps_is_rdonly(struct otx_cpt_eng_grps
*eng_grps
,
1548 mutex_lock(&eng_grps
->lock
);
1550 eng_grps
->is_rdonly
= is_rdonly
;
1552 mutex_unlock(&eng_grps
->lock
);
1555 void otx_cpt_disable_all_cores(struct otx_cpt_device
*cpt
)
1557 int grp
, timeout
= 100;
1560 /* Disengage the cores from groups */
1561 for (grp
= 0; grp
< OTX_CPT_MAX_ENGINE_GROUPS
; grp
++) {
1562 writeq(0, cpt
->reg_base
+ OTX_CPT_PF_GX_EN(grp
));
1566 reg
= readq(cpt
->reg_base
+ OTX_CPT_PF_EXEC_BUSY
);
1569 reg
= readq(cpt
->reg_base
+ OTX_CPT_PF_EXEC_BUSY
);
1571 dev_warn(&cpt
->pdev
->dev
, "Cores still busy");
1576 /* Disable the cores */
1577 writeq(0, cpt
->reg_base
+ OTX_CPT_PF_EXE_CTL
);
1580 void otx_cpt_cleanup_eng_grps(struct pci_dev
*pdev
,
1581 struct otx_cpt_eng_grps
*eng_grps
)
1583 struct otx_cpt_eng_grp_info
*grp
;
1586 mutex_lock(&eng_grps
->lock
);
1587 if (eng_grps
->is_ucode_load_created
) {
1588 device_remove_file(&pdev
->dev
,
1589 &eng_grps
->ucode_load_attr
);
1590 eng_grps
->is_ucode_load_created
= false;
1593 /* First delete all mirroring engine groups */
1594 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++)
1595 if (eng_grps
->grp
[i
].mirror
.is_ena
)
1596 delete_engine_group(&pdev
->dev
, &eng_grps
->grp
[i
]);
1598 /* Delete remaining engine groups */
1599 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++)
1600 delete_engine_group(&pdev
->dev
, &eng_grps
->grp
[i
]);
1602 /* Release memory */
1603 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++) {
1604 grp
= &eng_grps
->grp
[i
];
1605 for (j
= 0; j
< OTX_CPT_MAX_ETYPES_PER_GRP
; j
++) {
1606 kfree(grp
->engs
[j
].bmap
);
1607 grp
->engs
[j
].bmap
= NULL
;
1611 mutex_unlock(&eng_grps
->lock
);
1614 int otx_cpt_init_eng_grps(struct pci_dev
*pdev
,
1615 struct otx_cpt_eng_grps
*eng_grps
, int pf_type
)
1617 struct otx_cpt_eng_grp_info
*grp
;
1620 mutex_init(&eng_grps
->lock
);
1621 eng_grps
->obj
= pci_get_drvdata(pdev
);
1622 eng_grps
->avail
.se_cnt
= eng_grps
->avail
.max_se_cnt
;
1623 eng_grps
->avail
.ae_cnt
= eng_grps
->avail
.max_ae_cnt
;
1625 eng_grps
->engs_num
= eng_grps
->avail
.max_se_cnt
+
1626 eng_grps
->avail
.max_ae_cnt
;
1627 if (eng_grps
->engs_num
> OTX_CPT_MAX_ENGINES
) {
1629 "Number of engines %d > than max supported %d",
1630 eng_grps
->engs_num
, OTX_CPT_MAX_ENGINES
);
1635 for (i
= 0; i
< OTX_CPT_MAX_ENGINE_GROUPS
; i
++) {
1636 grp
= &eng_grps
->grp
[i
];
1640 snprintf(grp
->sysfs_info_name
, OTX_CPT_UCODE_NAME_LENGTH
,
1641 "engine_group%d", i
);
1642 for (j
= 0; j
< OTX_CPT_MAX_ETYPES_PER_GRP
; j
++) {
1644 kcalloc(BITS_TO_LONGS(eng_grps
->engs_num
),
1645 sizeof(long), GFP_KERNEL
);
1646 if (!grp
->engs
[j
].bmap
) {
1655 /* OcteonTX 83XX SE CPT PF has only SE engines attached */
1656 eng_grps
->eng_types_supported
= 1 << OTX_CPT_SE_TYPES
;
1660 /* OcteonTX 83XX AE CPT PF has only AE engines attached */
1661 eng_grps
->eng_types_supported
= 1 << OTX_CPT_AE_TYPES
;
1665 dev_err(&pdev
->dev
, "Unknown PF type %d\n", pf_type
);
1670 eng_grps
->ucode_load_attr
.show
= NULL
;
1671 eng_grps
->ucode_load_attr
.store
= ucode_load_store
;
1672 eng_grps
->ucode_load_attr
.attr
.name
= "ucode_load";
1673 eng_grps
->ucode_load_attr
.attr
.mode
= 0220;
1674 sysfs_attr_init(&eng_grps
->ucode_load_attr
.attr
);
1675 ret
= device_create_file(&pdev
->dev
,
1676 &eng_grps
->ucode_load_attr
);
1679 eng_grps
->is_ucode_load_created
= true;
1681 print_dbg_info(&pdev
->dev
, eng_grps
);
1684 otx_cpt_cleanup_eng_grps(pdev
, eng_grps
);