1 /* SPDX-License-Identifier: BSD-2-Clause-NetBSD AND BSD-3-Clause */
2 /* $NetBSD: qat_ae.c,v 1.1 2019/11/20 09:37:46 hikaru Exp $ */
5 * Copyright (c) 2019 Internet Initiative Japan, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
31 * Copyright(c) 2007-2019 Intel Corporation. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
37 * * Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * * Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in
41 * the documentation and/or other materials provided with the
43 * * Neither the name of Intel Corporation nor the names of its
44 * contributors may be used to endorse or promote products derived
45 * from this software without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
50 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
51 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
63 __KERNEL_RCSID(0, "$NetBSD: qat_ae.c,v 1.1 2019/11/20 09:37:46 hikaru Exp $");
66 #include <sys/param.h>
68 #include <sys/firmware.h>
69 #include <sys/limits.h>
70 #include <sys/systm.h>
72 #include <machine/bus.h>
74 #include <dev/pci/pcireg.h>
75 #include <dev/pci/pcivar.h>
79 #include "qat_aevar.h"
81 static int qat_ae_write_4(struct qat_softc *, u_char, bus_size_t,
83 static int qat_ae_read_4(struct qat_softc *, u_char, bus_size_t,
85 static void qat_ae_ctx_indr_write(struct qat_softc *, u_char, uint32_t,
86 bus_size_t, uint32_t);
87 static int qat_ae_ctx_indr_read(struct qat_softc *, u_char, uint32_t,
88 bus_size_t, uint32_t *);
90 static u_short qat_aereg_get_10bit_addr(enum aereg_type, u_short);
91 static int qat_aereg_rel_data_write(struct qat_softc *, u_char, u_char,
92 enum aereg_type, u_short, uint32_t);
93 static int qat_aereg_rel_data_read(struct qat_softc *, u_char, u_char,
94 enum aereg_type, u_short, uint32_t *);
95 static int qat_aereg_rel_rdxfer_write(struct qat_softc *, u_char, u_char,
96 enum aereg_type, u_short, uint32_t);
97 static int qat_aereg_rel_wrxfer_write(struct qat_softc *, u_char, u_char,
98 enum aereg_type, u_short, uint32_t);
99 static int qat_aereg_rel_nn_write(struct qat_softc *, u_char, u_char,
100 enum aereg_type, u_short, uint32_t);
101 static int qat_aereg_abs_to_rel(struct qat_softc *, u_char, u_short,
102 u_short *, u_char *);
103 static int qat_aereg_abs_data_write(struct qat_softc *, u_char,
104 enum aereg_type, u_short, uint32_t);
106 static void qat_ae_enable_ctx(struct qat_softc *, u_char, u_int);
107 static void qat_ae_disable_ctx(struct qat_softc *, u_char, u_int);
108 static void qat_ae_write_ctx_mode(struct qat_softc *, u_char, u_char);
109 static void qat_ae_write_nn_mode(struct qat_softc *, u_char, u_char);
110 static void qat_ae_write_lm_mode(struct qat_softc *, u_char,
111 enum aereg_type, u_char);
112 static void qat_ae_write_shared_cs_mode0(struct qat_softc *, u_char,
114 static void qat_ae_write_shared_cs_mode(struct qat_softc *, u_char, u_char);
115 static int qat_ae_set_reload_ustore(struct qat_softc *, u_char, u_int, int,
118 static enum qat_ae_status qat_ae_get_status(struct qat_softc *, u_char);
119 static int qat_ae_is_active(struct qat_softc *, u_char);
120 static int qat_ae_wait_num_cycles(struct qat_softc *, u_char, int, int);
122 static int qat_ae_clear_reset(struct qat_softc *);
123 static int qat_ae_check(struct qat_softc *);
124 static int qat_ae_reset_timestamp(struct qat_softc *);
125 static void qat_ae_clear_xfer(struct qat_softc *);
126 static int qat_ae_clear_gprs(struct qat_softc *);
128 static void qat_ae_get_shared_ustore_ae(u_char, u_char *);
129 static u_int qat_ae_ucode_parity64(uint64_t);
130 static uint64_t qat_ae_ucode_set_ecc(uint64_t);
131 static int qat_ae_ucode_write(struct qat_softc *, u_char, u_int, u_int,
133 static int qat_ae_ucode_read(struct qat_softc *, u_char, u_int, u_int,
135 static u_int qat_ae_concat_ucode(uint64_t *, u_int, u_int, u_int, u_int *);
136 static int qat_ae_exec_ucode(struct qat_softc *, u_char, u_char,
137 uint64_t *, u_int, int, u_int, u_int *);
138 static int qat_ae_exec_ucode_init_lm(struct qat_softc *, u_char, u_char,
139 int *, uint64_t *, u_int,
140 u_int *, u_int *, u_int *, u_int *, u_int *);
141 static int qat_ae_restore_init_lm_gprs(struct qat_softc *, u_char, u_char,
142 u_int, u_int, u_int, u_int, u_int);
143 static int qat_ae_get_inst_num(int);
144 static int qat_ae_batch_put_lm(struct qat_softc *, u_char,
145 struct qat_ae_batch_init_list *, size_t);
146 static int qat_ae_write_pc(struct qat_softc *, u_char, u_int, u_int);
148 static u_int qat_aefw_csum(char *, int);
149 static const char *qat_aefw_uof_string(struct qat_softc *, size_t);
150 static struct uof_chunk_hdr *qat_aefw_uof_find_chunk(struct qat_softc *,
151 const char *, struct uof_chunk_hdr *);
153 static int qat_aefw_load_mof(struct qat_softc *);
154 static void qat_aefw_unload_mof(struct qat_softc *);
155 static int qat_aefw_load_mmp(struct qat_softc *);
156 static void qat_aefw_unload_mmp(struct qat_softc *);
158 static int qat_aefw_mof_find_uof0(struct qat_softc *,
159 struct mof_uof_hdr *, struct mof_uof_chunk_hdr *,
160 u_int, size_t, const char *,
162 static int qat_aefw_mof_find_uof(struct qat_softc *);
163 static int qat_aefw_mof_parse(struct qat_softc *);
165 static int qat_aefw_uof_parse_image(struct qat_softc *,
166 struct qat_uof_image *, struct uof_chunk_hdr *uch);
167 static int qat_aefw_uof_parse_images(struct qat_softc *);
168 static int qat_aefw_uof_parse(struct qat_softc *);
170 static int qat_aefw_alloc_auth_dmamem(struct qat_softc *, char *, size_t,
171 struct qat_dmamem *);
172 static int qat_aefw_auth(struct qat_softc *, struct qat_dmamem *);
173 static int qat_aefw_suof_load(struct qat_softc *sc,
174 struct qat_dmamem *dma);
175 static int qat_aefw_suof_parse_image(struct qat_softc *,
176 struct qat_suof_image *, struct suof_chunk_hdr *);
177 static int qat_aefw_suof_parse(struct qat_softc *);
178 static int qat_aefw_suof_write(struct qat_softc *);
180 static int qat_aefw_uof_assign_image(struct qat_softc *, struct qat_ae *,
181 struct qat_uof_image *);
182 static int qat_aefw_uof_init_ae(struct qat_softc *, u_char);
183 static int qat_aefw_uof_init(struct qat_softc *);
185 static int qat_aefw_init_memory_one(struct qat_softc *,
186 struct uof_init_mem *);
187 static void qat_aefw_free_lm_init(struct qat_softc *, u_char);
188 static int qat_aefw_init_ustore(struct qat_softc *);
189 static int qat_aefw_init_reg(struct qat_softc *, u_char, u_char,
190 enum aereg_type, u_short, u_int);
191 static int qat_aefw_init_reg_sym_expr(struct qat_softc *, u_char,
192 struct qat_uof_image *);
193 static int qat_aefw_init_memory(struct qat_softc *);
194 static int qat_aefw_init_globals(struct qat_softc *);
195 static uint64_t qat_aefw_get_uof_inst(struct qat_softc *,
196 struct qat_uof_page *, u_int);
197 static int qat_aefw_do_pagein(struct qat_softc *, u_char,
198 struct qat_uof_page *);
199 static int qat_aefw_uof_write_one(struct qat_softc *,
200 struct qat_uof_image *);
201 static int qat_aefw_uof_write(struct qat_softc *);
204 qat_ae_write_4(struct qat_softc *sc, u_char ae, bus_size_t offset,
207 int times = TIMEOUT_AE_CSR;
210 qat_ae_local_write_4(sc, ae, offset, value);
211 if ((qat_ae_local_read_4(sc, ae, LOCAL_CSR_STATUS) &
212 LOCAL_CSR_STATUS_STATUS) == 0)
217 device_printf(sc->sc_dev,
218 "couldn't write AE CSR: ae 0x%hhx offset 0x%lx\n", ae, (long)offset);
223 qat_ae_read_4(struct qat_softc *sc, u_char ae, bus_size_t offset,
226 int times = TIMEOUT_AE_CSR;
230 v = qat_ae_local_read_4(sc, ae, offset);
231 if ((qat_ae_local_read_4(sc, ae, LOCAL_CSR_STATUS) &
232 LOCAL_CSR_STATUS_STATUS) == 0) {
238 device_printf(sc->sc_dev,
239 "couldn't read AE CSR: ae 0x%hhx offset 0x%lx\n", ae, (long)offset);
244 qat_ae_ctx_indr_write(struct qat_softc *sc, u_char ae, uint32_t ctx_mask,
245 bus_size_t offset, uint32_t value)
250 MPASS(offset == CTX_FUTURE_COUNT_INDIRECT ||
251 offset == FUTURE_COUNT_SIGNAL_INDIRECT ||
252 offset == CTX_STS_INDIRECT ||
253 offset == CTX_WAKEUP_EVENTS_INDIRECT ||
254 offset == CTX_SIG_EVENTS_INDIRECT ||
255 offset == LM_ADDR_0_INDIRECT ||
256 offset == LM_ADDR_1_INDIRECT ||
257 offset == INDIRECT_LM_ADDR_0_BYTE_INDEX ||
258 offset == INDIRECT_LM_ADDR_1_BYTE_INDEX);
260 qat_ae_read_4(sc, ae, CSR_CTX_POINTER, &ctxptr);
261 for (ctx = 0; ctx < MAX_AE_CTX; ctx++) {
262 if ((ctx_mask & (1 << ctx)) == 0)
264 qat_ae_write_4(sc, ae, CSR_CTX_POINTER, ctx);
265 qat_ae_write_4(sc, ae, offset, value);
267 qat_ae_write_4(sc, ae, CSR_CTX_POINTER, ctxptr);
271 qat_ae_ctx_indr_read(struct qat_softc *sc, u_char ae, uint32_t ctx,
272 bus_size_t offset, uint32_t *value)
277 MPASS(offset == CTX_FUTURE_COUNT_INDIRECT ||
278 offset == FUTURE_COUNT_SIGNAL_INDIRECT ||
279 offset == CTX_STS_INDIRECT ||
280 offset == CTX_WAKEUP_EVENTS_INDIRECT ||
281 offset == CTX_SIG_EVENTS_INDIRECT ||
282 offset == LM_ADDR_0_INDIRECT ||
283 offset == LM_ADDR_1_INDIRECT ||
284 offset == INDIRECT_LM_ADDR_0_BYTE_INDEX ||
285 offset == INDIRECT_LM_ADDR_1_BYTE_INDEX);
287 /* save the ctx ptr */
288 qat_ae_read_4(sc, ae, CSR_CTX_POINTER, &ctxptr);
289 if ((ctxptr & CSR_CTX_POINTER_CONTEXT) !=
290 (ctx & CSR_CTX_POINTER_CONTEXT))
291 qat_ae_write_4(sc, ae, CSR_CTX_POINTER, ctx);
293 error = qat_ae_read_4(sc, ae, offset, value);
295 /* restore ctx ptr */
296 if ((ctxptr & CSR_CTX_POINTER_CONTEXT) !=
297 (ctx & CSR_CTX_POINTER_CONTEXT))
298 qat_ae_write_4(sc, ae, CSR_CTX_POINTER, ctxptr);
304 qat_aereg_get_10bit_addr(enum aereg_type regtype, u_short reg)
311 addr = (reg & 0x7f) | 0x80;
317 case AEREG_SR_RD_REL:
318 case AEREG_SR_WR_REL:
320 addr = 0x180 | (reg & 0x1f);
323 addr = 0x140 | ((reg & 0x3) << 1);
325 case AEREG_DR_RD_REL:
326 case AEREG_DR_WR_REL:
328 addr = 0x1c0 | (reg & 0x1f);
331 addr = 0x100 | ((reg & 0x3) << 1);
333 case AEREG_NEIGH_INDX:
334 addr = 0x241 | ((reg & 0x3) << 1);
336 case AEREG_NEIGH_REL:
337 addr = 0x280 | (reg & 0x1f);
346 addr = 0x300 | (reg & 0xff);
349 addr = AEREG_BAD_REGADDR;
356 qat_aereg_rel_data_write(struct qat_softc *sc, u_char ae, u_char ctx,
357 enum aereg_type regtype, u_short relreg, uint32_t value)
359 uint16_t srchi, srclo, destaddr, data16hi, data16lo;
361 0x0F440000000ull, /* immed_w1[reg, val_hi16] */
362 0x0F040000000ull, /* immed_w0[reg, val_lo16] */
363 0x0F0000C0300ull, /* nop */
364 0x0E000010000ull /* ctx_arb[kill] */
366 const int ninst = nitems(inst);
367 const int imm_w1 = 0, imm_w0 = 1;
371 /* This logic only works for GPRs and LM index registers,
372 not NN or XFER registers! */
373 MPASS(regtype == AEREG_GPA_REL || regtype == AEREG_GPB_REL ||
374 regtype == AEREG_LMEM0 || regtype == AEREG_LMEM1);
376 if ((regtype == AEREG_GPA_REL) || (regtype == AEREG_GPB_REL)) {
377 /* determine the context mode */
378 qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
379 if (ctxen & CTX_ENABLES_INUSE_CONTEXTS) {
391 if ((destaddr = qat_aereg_get_10bit_addr(regtype, relreg)) ==
396 data16lo = 0xffff & value;
397 data16hi = 0xffff & (value >> 16);
398 srchi = qat_aereg_get_10bit_addr(AEREG_NO_DEST,
399 (uint16_t)(0xff & data16hi));
400 srclo = qat_aereg_get_10bit_addr(AEREG_NO_DEST,
401 (uint16_t)(0xff & data16lo));
404 case AEREG_GPA_REL: /* A rel source */
405 inst[imm_w1] = inst[imm_w1] | ((data16hi >> 8) << 20) |
406 ((srchi & 0x3ff) << 10) | (destaddr & 0x3ff);
407 inst[imm_w0] = inst[imm_w0] | ((data16lo >> 8) << 20) |
408 ((srclo & 0x3ff) << 10) | (destaddr & 0x3ff);
411 inst[imm_w1] = inst[imm_w1] | ((data16hi >> 8) << 20) |
412 ((destaddr & 0x3ff) << 10) | (srchi & 0x3ff);
413 inst[imm_w0] = inst[imm_w0] | ((data16lo >> 8) << 20) |
414 ((destaddr & 0x3ff) << 10) | (srclo & 0x3ff);
418 return qat_ae_exec_ucode(sc, ae, ctx, inst, ninst, 1, ninst * 5, NULL);
422 qat_aereg_rel_data_read(struct qat_softc *sc, u_char ae, u_char ctx,
423 enum aereg_type regtype, u_short relreg, uint32_t *value)
425 uint64_t inst, savucode;
426 uint32_t ctxen, misc, nmisc, savctx, ctxarbctl, ulo, uhi;
427 u_int uaddr, ustore_addr;
429 u_short mask, regaddr;
432 MPASS(regtype == AEREG_GPA_REL || regtype == AEREG_GPB_REL ||
433 regtype == AEREG_SR_REL || regtype == AEREG_SR_RD_REL ||
434 regtype == AEREG_DR_REL || regtype == AEREG_DR_RD_REL ||
435 regtype == AEREG_LMEM0 || regtype == AEREG_LMEM1);
437 if ((regtype == AEREG_GPA_REL) || (regtype == AEREG_GPB_REL) ||
438 (regtype == AEREG_SR_REL) || (regtype == AEREG_SR_RD_REL) ||
439 (regtype == AEREG_DR_REL) || (regtype == AEREG_DR_RD_REL))
441 /* determine the context mode */
442 qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
443 if (ctxen & CTX_ENABLES_INUSE_CONTEXTS) {
455 if ((regaddr = qat_aereg_get_10bit_addr(regtype, relreg)) ==
460 /* instruction -- alu[--, --, B, reg] */
464 inst = 0xA070000000ull | (regaddr & 0x3ff);
467 inst = (0xA030000000ull | ((regaddr & 0x3ff) << 10));
471 /* backup shared control store bit, and force AE to
472 * none-shared mode before executing ucode snippet */
473 qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &misc);
474 if (misc & AE_MISC_CONTROL_SHARE_CS) {
475 qat_ae_get_shared_ustore_ae(ae, &nae);
476 if ((1 << nae) & sc->sc_ae_mask && qat_ae_is_active(sc, nae))
480 nmisc = misc & ~AE_MISC_CONTROL_SHARE_CS;
481 qat_ae_write_4(sc, ae, AE_MISC_CONTROL, nmisc);
483 /* read current context */
484 qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS, &savctx);
485 qat_ae_read_4(sc, ae, CTX_ARB_CNTL, &ctxarbctl);
487 qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
488 /* prevent clearing the W1C bits: the breakpoint bit,
489 ECC error bit, and Parity error bit */
490 ctxen &= CTX_ENABLES_IGNORE_W1C_MASK;
492 /* change the context */
493 if (ctx != (savctx & ACTIVE_CTX_STATUS_ACNO))
494 qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS,
495 ctx & ACTIVE_CTX_STATUS_ACNO);
496 /* save a ustore location */
497 if ((error = qat_ae_ucode_read(sc, ae, 0, 1, &savucode)) != 0) {
498 /* restore AE_MISC_CONTROL csr */
499 qat_ae_write_4(sc, ae, AE_MISC_CONTROL, misc);
501 /* restore the context */
502 if (ctx != (savctx & ACTIVE_CTX_STATUS_ACNO)) {
503 qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS,
504 savctx & ACTIVE_CTX_STATUS_ACNO);
506 qat_ae_write_4(sc, ae, CTX_ARB_CNTL, ctxarbctl);
511 /* turn off ustore parity */
512 qat_ae_write_4(sc, ae, CTX_ENABLES,
513 ctxen & (~CTX_ENABLES_CNTL_STORE_PARITY_ENABLE));
515 /* save ustore-addr csr */
516 qat_ae_read_4(sc, ae, USTORE_ADDRESS, &ustore_addr);
518 /* write the ALU instruction to ustore, enable ecs bit */
519 uaddr = 0 | USTORE_ADDRESS_ECS;
521 /* set the uaddress */
522 qat_ae_write_4(sc, ae, USTORE_ADDRESS, uaddr);
523 inst = qat_ae_ucode_set_ecc(inst);
525 ulo = (uint32_t)(inst & 0xffffffff);
526 uhi = (uint32_t)(inst >> 32);
528 qat_ae_write_4(sc, ae, USTORE_DATA_LOWER, ulo);
530 /* this will auto increment the address */
531 qat_ae_write_4(sc, ae, USTORE_DATA_UPPER, uhi);
533 /* set the uaddress */
534 qat_ae_write_4(sc, ae, USTORE_ADDRESS, uaddr);
536 /* delay for at least 8 cycles */
537 qat_ae_wait_num_cycles(sc, ae, 0x8, 0);
539 /* read ALU output -- the instruction should have been executed
540 prior to clearing the ECS in putUwords */
541 qat_ae_read_4(sc, ae, ALU_OUT, value);
543 /* restore ustore-addr csr */
544 qat_ae_write_4(sc, ae, USTORE_ADDRESS, ustore_addr);
546 /* restore the ustore */
547 error = qat_ae_ucode_write(sc, ae, 0, 1, &savucode);
549 /* restore the context */
550 if (ctx != (savctx & ACTIVE_CTX_STATUS_ACNO)) {
551 qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS,
552 savctx & ACTIVE_CTX_STATUS_ACNO);
555 qat_ae_write_4(sc, ae, CTX_ARB_CNTL, ctxarbctl);
557 /* restore AE_MISC_CONTROL csr */
558 qat_ae_write_4(sc, ae, AE_MISC_CONTROL, misc);
560 qat_ae_write_4(sc, ae, CTX_ENABLES, ctxen);
566 qat_aereg_rel_rdxfer_write(struct qat_softc *sc, u_char ae, u_char ctx,
567 enum aereg_type regtype, u_short relreg, uint32_t value)
575 MPASS(regtype == AEREG_SR_REL || regtype == AEREG_DR_REL ||
576 regtype == AEREG_SR_RD_REL || regtype == AEREG_DR_RD_REL);
578 error = qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
579 if (ctxen & CTX_ENABLES_INUSE_CONTEXTS) {
581 device_printf(sc->sc_dev,
582 "bad ctx argument in 4-ctx mode,ctx=0x%x\n", ctx);
596 addr = relreg + (ctx << 0x5);
600 case AEREG_SR_RD_REL:
601 qat_ae_xfer_write_4(sc, ae, addr, value);
604 case AEREG_DR_RD_REL:
605 qat_ae_xfer_write_4(sc, ae, addr + dr_offset, value);
615 qat_aereg_rel_wrxfer_write(struct qat_softc *sc, u_char ae, u_char ctx,
616 enum aereg_type regtype, u_short relreg, uint32_t value)
625 qat_aereg_rel_nn_write(struct qat_softc *sc, u_char ae, u_char ctx,
626 enum aereg_type regtype, u_short relreg, uint32_t value)
635 qat_aereg_abs_to_rel(struct qat_softc *sc, u_char ae,
636 u_short absreg, u_short *relreg, u_char *ctx)
640 qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
641 if (ctxen & CTX_ENABLES_INUSE_CONTEXTS) {
643 *relreg = absreg & 0x1f;
644 *ctx = (absreg >> 0x4) & 0x6;
647 *relreg = absreg & 0x0f;
648 *ctx = (absreg >> 0x4) & 0x7;
655 qat_aereg_abs_data_write(struct qat_softc *sc, u_char ae,
656 enum aereg_type regtype, u_short absreg, uint32_t value)
662 qat_aereg_abs_to_rel(sc, ae, absreg, &relreg, &ctx);
666 MPASS(absreg < MAX_GPR_REG);
667 error = qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPA_REL,
671 MPASS(absreg < MAX_GPR_REG);
672 error = qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPB_REL,
675 case AEREG_DR_RD_ABS:
676 MPASS(absreg < MAX_XFER_REG);
677 error = qat_aereg_rel_rdxfer_write(sc, ae, ctx, AEREG_DR_RD_REL,
680 case AEREG_SR_RD_ABS:
681 MPASS(absreg < MAX_XFER_REG);
682 error = qat_aereg_rel_rdxfer_write(sc, ae, ctx, AEREG_SR_RD_REL,
685 case AEREG_DR_WR_ABS:
686 MPASS(absreg < MAX_XFER_REG);
687 error = qat_aereg_rel_wrxfer_write(sc, ae, ctx, AEREG_DR_WR_REL,
690 case AEREG_SR_WR_ABS:
691 MPASS(absreg < MAX_XFER_REG);
692 error = qat_aereg_rel_wrxfer_write(sc, ae, ctx, AEREG_SR_WR_REL,
695 case AEREG_NEIGH_ABS:
696 MPASS(absreg < MAX_NN_REG);
697 if (absreg >= MAX_NN_REG)
699 error = qat_aereg_rel_nn_write(sc, ae, ctx, AEREG_NEIGH_REL,
703 panic("Invalid Register Type");
710 qat_ae_enable_ctx(struct qat_softc *sc, u_char ae, u_int ctx_mask)
714 qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
715 ctxen &= CTX_ENABLES_IGNORE_W1C_MASK;
717 if (ctxen & CTX_ENABLES_INUSE_CONTEXTS) {
723 ctxen |= __SHIFTIN(ctx_mask, CTX_ENABLES_ENABLE);
724 qat_ae_write_4(sc, ae, CTX_ENABLES, ctxen);
728 qat_ae_disable_ctx(struct qat_softc *sc, u_char ae, u_int ctx_mask)
732 qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
733 ctxen &= CTX_ENABLES_IGNORE_W1C_MASK;
734 ctxen &= ~(__SHIFTIN(ctx_mask & AE_ALL_CTX, CTX_ENABLES_ENABLE));
735 qat_ae_write_4(sc, ae, CTX_ENABLES, ctxen);
739 qat_ae_write_ctx_mode(struct qat_softc *sc, u_char ae, u_char mode)
743 qat_ae_read_4(sc, ae, CTX_ENABLES, &val);
744 val &= CTX_ENABLES_IGNORE_W1C_MASK;
747 nval = val | CTX_ENABLES_INUSE_CONTEXTS;
749 nval = val & ~CTX_ENABLES_INUSE_CONTEXTS;
752 qat_ae_write_4(sc, ae, CTX_ENABLES, nval);
756 qat_ae_write_nn_mode(struct qat_softc *sc, u_char ae, u_char mode)
760 qat_ae_read_4(sc, ae, CTX_ENABLES, &val);
761 val &= CTX_ENABLES_IGNORE_W1C_MASK;
764 nval = val | CTX_ENABLES_NN_MODE;
766 nval = val & ~CTX_ENABLES_NN_MODE;
769 qat_ae_write_4(sc, ae, CTX_ENABLES, nval);
773 qat_ae_write_lm_mode(struct qat_softc *sc, u_char ae,
774 enum aereg_type lm, u_char mode)
779 qat_ae_read_4(sc, ae, CTX_ENABLES, &val);
780 val &= CTX_ENABLES_IGNORE_W1C_MASK;
784 bit = CTX_ENABLES_LMADDR_0_GLOBAL;
787 bit = CTX_ENABLES_LMADDR_1_GLOBAL;
790 panic("invalid lmem reg type");
800 qat_ae_write_4(sc, ae, CTX_ENABLES, nval);
804 qat_ae_write_shared_cs_mode0(struct qat_softc *sc, u_char ae, u_char mode)
808 qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &val);
811 nval = val | AE_MISC_CONTROL_SHARE_CS;
813 nval = val & ~AE_MISC_CONTROL_SHARE_CS;
816 qat_ae_write_4(sc, ae, AE_MISC_CONTROL, nval);
820 qat_ae_write_shared_cs_mode(struct qat_softc *sc, u_char ae, u_char mode)
824 qat_ae_get_shared_ustore_ae(ae, &nae);
826 qat_ae_write_shared_cs_mode0(sc, ae, mode);
828 if ((sc->sc_ae_mask & (1 << nae))) {
829 qat_ae_write_shared_cs_mode0(sc, nae, mode);
834 qat_ae_set_reload_ustore(struct qat_softc *sc, u_char ae,
835 u_int reload_size, int shared_mode, u_int ustore_dram_addr)
837 uint32_t val, cs_reload;
839 switch (reload_size) {
857 QAT_AE(sc, ae).qae_ustore_dram_addr = ustore_dram_addr;
859 QAT_AE(sc, ae).qae_reload_size = reload_size;
861 qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &val);
862 val &= ~(AE_MISC_CONTROL_ONE_CTX_RELOAD |
863 AE_MISC_CONTROL_CS_RELOAD | AE_MISC_CONTROL_SHARE_CS);
864 val |= __SHIFTIN(cs_reload, AE_MISC_CONTROL_CS_RELOAD) |
865 __SHIFTIN(shared_mode, AE_MISC_CONTROL_ONE_CTX_RELOAD);
866 qat_ae_write_4(sc, ae, AE_MISC_CONTROL, val);
871 static enum qat_ae_status
872 qat_ae_get_status(struct qat_softc *sc, u_char ae)
877 error = qat_ae_read_4(sc, ae, CTX_ENABLES, &val);
878 if (error || val & CTX_ENABLES_ENABLE)
879 return QAT_AE_ENABLED;
881 qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS, &val);
882 if (val & ACTIVE_CTX_STATUS_ABO)
883 return QAT_AE_ACTIVE;
885 return QAT_AE_DISABLED;
890 qat_ae_is_active(struct qat_softc *sc, u_char ae)
894 if (qat_ae_get_status(sc, ae) != QAT_AE_DISABLED)
897 qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS, &val);
898 if (val & ACTIVE_CTX_STATUS_ABO)
904 /* returns 1 if actually waited for specified number of cycles */
906 qat_ae_wait_num_cycles(struct qat_softc *sc, u_char ae, int cycles, int check)
909 int pcnt, ccnt, elapsed, times;
911 qat_ae_read_4(sc, ae, PROFILE_COUNT, &cnt);
914 times = TIMEOUT_AE_CHECK;
916 qat_ae_read_4(sc, ae, PROFILE_COUNT, &cnt);
919 elapsed = ccnt - pcnt;
924 device_printf(sc->sc_dev,
925 "qat_ae_wait_num_cycles timeout\n");
932 if (elapsed >= CYCLES_FROM_READY2EXE && check) {
933 if (qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS,
935 if ((actx & ACTIVE_CTX_STATUS_ABO) == 0)
939 } while (cycles > elapsed);
941 if (check && qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS, &actx) == 0) {
942 if ((actx & ACTIVE_CTX_STATUS_ABO) == 0)
950 qat_ae_init(struct qat_softc *sc)
953 uint32_t mask, val = 0;
956 /* XXX adf_initSysMemInfo */
958 /* XXX Disable clock gating for some chip if debug mode */
960 for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
961 struct qat_ae *qae = &sc->sc_ae[ae];
965 qae->qae_ustore_size = USTORE_SIZE;
967 qae->qae_free_addr = 0;
968 qae->qae_free_size = USTORE_SIZE;
969 qae->qae_live_ctx_mask = AE_ALL_CTX;
970 qae->qae_ustore_dram_addr = 0;
971 qae->qae_reload_size = 0;
974 /* XXX Enable attention interrupt */
976 error = qat_ae_clear_reset(sc);
980 qat_ae_clear_xfer(sc);
982 if (!sc->sc_hw.qhw_fw_auth) {
983 error = qat_ae_clear_gprs(sc);
988 /* Set SIGNATURE_ENABLE[0] to 0x1 in order to enable ALU_OUT csr */
989 for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
992 qat_ae_read_4(sc, ae, SIGNATURE_ENABLE, &val);
994 qat_ae_write_4(sc, ae, SIGNATURE_ENABLE, val);
997 error = qat_ae_clear_reset(sc);
1001 /* XXX XXX XXX Clean MMP memory if mem scrub is supported */
1002 /* halMem_ScrubMMPMemory */
1008 qat_ae_start(struct qat_softc *sc)
1013 for (ae = 0; ae < sc->sc_ae_num; ae++) {
1014 if ((sc->sc_ae_mask & (1 << ae)) == 0)
1017 error = qat_aefw_start(sc, ae, 0xff);
1026 qat_ae_cluster_intr(void *arg)
1028 /* Nothing to implement until we support SRIOV. */
1029 printf("qat_ae_cluster_intr\n");
1033 qat_ae_clear_reset(struct qat_softc *sc)
1036 uint32_t times, reset, clock, reg, mask;
1039 reset = qat_cap_global_read_4(sc, CAP_GLOBAL_CTL_RESET);
1040 reset &= ~(__SHIFTIN(sc->sc_ae_mask, CAP_GLOBAL_CTL_RESET_AE_MASK));
1041 reset &= ~(__SHIFTIN(sc->sc_accel_mask, CAP_GLOBAL_CTL_RESET_ACCEL_MASK));
1042 times = TIMEOUT_AE_RESET;
1044 qat_cap_global_write_4(sc, CAP_GLOBAL_CTL_RESET, reset);
1045 if ((times--) == 0) {
1046 device_printf(sc->sc_dev, "couldn't reset AEs\n");
1049 reg = qat_cap_global_read_4(sc, CAP_GLOBAL_CTL_RESET);
1050 } while ((__SHIFTIN(sc->sc_ae_mask, CAP_GLOBAL_CTL_RESET_AE_MASK) |
1051 __SHIFTIN(sc->sc_accel_mask, CAP_GLOBAL_CTL_RESET_ACCEL_MASK))
1054 /* Enable clock for AE and QAT */
1055 clock = qat_cap_global_read_4(sc, CAP_GLOBAL_CTL_CLK_EN);
1056 clock |= __SHIFTIN(sc->sc_ae_mask, CAP_GLOBAL_CTL_CLK_EN_AE_MASK);
1057 clock |= __SHIFTIN(sc->sc_accel_mask, CAP_GLOBAL_CTL_CLK_EN_ACCEL_MASK);
1058 qat_cap_global_write_4(sc, CAP_GLOBAL_CTL_CLK_EN, clock);
1060 error = qat_ae_check(sc);
1065 * Set undefined power-up/reset states to reasonable default values...
1066 * just to make sure we're starting from a known point
1068 for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
1072 /* init the ctx_enable */
1073 qat_ae_write_4(sc, ae, CTX_ENABLES,
1076 /* initialize the PCs */
1077 qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1079 UPC_MASK & CTX_STS_INDIRECT_UPC_INIT);
1081 /* init the ctx_arb */
1082 qat_ae_write_4(sc, ae, CTX_ARB_CNTL,
1086 qat_ae_write_4(sc, ae, CC_ENABLE,
1088 qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1089 CTX_WAKEUP_EVENTS_INDIRECT,
1090 CTX_WAKEUP_EVENTS_INDIRECT_INIT);
1091 qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1092 CTX_SIG_EVENTS_INDIRECT,
1093 CTX_SIG_EVENTS_INDIRECT_INIT);
1096 if ((sc->sc_ae_mask != 0) &&
1097 sc->sc_flags & QAT_FLAG_ESRAM_ENABLE_AUTO_INIT) {
1098 /* XXX XXX XXX init eSram only when this is boot time */
1101 if ((sc->sc_ae_mask != 0) &&
1102 sc->sc_flags & QAT_FLAG_SHRAM_WAIT_READY) {
1103 /* XXX XXX XXX wait shram to complete initialization */
1106 qat_ae_reset_timestamp(sc);
1112 qat_ae_check(struct qat_softc *sc)
1114 int error, times, ae;
1115 uint32_t cnt, pcnt, mask;
1117 for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
1121 times = TIMEOUT_AE_CHECK;
1122 error = qat_ae_read_4(sc, ae, PROFILE_COUNT, &cnt);
1124 device_printf(sc->sc_dev,
1125 "couldn't access AE %d CSR\n", ae);
1128 pcnt = cnt & 0xffff;
1131 error = qat_ae_read_4(sc, ae,
1132 PROFILE_COUNT, &cnt);
1134 device_printf(sc->sc_dev,
1135 "couldn't access AE %d CSR\n", ae);
1144 device_printf(sc->sc_dev,
1145 "AE %d CSR is useless\n", ae);
1155 qat_ae_reset_timestamp(struct qat_softc *sc)
1157 uint32_t misc, mask;
1160 /* stop the timestamp timers */
1161 misc = qat_cap_global_read_4(sc, CAP_GLOBAL_CTL_MISC);
1162 if (misc & CAP_GLOBAL_CTL_MISC_TIMESTAMP_EN) {
1163 qat_cap_global_write_4(sc, CAP_GLOBAL_CTL_MISC,
1164 misc & (~CAP_GLOBAL_CTL_MISC_TIMESTAMP_EN));
1167 for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
1170 qat_ae_write_4(sc, ae, TIMESTAMP_LOW, 0);
1171 qat_ae_write_4(sc, ae, TIMESTAMP_HIGH, 0);
1174 /* start timestamp timers */
1175 qat_cap_global_write_4(sc, CAP_GLOBAL_CTL_MISC,
1176 misc | CAP_GLOBAL_CTL_MISC_TIMESTAMP_EN);
1182 qat_ae_clear_xfer(struct qat_softc *sc)
1187 for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
1191 for (reg = 0; reg < MAX_GPR_REG; reg++) {
1192 qat_aereg_abs_data_write(sc, ae, AEREG_SR_RD_ABS,
1194 qat_aereg_abs_data_write(sc, ae, AEREG_DR_RD_ABS,
1201 qat_ae_clear_gprs(struct qat_softc *sc)
1204 uint32_t saved_ctx = 0;
1205 int times = TIMEOUT_AE_CHECK, rv;
1209 for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
1213 /* turn off share control store bit */
1214 val = qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &val);
1215 val &= ~AE_MISC_CONTROL_SHARE_CS;
1216 qat_ae_write_4(sc, ae, AE_MISC_CONTROL, val);
1218 /* turn off ucode parity */
1219 /* make sure nn_mode is set to self */
1220 qat_ae_read_4(sc, ae, CTX_ENABLES, &val);
1221 val &= CTX_ENABLES_IGNORE_W1C_MASK;
1222 val |= CTX_ENABLES_NN_MODE;
1223 val &= ~CTX_ENABLES_CNTL_STORE_PARITY_ENABLE;
1224 qat_ae_write_4(sc, ae, CTX_ENABLES, val);
1226 /* copy instructions to ustore */
1227 qat_ae_ucode_write(sc, ae, 0, nitems(ae_clear_gprs_inst),
1228 ae_clear_gprs_inst);
1231 qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX, CTX_STS_INDIRECT,
1232 UPC_MASK & CTX_STS_INDIRECT_UPC_INIT);
1234 /* save current context */
1235 qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS, &saved_ctx);
1236 /* change the active context */
1237 /* start the context from ctx 0 */
1238 qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS, 0);
1240 /* wakeup-event voluntary */
1241 qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1242 CTX_WAKEUP_EVENTS_INDIRECT,
1243 CTX_WAKEUP_EVENTS_INDIRECT_VOLUNTARY);
1245 qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1246 CTX_SIG_EVENTS_INDIRECT, 0);
1247 qat_ae_write_4(sc, ae, CTX_SIG_EVENTS_ACTIVE, 0);
1249 qat_ae_enable_ctx(sc, ae, AE_ALL_CTX);
1252 for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
1255 /* wait for AE to finish */
1257 rv = qat_ae_wait_num_cycles(sc, ae, AE_EXEC_CYCLE, 1);
1258 } while (rv && times--);
1260 device_printf(sc->sc_dev,
1261 "qat_ae_clear_gprs timeout");
1264 qat_ae_disable_ctx(sc, ae, AE_ALL_CTX);
1265 /* change the active context */
1266 qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS,
1267 saved_ctx & ACTIVE_CTX_STATUS_ACNO);
1268 /* init the ctx_enable */
1269 qat_ae_write_4(sc, ae, CTX_ENABLES, CTX_ENABLES_INIT);
1270 /* initialize the PCs */
1271 qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1272 CTX_STS_INDIRECT, UPC_MASK & CTX_STS_INDIRECT_UPC_INIT);
1273 /* init the ctx_arb */
1274 qat_ae_write_4(sc, ae, CTX_ARB_CNTL, CTX_ARB_CNTL_INIT);
1276 qat_ae_write_4(sc, ae, CC_ENABLE, CC_ENABLE_INIT);
1277 qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1278 CTX_WAKEUP_EVENTS_INDIRECT, CTX_WAKEUP_EVENTS_INDIRECT_INIT);
1279 qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX, CTX_SIG_EVENTS_INDIRECT,
1280 CTX_SIG_EVENTS_INDIRECT_INIT);
1287 qat_ae_get_shared_ustore_ae(u_char ae, u_char *nae)
1296 qat_ae_ucode_parity64(uint64_t ucode)
1299 ucode ^= ucode >> 1;
1300 ucode ^= ucode >> 2;
1301 ucode ^= ucode >> 4;
1302 ucode ^= ucode >> 8;
1303 ucode ^= ucode >> 16;
1304 ucode ^= ucode >> 32;
1306 return ((u_int)(ucode & 1));
1310 qat_ae_ucode_set_ecc(uint64_t ucode)
1312 static const uint64_t
1313 bit0mask=0xff800007fffULL, bit1mask=0x1f801ff801fULL,
1314 bit2mask=0xe387e0781e1ULL, bit3mask=0x7cb8e388e22ULL,
1315 bit4mask=0xaf5b2c93244ULL, bit5mask=0xf56d5525488ULL,
1316 bit6mask=0xdaf69a46910ULL;
1318 /* clear the ecc bits */
1319 ucode &= ~(0x7fULL << USTORE_ECC_BIT_0);
1321 ucode |= (uint64_t)qat_ae_ucode_parity64(bit0mask & ucode) <<
1323 ucode |= (uint64_t)qat_ae_ucode_parity64(bit1mask & ucode) <<
1325 ucode |= (uint64_t)qat_ae_ucode_parity64(bit2mask & ucode) <<
1327 ucode |= (uint64_t)qat_ae_ucode_parity64(bit3mask & ucode) <<
1329 ucode |= (uint64_t)qat_ae_ucode_parity64(bit4mask & ucode) <<
1331 ucode |= (uint64_t)qat_ae_ucode_parity64(bit5mask & ucode) <<
1333 ucode |= (uint64_t)qat_ae_ucode_parity64(bit6mask & ucode) <<
1340 qat_ae_ucode_write(struct qat_softc *sc, u_char ae, u_int uaddr, u_int ninst,
1341 const uint64_t *ucode)
1344 uint32_t ustore_addr, ulo, uhi;
1347 qat_ae_read_4(sc, ae, USTORE_ADDRESS, &ustore_addr);
1348 uaddr |= USTORE_ADDRESS_ECS;
1350 qat_ae_write_4(sc, ae, USTORE_ADDRESS, uaddr);
1351 for (i = 0; i < ninst; i++) {
1352 tmp = qat_ae_ucode_set_ecc(ucode[i]);
1353 ulo = (uint32_t)(tmp & 0xffffffff);
1354 uhi = (uint32_t)(tmp >> 32);
1356 qat_ae_write_4(sc, ae, USTORE_DATA_LOWER, ulo);
1357 /* this will auto increment the address */
1358 qat_ae_write_4(sc, ae, USTORE_DATA_UPPER, uhi);
1360 qat_ae_write_4(sc, ae, USTORE_ADDRESS, ustore_addr);
1366 qat_ae_ucode_read(struct qat_softc *sc, u_char ae, u_int uaddr, u_int ninst,
1369 uint32_t misc, ustore_addr, ulo, uhi;
1373 if (qat_ae_get_status(sc, ae) != QAT_AE_DISABLED)
1376 /* determine whether it neighbour AE runs in shared control store
1378 qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &misc);
1379 if (misc & AE_MISC_CONTROL_SHARE_CS) {
1380 qat_ae_get_shared_ustore_ae(ae, &nae);
1381 if ((sc->sc_ae_mask & (1 << nae)) && qat_ae_is_active(sc, nae))
1385 /* if reloadable, then get it all from dram-ustore */
1386 if (__SHIFTOUT(misc, AE_MISC_CONTROL_CS_RELOAD))
1387 panic("notyet"); /* XXX getReloadUwords */
1389 /* disable SHARE_CS bit to workaround silicon bug */
1390 qat_ae_write_4(sc, ae, AE_MISC_CONTROL, misc & 0xfffffffb);
1392 MPASS(uaddr + ninst <= USTORE_SIZE);
1394 /* save ustore-addr csr */
1395 qat_ae_read_4(sc, ae, USTORE_ADDRESS, &ustore_addr);
1397 uaddr |= USTORE_ADDRESS_ECS; /* enable ecs bit */
1398 for (ii = 0; ii < ninst; ii++) {
1399 qat_ae_write_4(sc, ae, USTORE_ADDRESS, uaddr);
1402 qat_ae_read_4(sc, ae, USTORE_DATA_LOWER, &ulo);
1403 qat_ae_read_4(sc, ae, USTORE_DATA_UPPER, &uhi);
1405 ucode[ii] = (ucode[ii] << 32) | ulo;
1408 /* restore SHARE_CS bit to workaround silicon bug */
1409 qat_ae_write_4(sc, ae, AE_MISC_CONTROL, misc);
1410 qat_ae_write_4(sc, ae, USTORE_ADDRESS, ustore_addr);
1416 qat_ae_concat_ucode(uint64_t *ucode, u_int ninst, u_int size, u_int addr,
1419 const uint64_t *inst_arr;
1420 u_int ninst0, curvalue;
1421 int ii, vali, fixup, usize = 0;
1428 curvalue = value[vali++];
1432 inst_arr = ae_inst_1b;
1433 usize = nitems(ae_inst_1b);
1436 inst_arr = ae_inst_2b;
1437 usize = nitems(ae_inst_2b);
1440 inst_arr = ae_inst_3b;
1441 usize = nitems(ae_inst_3b);
1444 inst_arr = ae_inst_4b;
1445 usize = nitems(ae_inst_4b);
1450 for (ii = 0; ii < usize; ii++)
1451 ucode[ninst++] = inst_arr[ii];
1453 INSERT_IMMED_GPRA_CONST(ucode[fixup], (addr));
1455 INSERT_IMMED_GPRA_CONST(ucode[fixup], 0);
1457 INSERT_IMMED_GPRB_CONST(ucode[fixup], (curvalue >> 0));
1459 INSERT_IMMED_GPRB_CONST(ucode[fixup], (curvalue >> 16));
1463 return (ninst - ninst0);
1465 size -= sizeof(u_int);
1466 while (size >= sizeof(u_int)) {
1467 curvalue = value[vali++];
1469 ucode[ninst++] = ae_inst_4b[0x2];
1470 ucode[ninst++] = ae_inst_4b[0x3];
1471 ucode[ninst++] = ae_inst_4b[0x8];
1472 INSERT_IMMED_GPRB_CONST(ucode[fixup], (curvalue >> 16));
1474 INSERT_IMMED_GPRB_CONST(ucode[fixup], (curvalue >> 0));
1477 addr += sizeof(u_int);
1478 size -= sizeof(u_int);
1480 /* call this function recusive when the left size less than 4 */
1482 qat_ae_concat_ucode(ucode, ninst, size, addr, value + vali);
1484 return (ninst - ninst0);
1488 qat_ae_exec_ucode(struct qat_softc *sc, u_char ae, u_char ctx,
1489 uint64_t *ucode, u_int ninst, int cond_code_off, u_int max_cycles,
1492 int error = 0, share_cs = 0;
1493 uint64_t savucode[MAX_EXEC_INST];
1494 uint32_t indr_lm_addr_0, indr_lm_addr_1;
1495 uint32_t indr_lm_addr_byte_0, indr_lm_addr_byte_1;
1496 uint32_t indr_future_cnt_sig;
1497 uint32_t indr_sig, active_sig;
1498 uint32_t wakeup_ev, savpc, savcc, savctx, ctxarbctl;
1499 uint32_t misc, nmisc, ctxen;
1502 MPASS(ninst <= USTORE_SIZE);
1504 if (qat_ae_is_active(sc, ae))
1507 /* save current LM addr */
1508 qat_ae_ctx_indr_read(sc, ae, ctx, LM_ADDR_0_INDIRECT, &indr_lm_addr_0);
1509 qat_ae_ctx_indr_read(sc, ae, ctx, LM_ADDR_1_INDIRECT, &indr_lm_addr_1);
1510 qat_ae_ctx_indr_read(sc, ae, ctx, INDIRECT_LM_ADDR_0_BYTE_INDEX,
1511 &indr_lm_addr_byte_0);
1512 qat_ae_ctx_indr_read(sc, ae, ctx, INDIRECT_LM_ADDR_1_BYTE_INDEX,
1513 &indr_lm_addr_byte_1);
1515 /* backup shared control store bit, and force AE to
1516 none-shared mode before executing ucode snippet */
1517 qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &misc);
1518 if (misc & AE_MISC_CONTROL_SHARE_CS) {
1520 qat_ae_get_shared_ustore_ae(ae, &nae);
1521 if ((sc->sc_ae_mask & (1 << nae)) && qat_ae_is_active(sc, nae))
1524 nmisc = misc & ~AE_MISC_CONTROL_SHARE_CS;
1525 qat_ae_write_4(sc, ae, AE_MISC_CONTROL, nmisc);
1527 /* save current states: */
1528 if (ninst <= MAX_EXEC_INST) {
1529 error = qat_ae_ucode_read(sc, ae, 0, ninst, savucode);
1531 qat_ae_write_4(sc, ae, AE_MISC_CONTROL, misc);
1536 /* save wakeup-events */
1537 qat_ae_ctx_indr_read(sc, ae, ctx, CTX_WAKEUP_EVENTS_INDIRECT,
1540 qat_ae_ctx_indr_read(sc, ae, ctx, CTX_STS_INDIRECT, &savpc);
1543 /* save ctx enables */
1544 qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
1545 ctxen &= CTX_ENABLES_IGNORE_W1C_MASK;
1546 /* save conditional-code */
1547 qat_ae_read_4(sc, ae, CC_ENABLE, &savcc);
1548 /* save current context */
1549 qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS, &savctx);
1550 qat_ae_read_4(sc, ae, CTX_ARB_CNTL, &ctxarbctl);
1552 /* save indirect csrs */
1553 qat_ae_ctx_indr_read(sc, ae, ctx, FUTURE_COUNT_SIGNAL_INDIRECT,
1554 &indr_future_cnt_sig);
1555 qat_ae_ctx_indr_read(sc, ae, ctx, CTX_SIG_EVENTS_INDIRECT, &indr_sig);
1556 qat_ae_read_4(sc, ae, CTX_SIG_EVENTS_ACTIVE, &active_sig);
1558 /* turn off ucode parity */
1559 qat_ae_write_4(sc, ae, CTX_ENABLES,
1560 ctxen & ~CTX_ENABLES_CNTL_STORE_PARITY_ENABLE);
1562 /* copy instructions to ustore */
1563 qat_ae_ucode_write(sc, ae, 0, ninst, ucode);
1565 qat_ae_ctx_indr_write(sc, ae, 1 << ctx, CTX_STS_INDIRECT, 0);
1566 /* change the active context */
1567 qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS,
1568 ctx & ACTIVE_CTX_STATUS_ACNO);
1570 if (cond_code_off) {
1571 /* disable conditional-code*/
1572 qat_ae_write_4(sc, ae, CC_ENABLE, savcc & 0xffffdfff);
1575 /* wakeup-event voluntary */
1576 qat_ae_ctx_indr_write(sc, ae, 1 << ctx,
1577 CTX_WAKEUP_EVENTS_INDIRECT, CTX_WAKEUP_EVENTS_INDIRECT_VOLUNTARY);
1580 qat_ae_ctx_indr_write(sc, ae, 1 << ctx, CTX_SIG_EVENTS_INDIRECT, 0);
1581 qat_ae_write_4(sc, ae, CTX_SIG_EVENTS_ACTIVE, 0);
1583 /* enable context */
1584 qat_ae_enable_ctx(sc, ae, 1 << ctx);
1586 /* wait for it to finish */
1587 if (qat_ae_wait_num_cycles(sc, ae, max_cycles, 1) != 0)
1590 /* see if we need to get the current PC */
1591 if (endpc != NULL) {
1592 uint32_t ctx_status;
1594 qat_ae_ctx_indr_read(sc, ae, ctx, CTX_STS_INDIRECT,
1596 *endpc = ctx_status & UPC_MASK;
1600 uint32_t ctx_status;
1602 qat_ae_ctx_indr_read(sc, ae, ctx, CTX_STS_INDIRECT,
1604 printf("%s: endpc 0x%08x\n", __func__,
1605 ctx_status & UPC_MASK);
1609 /* retore to previous states: */
1610 /* disable context */
1611 qat_ae_disable_ctx(sc, ae, 1 << ctx);
1612 if (ninst <= MAX_EXEC_INST) {
1614 qat_ae_ucode_write(sc, ae, 0, ninst, savucode);
1617 qat_ae_ctx_indr_write(sc, ae, 1 << ctx, CTX_WAKEUP_EVENTS_INDIRECT,
1619 qat_ae_ctx_indr_write(sc, ae, 1 << ctx, CTX_STS_INDIRECT, savpc);
1621 /* only restore shared control store bit,
1622 other bit might be changed by AE code snippet */
1623 qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &misc);
1625 nmisc = misc | AE_MISC_CONTROL_SHARE_CS;
1627 nmisc = misc & ~AE_MISC_CONTROL_SHARE_CS;
1628 qat_ae_write_4(sc, ae, AE_MISC_CONTROL, nmisc);
1629 /* conditional-code */
1630 qat_ae_write_4(sc, ae, CC_ENABLE, savcc);
1631 /* change the active context */
1632 qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS,
1633 savctx & ACTIVE_CTX_STATUS_ACNO);
1634 /* restore the nxt ctx to run */
1635 qat_ae_write_4(sc, ae, CTX_ARB_CNTL, ctxarbctl);
1636 /* restore current LM addr */
1637 qat_ae_ctx_indr_write(sc, ae, 1 << ctx, LM_ADDR_0_INDIRECT,
1639 qat_ae_ctx_indr_write(sc, ae, 1 << ctx, LM_ADDR_1_INDIRECT,
1641 qat_ae_ctx_indr_write(sc, ae, 1 << ctx, INDIRECT_LM_ADDR_0_BYTE_INDEX,
1642 indr_lm_addr_byte_0);
1643 qat_ae_ctx_indr_write(sc, ae, 1 << ctx, INDIRECT_LM_ADDR_1_BYTE_INDEX,
1644 indr_lm_addr_byte_1);
1646 /* restore indirect csrs */
1647 qat_ae_ctx_indr_write(sc, ae, 1 << ctx, FUTURE_COUNT_SIGNAL_INDIRECT,
1648 indr_future_cnt_sig);
1649 qat_ae_ctx_indr_write(sc, ae, 1 << ctx, CTX_SIG_EVENTS_INDIRECT,
1651 qat_ae_write_4(sc, ae, CTX_SIG_EVENTS_ACTIVE, active_sig);
1654 qat_ae_write_4(sc, ae, CTX_ENABLES, ctxen);
1660 qat_ae_exec_ucode_init_lm(struct qat_softc *sc, u_char ae, u_char ctx,
1661 int *first_exec, uint64_t *ucode, u_int ninst,
1662 u_int *gpr_a0, u_int *gpr_a1, u_int *gpr_a2, u_int *gpr_b0, u_int *gpr_b1)
1666 qat_aereg_rel_data_read(sc, ae, ctx, AEREG_GPA_REL, 0, gpr_a0);
1667 qat_aereg_rel_data_read(sc, ae, ctx, AEREG_GPA_REL, 1, gpr_a1);
1668 qat_aereg_rel_data_read(sc, ae, ctx, AEREG_GPA_REL, 2, gpr_a2);
1669 qat_aereg_rel_data_read(sc, ae, ctx, AEREG_GPB_REL, 0, gpr_b0);
1670 qat_aereg_rel_data_read(sc, ae, ctx, AEREG_GPB_REL, 1, gpr_b1);
1674 return qat_ae_exec_ucode(sc, ae, ctx, ucode, ninst, 1, ninst * 5, NULL);
1678 qat_ae_restore_init_lm_gprs(struct qat_softc *sc, u_char ae, u_char ctx,
1679 u_int gpr_a0, u_int gpr_a1, u_int gpr_a2, u_int gpr_b0, u_int gpr_b1)
1681 qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPA_REL, 0, gpr_a0);
1682 qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPA_REL, 1, gpr_a1);
1683 qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPA_REL, 2, gpr_a2);
1684 qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPB_REL, 0, gpr_b0);
1685 qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPB_REL, 1, gpr_b1);
1691 qat_ae_get_inst_num(int lmsize)
1698 left = lmsize % sizeof(u_int);
1701 ninst = nitems(ae_inst_1b) +
1702 qat_ae_get_inst_num(lmsize - left);
1704 /* 3 instruction is needed for further code */
1705 ninst = (lmsize - sizeof(u_int)) * 3 / 4 + nitems(ae_inst_4b);
1712 qat_ae_batch_put_lm(struct qat_softc *sc, u_char ae,
1713 struct qat_ae_batch_init_list *qabi_list, size_t nqabi)
1715 struct qat_ae_batch_init *qabi;
1716 size_t alloc_ninst, ninst;
1718 u_int gpr_a0, gpr_a1, gpr_a2, gpr_b0, gpr_b1;
1719 int insnsz, error = 0, execed = 0, first_exec = 1;
1721 if (STAILQ_FIRST(qabi_list) == NULL)
1724 alloc_ninst = min(USTORE_SIZE, nqabi);
1725 ucode = qat_alloc_mem(sizeof(uint64_t) * alloc_ninst);
1728 STAILQ_FOREACH(qabi, qabi_list, qabi_next) {
1729 insnsz = qat_ae_get_inst_num(qabi->qabi_size);
1730 if (insnsz + ninst > alloc_ninst) {
1731 /* add ctx_arb[kill] */
1732 ucode[ninst++] = 0x0E000010000ull;
1735 error = qat_ae_exec_ucode_init_lm(sc, ae, 0,
1736 &first_exec, ucode, ninst,
1737 &gpr_a0, &gpr_a1, &gpr_a2, &gpr_b0, &gpr_b1);
1739 qat_ae_restore_init_lm_gprs(sc, ae, 0,
1740 gpr_a0, gpr_a1, gpr_a2, gpr_b0, gpr_b1);
1741 qat_free_mem(ucode);
1744 /* run microExec to execute the microcode */
1747 ninst += qat_ae_concat_ucode(ucode, ninst,
1748 qabi->qabi_size, qabi->qabi_addr, qabi->qabi_value);
1752 ucode[ninst++] = 0x0E000010000ull;
1755 error = qat_ae_exec_ucode_init_lm(sc, ae, 0,
1756 &first_exec, ucode, ninst,
1757 &gpr_a0, &gpr_a1, &gpr_a2, &gpr_b0, &gpr_b1);
1760 qat_ae_restore_init_lm_gprs(sc, ae, 0,
1761 gpr_a0, gpr_a1, gpr_a2, gpr_b0, gpr_b1);
1764 qat_free_mem(ucode);
1770 qat_ae_write_pc(struct qat_softc *sc, u_char ae, u_int ctx_mask, u_int upc)
1773 if (qat_ae_is_active(sc, ae))
1776 qat_ae_ctx_indr_write(sc, ae, ctx_mask, CTX_STS_INDIRECT,
1782 qat_aefw_csum_calc(u_int reg, int ch)
1785 u_int topbit = CRC_BITMASK(CRC_WIDTH - 1);
1786 u_int inbyte = (u_int)((reg >> 0x18) ^ ch);
1788 reg ^= inbyte << (CRC_WIDTH - 0x8);
1789 for (i = 0; i < 0x8; i++) {
1791 reg = (reg << 1) ^ CRC_POLY;
1796 return (reg & CRC_WIDTHMASK(CRC_WIDTH));
1800 qat_aefw_csum(char *buf, int size)
1805 csum = qat_aefw_csum_calc(csum, *buf++);
1812 qat_aefw_uof_string(struct qat_softc *sc, size_t offset)
1814 if (offset >= sc->sc_aefw_uof.qafu_str_tab_size)
1816 if (sc->sc_aefw_uof.qafu_str_tab == NULL)
1819 return (const char *)((uintptr_t)sc->sc_aefw_uof.qafu_str_tab + offset);
1822 static struct uof_chunk_hdr *
1823 qat_aefw_uof_find_chunk(struct qat_softc *sc,
1824 const char *id, struct uof_chunk_hdr *cur)
1826 struct uof_obj_hdr *uoh = sc->sc_aefw_uof.qafu_obj_hdr;
1827 struct uof_chunk_hdr *uch;
1830 uch = (struct uof_chunk_hdr *)(uoh + 1);
1831 for (i = 0; i < uoh->uoh_num_chunks; i++, uch++) {
1832 if (uch->uch_offset + uch->uch_size > sc->sc_aefw_uof.qafu_size)
1835 if (cur < uch && !strncmp(uch->uch_id, id, UOF_OBJ_ID_LEN))
1843 qat_aefw_load_mof(struct qat_softc *sc)
1845 const struct firmware *fw;
1847 fw = firmware_get(sc->sc_hw.qhw_mof_fwname);
1849 device_printf(sc->sc_dev, "couldn't load MOF firmware %s\n",
1850 sc->sc_hw.qhw_mof_fwname);
1854 sc->sc_fw_mof = qat_alloc_mem(fw->datasize);
1855 sc->sc_fw_mof_size = fw->datasize;
1856 memcpy(sc->sc_fw_mof, fw->data, fw->datasize);
1857 firmware_put(fw, FIRMWARE_UNLOAD);
1862 qat_aefw_unload_mof(struct qat_softc *sc)
1864 if (sc->sc_fw_mof != NULL) {
1865 qat_free_mem(sc->sc_fw_mof);
1866 sc->sc_fw_mof = NULL;
1871 qat_aefw_load_mmp(struct qat_softc *sc)
1873 const struct firmware *fw;
1875 fw = firmware_get(sc->sc_hw.qhw_mmp_fwname);
1877 device_printf(sc->sc_dev, "couldn't load MOF firmware %s\n",
1878 sc->sc_hw.qhw_mmp_fwname);
1882 sc->sc_fw_mmp = qat_alloc_mem(fw->datasize);
1883 sc->sc_fw_mmp_size = fw->datasize;
1884 memcpy(sc->sc_fw_mmp, fw->data, fw->datasize);
1885 firmware_put(fw, FIRMWARE_UNLOAD);
1890 qat_aefw_unload_mmp(struct qat_softc *sc)
1892 if (sc->sc_fw_mmp != NULL) {
1893 qat_free_mem(sc->sc_fw_mmp);
1894 sc->sc_fw_mmp = NULL;
1899 qat_aefw_mof_find_uof0(struct qat_softc *sc,
1900 struct mof_uof_hdr *muh, struct mof_uof_chunk_hdr *head,
1901 u_int nchunk, size_t size, const char *id,
1902 size_t *fwsize, void **fwptr)
1907 for (i = 0; i < nchunk; i++) {
1908 struct mof_uof_chunk_hdr *much = &head[i];
1910 if (strncmp(much->much_id, id, MOF_OBJ_ID_LEN))
1913 if (much->much_offset + much->much_size > size)
1916 if (sc->sc_mof.qmf_sym_size <= much->much_name)
1919 uof_name = (char *)((uintptr_t)sc->sc_mof.qmf_sym +
1922 if (!strcmp(uof_name, sc->sc_fw_uof_name)) {
1923 *fwptr = (void *)((uintptr_t)muh +
1924 (uintptr_t)much->much_offset);
1925 *fwsize = (size_t)much->much_size;
1934 qat_aefw_mof_find_uof(struct qat_softc *sc)
1936 struct mof_uof_hdr *uof_hdr, *suof_hdr;
1937 u_int nuof_chunks = 0, nsuof_chunks = 0;
1940 uof_hdr = sc->sc_mof.qmf_uof_objs;
1941 suof_hdr = sc->sc_mof.qmf_suof_objs;
1943 if (uof_hdr != NULL) {
1944 if (uof_hdr->muh_max_chunks < uof_hdr->muh_num_chunks) {
1947 nuof_chunks = uof_hdr->muh_num_chunks;
1949 if (suof_hdr != NULL) {
1950 if (suof_hdr->muh_max_chunks < suof_hdr->muh_num_chunks)
1952 nsuof_chunks = suof_hdr->muh_num_chunks;
1955 if (nuof_chunks + nsuof_chunks == 0)
1958 if (uof_hdr != NULL) {
1959 error = qat_aefw_mof_find_uof0(sc, uof_hdr,
1960 (struct mof_uof_chunk_hdr *)(uof_hdr + 1), nuof_chunks,
1961 sc->sc_mof.qmf_uof_objs_size, UOF_IMAG,
1962 &sc->sc_fw_uof_size, &sc->sc_fw_uof);
1963 if (error && error != ENOENT)
1967 if (suof_hdr != NULL) {
1968 error = qat_aefw_mof_find_uof0(sc, suof_hdr,
1969 (struct mof_uof_chunk_hdr *)(suof_hdr + 1), nsuof_chunks,
1970 sc->sc_mof.qmf_suof_objs_size, SUOF_IMAG,
1971 &sc->sc_fw_suof_size, &sc->sc_fw_suof);
1972 if (error && error != ENOENT)
1976 if (sc->sc_fw_uof == NULL && sc->sc_fw_suof == NULL)
1983 qat_aefw_mof_parse(struct qat_softc *sc)
1985 const struct mof_file_hdr *mfh;
1986 const struct mof_file_chunk_hdr *mfch;
1991 size = sc->sc_fw_mof_size;
1993 if (size < sizeof(struct mof_file_hdr))
1995 size -= sizeof(struct mof_file_hdr);
1997 mfh = sc->sc_fw_mof;
1999 if (mfh->mfh_fid != MOF_FID)
2002 csum = qat_aefw_csum((char *)((uintptr_t)sc->sc_fw_mof +
2003 offsetof(struct mof_file_hdr, mfh_min_ver)),
2004 sc->sc_fw_mof_size -
2005 offsetof(struct mof_file_hdr, mfh_min_ver));
2006 if (mfh->mfh_csum != csum)
2009 if (mfh->mfh_min_ver != MOF_MIN_VER ||
2010 mfh->mfh_maj_ver != MOF_MAJ_VER)
2013 if (mfh->mfh_max_chunks < mfh->mfh_num_chunks)
2016 if (size < sizeof(struct mof_file_chunk_hdr) * mfh->mfh_num_chunks)
2018 mfch = (const struct mof_file_chunk_hdr *)(mfh + 1);
2020 for (i = 0; i < mfh->mfh_num_chunks; i++, mfch++) {
2021 if (mfch->mfch_offset + mfch->mfch_size > sc->sc_fw_mof_size)
2024 if (!strncmp(mfch->mfch_id, SYM_OBJS, MOF_OBJ_ID_LEN)) {
2025 if (sc->sc_mof.qmf_sym != NULL)
2028 sc->sc_mof.qmf_sym =
2029 (void *)((uintptr_t)sc->sc_fw_mof +
2030 (uintptr_t)mfch->mfch_offset + sizeof(u_int));
2031 sc->sc_mof.qmf_sym_size =
2032 *(u_int *)((uintptr_t)sc->sc_fw_mof +
2033 (uintptr_t)mfch->mfch_offset);
2035 if (sc->sc_mof.qmf_sym_size % sizeof(u_int) != 0)
2037 if (mfch->mfch_size != sc->sc_mof.qmf_sym_size +
2038 sizeof(u_int) || mfch->mfch_size == 0)
2040 if (*(char *)((uintptr_t)sc->sc_mof.qmf_sym +
2041 sc->sc_mof.qmf_sym_size - 1) != '\0')
2044 } else if (!strncmp(mfch->mfch_id, UOF_OBJS, MOF_OBJ_ID_LEN)) {
2045 if (sc->sc_mof.qmf_uof_objs != NULL)
2048 sc->sc_mof.qmf_uof_objs =
2049 (void *)((uintptr_t)sc->sc_fw_mof +
2050 (uintptr_t)mfch->mfch_offset);
2051 sc->sc_mof.qmf_uof_objs_size = mfch->mfch_size;
2053 } else if (!strncmp(mfch->mfch_id, SUOF_OBJS, MOF_OBJ_ID_LEN)) {
2054 if (sc->sc_mof.qmf_suof_objs != NULL)
2057 sc->sc_mof.qmf_suof_objs =
2058 (void *)((uintptr_t)sc->sc_fw_mof +
2059 (uintptr_t)mfch->mfch_offset);
2060 sc->sc_mof.qmf_suof_objs_size = mfch->mfch_size;
2064 if (sc->sc_mof.qmf_sym == NULL ||
2065 (sc->sc_mof.qmf_uof_objs == NULL &&
2066 sc->sc_mof.qmf_suof_objs == NULL))
2069 error = qat_aefw_mof_find_uof(sc);
2076 qat_aefw_uof_parse_image(struct qat_softc *sc,
2077 struct qat_uof_image *qui, struct uof_chunk_hdr *uch)
2079 struct uof_image *image;
2080 struct uof_code_page *page;
2081 uintptr_t base = (uintptr_t)sc->sc_aefw_uof.qafu_obj_hdr;
2082 size_t lim = uch->uch_offset + uch->uch_size, size;
2085 size = uch->uch_size;
2086 if (size < sizeof(struct uof_image))
2088 size -= sizeof(struct uof_image);
2090 qui->qui_image = image =
2091 (struct uof_image *)(base + uch->uch_offset);
2093 #define ASSIGN_OBJ_TAB(np, typep, type, base, off, lim) \
2096 nent = ((struct uof_obj_table *)((base) + (off)))->uot_nentries;\
2097 if ((lim) < off + sizeof(struct uof_obj_table) + \
2098 sizeof(type) * nent) \
2102 *(typep) = (type)((struct uof_obj_table *) \
2103 ((base) + (off)) + 1); \
2108 ASSIGN_OBJ_TAB(&qui->qui_num_ae_reg, &qui->qui_ae_reg,
2109 struct uof_ae_reg *, base, image->ui_reg_tab, lim);
2110 ASSIGN_OBJ_TAB(&qui->qui_num_init_reg_sym, &qui->qui_init_reg_sym,
2111 struct uof_init_reg_sym *, base, image->ui_init_reg_sym_tab, lim);
2112 ASSIGN_OBJ_TAB(&qui->qui_num_sbreak, &qui->qui_sbreak,
2113 struct qui_sbreak *, base, image->ui_sbreak_tab, lim);
2115 if (size < sizeof(struct uof_code_page) * image->ui_num_pages)
2117 if (nitems(qui->qui_pages) < image->ui_num_pages)
2120 page = (struct uof_code_page *)(image + 1);
2122 for (p = 0; p < image->ui_num_pages; p++, page++) {
2123 struct qat_uof_page *qup = &qui->qui_pages[p];
2124 struct uof_code_area *uca;
2126 qup->qup_page_num = page->ucp_page_num;
2127 qup->qup_def_page = page->ucp_def_page;
2128 qup->qup_page_region = page->ucp_page_region;
2129 qup->qup_beg_vaddr = page->ucp_beg_vaddr;
2130 qup->qup_beg_paddr = page->ucp_beg_paddr;
2132 ASSIGN_OBJ_TAB(&qup->qup_num_uc_var, &qup->qup_uc_var,
2133 struct uof_uword_fixup *, base,
2134 page->ucp_uc_var_tab, lim);
2135 ASSIGN_OBJ_TAB(&qup->qup_num_imp_var, &qup->qup_imp_var,
2136 struct uof_import_var *, base,
2137 page->ucp_imp_var_tab, lim);
2138 ASSIGN_OBJ_TAB(&qup->qup_num_imp_expr, &qup->qup_imp_expr,
2139 struct uof_uword_fixup *, base,
2140 page->ucp_imp_expr_tab, lim);
2141 ASSIGN_OBJ_TAB(&qup->qup_num_neigh_reg, &qup->qup_neigh_reg,
2142 struct uof_uword_fixup *, base,
2143 page->ucp_neigh_reg_tab, lim);
2145 if (lim < page->ucp_code_area + sizeof(struct uof_code_area))
2148 uca = (struct uof_code_area *)(base + page->ucp_code_area);
2149 qup->qup_num_micro_words = uca->uca_num_micro_words;
2151 ASSIGN_OBJ_TAB(&qup->qup_num_uw_blocks, &qup->qup_uw_blocks,
2152 struct qat_uof_uword_block *, base,
2153 uca->uca_uword_block_tab, lim);
2155 for (i = 0; i < qup->qup_num_uw_blocks; i++) {
2156 u_int uwordoff = ((struct uof_uword_block *)(
2157 &qup->qup_uw_blocks[i]))->uub_uword_offset;
2162 qup->qup_uw_blocks[i].quub_micro_words =
2167 #undef ASSIGN_OBJ_TAB
2173 qat_aefw_uof_parse_images(struct qat_softc *sc)
2175 struct uof_chunk_hdr *uch = NULL;
2179 for (i = 0; i < MAX_NUM_AE * MAX_AE_CTX; i++) {
2180 uch = qat_aefw_uof_find_chunk(sc, UOF_IMAG, uch);
2184 if (i >= nitems(sc->sc_aefw_uof.qafu_imgs))
2187 error = qat_aefw_uof_parse_image(sc, &sc->sc_aefw_uof.qafu_imgs[i], uch);
2191 sc->sc_aefw_uof.qafu_num_imgs++;
2195 for (i = 0; i < sc->sc_aefw_uof.qafu_num_imgs; i++) {
2196 assigned_ae |= sc->sc_aefw_uof.qafu_imgs[i].qui_image->ui_ae_assigned;
2203 qat_aefw_uof_parse(struct qat_softc *sc)
2205 struct uof_file_hdr *ufh;
2206 struct uof_file_chunk_hdr *ufch;
2207 struct uof_obj_hdr *uoh;
2208 struct uof_chunk_hdr *uch;
2210 size_t size, uof_size, hdr_size;
2215 size = sc->sc_fw_uof_size;
2216 if (size < MIN_UOF_SIZE)
2218 size -= sizeof(struct uof_file_hdr);
2220 ufh = sc->sc_fw_uof;
2222 if (ufh->ufh_id != UOF_FID)
2224 if (ufh->ufh_min_ver != UOF_MIN_VER || ufh->ufh_maj_ver != UOF_MAJ_VER)
2227 if (ufh->ufh_max_chunks < ufh->ufh_num_chunks)
2229 if (size < sizeof(struct uof_file_chunk_hdr) * ufh->ufh_num_chunks)
2231 ufch = (struct uof_file_chunk_hdr *)(ufh + 1);
2234 for (i = 0; i < ufh->ufh_num_chunks; i++, ufch++) {
2235 if (ufch->ufch_offset + ufch->ufch_size > sc->sc_fw_uof_size)
2238 if (!strncmp(ufch->ufch_id, UOF_OBJS, UOF_OBJ_ID_LEN)) {
2243 (void *)((uintptr_t)sc->sc_fw_uof +
2245 uof_size = ufch->ufch_size;
2247 csum = qat_aefw_csum(uof, uof_size);
2248 if (csum != ufch->ufch_csum)
2257 if (size < sizeof(struct uof_obj_hdr))
2259 size -= sizeof(struct uof_obj_hdr);
2263 if (size < sizeof(struct uof_chunk_hdr) * uoh->uoh_num_chunks)
2266 /* Check if the UOF objects are compatible with the chip */
2267 if ((uoh->uoh_cpu_type & sc->sc_hw.qhw_prod_type) == 0)
2270 if (uoh->uoh_min_cpu_ver > sc->sc_rev ||
2271 uoh->uoh_max_cpu_ver < sc->sc_rev)
2274 sc->sc_aefw_uof.qafu_size = uof_size;
2275 sc->sc_aefw_uof.qafu_obj_hdr = uoh;
2277 base = (uintptr_t)sc->sc_aefw_uof.qafu_obj_hdr;
2279 /* map uof string-table */
2280 uch = qat_aefw_uof_find_chunk(sc, UOF_STRT, NULL);
2282 hdr_size = offsetof(struct uof_str_tab, ust_strings);
2283 sc->sc_aefw_uof.qafu_str_tab =
2284 (void *)(base + uch->uch_offset + hdr_size);
2285 sc->sc_aefw_uof.qafu_str_tab_size = uch->uch_size - hdr_size;
2288 /* get ustore mem inits table -- should be only one */
2289 uch = qat_aefw_uof_find_chunk(sc, UOF_IMEM, NULL);
2291 if (uch->uch_size < sizeof(struct uof_obj_table))
2293 sc->sc_aefw_uof.qafu_num_init_mem = ((struct uof_obj_table *)(base +
2294 uch->uch_offset))->uot_nentries;
2295 if (sc->sc_aefw_uof.qafu_num_init_mem) {
2296 sc->sc_aefw_uof.qafu_init_mem =
2297 (struct uof_init_mem *)(base + uch->uch_offset +
2298 sizeof(struct uof_obj_table));
2299 sc->sc_aefw_uof.qafu_init_mem_size =
2300 uch->uch_size - sizeof(struct uof_obj_table);
2304 uch = qat_aefw_uof_find_chunk(sc, UOF_MSEG, NULL);
2306 if (uch->uch_size < sizeof(struct uof_obj_table) +
2307 sizeof(struct uof_var_mem_seg))
2309 sc->sc_aefw_uof.qafu_var_mem_seg =
2310 (struct uof_var_mem_seg *)(base + uch->uch_offset +
2311 sizeof(struct uof_obj_table));
2314 return qat_aefw_uof_parse_images(sc);
2318 qat_aefw_suof_parse_image(struct qat_softc *sc, struct qat_suof_image *qsi,
2319 struct suof_chunk_hdr *sch)
2321 struct qat_aefw_suof *qafs = &sc->sc_aefw_suof;
2322 struct simg_ae_mode *ae_mode;
2325 qsi->qsi_simg_buf = qafs->qafs_suof_buf + sch->sch_offset +
2326 sizeof(struct suof_obj_hdr);
2328 ((struct suof_obj_hdr *)
2329 (qafs->qafs_suof_buf + sch->sch_offset))->soh_img_length;
2331 qsi->qsi_css_header = qsi->qsi_simg_buf;
2332 qsi->qsi_css_key = qsi->qsi_css_header + sizeof(struct css_hdr);
2333 qsi->qsi_css_signature = qsi->qsi_css_key +
2334 CSS_FWSK_MODULUS_LEN + CSS_FWSK_EXPONENT_LEN;
2335 qsi->qsi_css_simg = qsi->qsi_css_signature + CSS_SIGNATURE_LEN;
2337 ae_mode = (struct simg_ae_mode *)qsi->qsi_css_simg;
2338 qsi->qsi_ae_mask = ae_mode->sam_ae_mask;
2339 qsi->qsi_simg_name = (u_long)&ae_mode->sam_simg_name;
2340 qsi->qsi_appmeta_data = (u_long)&ae_mode->sam_appmeta_data;
2341 qsi->qsi_fw_type = ae_mode->sam_fw_type;
2343 if (ae_mode->sam_dev_type != sc->sc_hw.qhw_prod_type)
2346 maj_ver = (QAT_PID_MAJOR_REV | (sc->sc_rev & QAT_PID_MINOR_REV)) & 0xff;
2347 if ((maj_ver > ae_mode->sam_devmax_ver) ||
2348 (maj_ver < ae_mode->sam_devmin_ver)) {
2356 qat_aefw_suof_parse(struct qat_softc *sc)
2358 struct suof_file_hdr *sfh;
2359 struct suof_chunk_hdr *sch;
2360 struct qat_aefw_suof *qafs = &sc->sc_aefw_suof;
2361 struct qat_suof_image *qsi;
2364 int ae0_img = MAX_AE;
2367 size = sc->sc_fw_suof_size;
2368 if (size < sizeof(struct suof_file_hdr))
2371 sfh = sc->sc_fw_suof;
2373 if (sfh->sfh_file_id != SUOF_FID)
2375 if (sfh->sfh_fw_type != 0)
2377 if (sfh->sfh_num_chunks <= 1)
2379 if (sfh->sfh_min_ver != SUOF_MIN_VER ||
2380 sfh->sfh_maj_ver != SUOF_MAJ_VER)
2383 csum = qat_aefw_csum((char *)&sfh->sfh_min_ver,
2384 size - offsetof(struct suof_file_hdr, sfh_min_ver));
2385 if (csum != sfh->sfh_check_sum)
2388 size -= sizeof(struct suof_file_hdr);
2390 qafs->qafs_file_id = SUOF_FID;
2391 qafs->qafs_suof_buf = sc->sc_fw_suof;
2392 qafs->qafs_suof_size = sc->sc_fw_suof_size;
2393 qafs->qafs_check_sum = sfh->sfh_check_sum;
2394 qafs->qafs_min_ver = sfh->sfh_min_ver;
2395 qafs->qafs_maj_ver = sfh->sfh_maj_ver;
2396 qafs->qafs_fw_type = sfh->sfh_fw_type;
2398 if (size < sizeof(struct suof_chunk_hdr))
2400 sch = (struct suof_chunk_hdr *)(sfh + 1);
2401 size -= sizeof(struct suof_chunk_hdr);
2403 if (size < sizeof(struct suof_str_tab))
2405 size -= offsetof(struct suof_str_tab, sst_strings);
2407 qafs->qafs_sym_size = ((struct suof_str_tab *)
2408 (qafs->qafs_suof_buf + sch->sch_offset))->sst_tab_length;
2409 if (size < qafs->qafs_sym_size)
2411 qafs->qafs_sym_str = qafs->qafs_suof_buf + sch->sch_offset +
2412 offsetof(struct suof_str_tab, sst_strings);
2414 qafs->qafs_num_simgs = sfh->sfh_num_chunks - 1;
2415 if (qafs->qafs_num_simgs == 0)
2418 qsi = qat_alloc_mem(
2419 sizeof(struct qat_suof_image) * qafs->qafs_num_simgs);
2420 qafs->qafs_simg = qsi;
2422 for (i = 0; i < qafs->qafs_num_simgs; i++) {
2423 error = qat_aefw_suof_parse_image(sc, &qsi[i], &sch[i + 1]);
2426 if ((qsi[i].qsi_ae_mask & 0x1) != 0)
2430 if (ae0_img != qafs->qafs_num_simgs - 1) {
2431 struct qat_suof_image last_qsi;
2433 memcpy(&last_qsi, &qsi[qafs->qafs_num_simgs - 1],
2434 sizeof(struct qat_suof_image));
2435 memcpy(&qsi[qafs->qafs_num_simgs - 1], &qsi[ae0_img],
2436 sizeof(struct qat_suof_image));
2437 memcpy(&qsi[ae0_img], &last_qsi,
2438 sizeof(struct qat_suof_image));
2445 qat_aefw_alloc_auth_dmamem(struct qat_softc *sc, char *image, size_t size,
2446 struct qat_dmamem *dma)
2448 struct css_hdr *css = (struct css_hdr *)image;
2449 struct auth_chunk *auth_chunk;
2450 struct fw_auth_desc *auth_desc;
2451 size_t mapsize, simg_offset = sizeof(struct auth_chunk);
2452 bus_size_t bus_addr;
2453 uintptr_t virt_addr;
2456 if (size > AE_IMG_OFFSET + CSS_MAX_IMAGE_LEN)
2459 mapsize = (css->css_fw_type == CSS_AE_FIRMWARE) ?
2460 CSS_AE_SIMG_LEN + simg_offset :
2461 size + CSS_FWSK_PAD_LEN + simg_offset;
2462 error = qat_alloc_dmamem(sc, dma, 1, mapsize, PAGE_SIZE);
2466 memset(dma->qdm_dma_vaddr, 0, mapsize);
2468 auth_chunk = dma->qdm_dma_vaddr;
2469 auth_chunk->ac_chunk_size = mapsize;
2470 auth_chunk->ac_chunk_bus_addr = dma->qdm_dma_seg.ds_addr;
2472 virt_addr = (uintptr_t)dma->qdm_dma_vaddr;
2473 virt_addr += simg_offset;
2474 bus_addr = auth_chunk->ac_chunk_bus_addr;
2475 bus_addr += simg_offset;
2477 auth_desc = &auth_chunk->ac_fw_auth_desc;
2478 auth_desc->fad_css_hdr_high = (uint64_t)bus_addr >> 32;
2479 auth_desc->fad_css_hdr_low = bus_addr;
2481 memcpy((void *)virt_addr, image, sizeof(struct css_hdr));
2483 virt_addr += sizeof(struct css_hdr);
2484 bus_addr += sizeof(struct css_hdr);
2485 image += sizeof(struct css_hdr);
2487 auth_desc->fad_fwsk_pub_high = (uint64_t)bus_addr >> 32;
2488 auth_desc->fad_fwsk_pub_low = bus_addr;
2490 memcpy((void *)virt_addr, image, CSS_FWSK_MODULUS_LEN);
2491 memset((void *)(virt_addr + CSS_FWSK_MODULUS_LEN), 0, CSS_FWSK_PAD_LEN);
2492 memcpy((void *)(virt_addr + CSS_FWSK_MODULUS_LEN + CSS_FWSK_PAD_LEN),
2493 image + CSS_FWSK_MODULUS_LEN, sizeof(uint32_t));
2495 virt_addr += CSS_FWSK_PUB_LEN;
2496 bus_addr += CSS_FWSK_PUB_LEN;
2497 image += CSS_FWSK_MODULUS_LEN + CSS_FWSK_EXPONENT_LEN;
2499 auth_desc->fad_signature_high = (uint64_t)bus_addr >> 32;
2500 auth_desc->fad_signature_low = bus_addr;
2502 memcpy((void *)virt_addr, image, CSS_SIGNATURE_LEN);
2504 virt_addr += CSS_SIGNATURE_LEN;
2505 bus_addr += CSS_SIGNATURE_LEN;
2506 image += CSS_SIGNATURE_LEN;
2508 auth_desc->fad_img_high = (uint64_t)bus_addr >> 32;
2509 auth_desc->fad_img_low = bus_addr;
2510 auth_desc->fad_img_len = size - AE_IMG_OFFSET;
2512 memcpy((void *)virt_addr, image, auth_desc->fad_img_len);
2514 if (css->css_fw_type == CSS_AE_FIRMWARE) {
2515 auth_desc->fad_img_ae_mode_data_high = auth_desc->fad_img_high;
2516 auth_desc->fad_img_ae_mode_data_low = auth_desc->fad_img_low;
2518 bus_addr += sizeof(struct simg_ae_mode);
2520 auth_desc->fad_img_ae_init_data_high = (uint64_t)bus_addr >> 32;
2521 auth_desc->fad_img_ae_init_data_low = bus_addr;
2523 bus_addr += SIMG_AE_INIT_SEQ_LEN;
2525 auth_desc->fad_img_ae_insts_high = (uint64_t)bus_addr >> 32;
2526 auth_desc->fad_img_ae_insts_low = bus_addr;
2528 auth_desc->fad_img_ae_insts_high = auth_desc->fad_img_high;
2529 auth_desc->fad_img_ae_insts_low = auth_desc->fad_img_low;
2532 bus_dmamap_sync(dma->qdm_dma_tag, dma->qdm_dma_map,
2533 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2539 qat_aefw_auth(struct qat_softc *sc, struct qat_dmamem *dma)
2545 addr = dma->qdm_dma_seg.ds_addr;
2546 qat_cap_global_write_4(sc, FCU_DRAM_ADDR_HI, (uint64_t)addr >> 32);
2547 qat_cap_global_write_4(sc, FCU_DRAM_ADDR_LO, addr);
2548 qat_cap_global_write_4(sc, FCU_CTRL, FCU_CTRL_CMD_AUTH);
2551 DELAY(FW_AUTH_WAIT_PERIOD * 1000);
2552 fcu = qat_cap_global_read_4(sc, FCU_STATUS);
2553 sts = __SHIFTOUT(fcu, FCU_STATUS_STS);
2554 if (sts == FCU_STATUS_STS_VERI_FAIL)
2556 if (fcu & FCU_STATUS_AUTHFWLD &&
2557 sts == FCU_STATUS_STS_VERI_DONE) {
2560 } while (retry++ < FW_AUTH_MAX_RETRY);
2563 device_printf(sc->sc_dev,
2564 "firmware authentication error: status 0x%08x retry %d\n",
2570 qat_aefw_suof_load(struct qat_softc *sc, struct qat_dmamem *dma)
2572 struct simg_ae_mode *ae_mode;
2573 uint32_t fcu, sts, loaded;
2578 ae_mode = (struct simg_ae_mode *)((uintptr_t)dma->qdm_dma_vaddr +
2579 sizeof(struct auth_chunk) + sizeof(struct css_hdr) +
2580 CSS_FWSK_PUB_LEN + CSS_SIGNATURE_LEN);
2582 for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
2585 if (!((ae_mode->sam_ae_mask >> ae) & 0x1))
2587 if (qat_ae_is_active(sc, ae)) {
2588 device_printf(sc->sc_dev, "AE %d is active\n", ae);
2591 qat_cap_global_write_4(sc, FCU_CTRL,
2592 FCU_CTRL_CMD_LOAD | __SHIFTIN(ae, FCU_CTRL_AE));
2594 DELAY(FW_AUTH_WAIT_PERIOD * 1000);
2595 fcu = qat_cap_global_read_4(sc, FCU_STATUS);
2596 sts = __SHIFTOUT(fcu, FCU_STATUS_STS);
2597 loaded = __SHIFTOUT(fcu, FCU_STATUS_LOADED_AE);
2598 if (sts == FCU_STATUS_STS_LOAD_DONE &&
2599 (loaded & (1 << ae))) {
2602 } while (retry++ < FW_AUTH_MAX_RETRY);
2604 if (retry > FW_AUTH_MAX_RETRY) {
2605 device_printf(sc->sc_dev,
2606 "firmware load timeout: status %08x\n", fcu);
2615 qat_aefw_suof_write(struct qat_softc *sc)
2617 struct qat_suof_image *qsi;
2620 for (i = 0; i < sc->sc_aefw_suof.qafs_num_simgs; i++) {
2621 qsi = &sc->sc_aefw_suof.qafs_simg[i];
2622 error = qat_aefw_alloc_auth_dmamem(sc, qsi->qsi_simg_buf,
2623 qsi->qsi_simg_len, &qsi->qsi_dma);
2626 error = qat_aefw_auth(sc, &qsi->qsi_dma);
2628 qat_free_dmamem(sc, &qsi->qsi_dma);
2631 error = qat_aefw_suof_load(sc, &qsi->qsi_dma);
2633 qat_free_dmamem(sc, &qsi->qsi_dma);
2636 qat_free_dmamem(sc, &qsi->qsi_dma);
2638 qat_free_mem(sc->sc_aefw_suof.qafs_simg);
2644 qat_aefw_uof_assign_image(struct qat_softc *sc, struct qat_ae *qae,
2645 struct qat_uof_image *qui)
2647 struct qat_ae_slice *slice;
2648 int i, npages, nregions;
2650 if (qae->qae_num_slices >= nitems(qae->qae_slices))
2653 if (qui->qui_image->ui_ae_mode &
2654 (AE_MODE_RELOAD_CTX_SHARED | AE_MODE_SHARED_USTORE)) {
2656 device_printf(sc->sc_dev,
2657 "shared ae mode is not supported yet\n");
2661 qae->qae_shareable_ustore = 0; /* XXX */
2662 qae->qae_effect_ustore_size = USTORE_SIZE;
2664 slice = &qae->qae_slices[qae->qae_num_slices];
2666 slice->qas_image = qui;
2667 slice->qas_assigned_ctx_mask = qui->qui_image->ui_ctx_assigned;
2669 nregions = qui->qui_image->ui_num_page_regions;
2670 npages = qui->qui_image->ui_num_pages;
2672 if (nregions > nitems(slice->qas_regions))
2674 if (npages > nitems(slice->qas_pages))
2677 for (i = 0; i < nregions; i++) {
2678 STAILQ_INIT(&slice->qas_regions[i].qar_waiting_pages);
2680 for (i = 0; i < npages; i++) {
2681 struct qat_ae_page *page = &slice->qas_pages[i];
2684 page->qap_page = &qui->qui_pages[i];
2685 region = page->qap_page->qup_page_region;
2686 if (region >= nregions)
2689 page->qap_region = &slice->qas_regions[region];
2692 qae->qae_num_slices++;
2698 qat_aefw_uof_init_ae(struct qat_softc *sc, u_char ae)
2700 struct uof_image *image;
2701 struct qat_ae *qae = &(QAT_AE(sc, ae));
2705 for (s = 0; s < qae->qae_num_slices; s++) {
2706 if (qae->qae_slices[s].qas_image == NULL)
2709 image = qae->qae_slices[s].qas_image->qui_image;
2710 qat_ae_write_ctx_mode(sc, ae,
2711 __SHIFTOUT(image->ui_ae_mode, AE_MODE_CTX_MODE));
2713 nn_mode = __SHIFTOUT(image->ui_ae_mode, AE_MODE_NN_MODE);
2714 if (nn_mode != AE_MODE_NN_MODE_DONTCARE)
2715 qat_ae_write_nn_mode(sc, ae, nn_mode);
2717 qat_ae_write_lm_mode(sc, ae, AEREG_LMEM0,
2718 __SHIFTOUT(image->ui_ae_mode, AE_MODE_LMEM0));
2719 qat_ae_write_lm_mode(sc, ae, AEREG_LMEM1,
2720 __SHIFTOUT(image->ui_ae_mode, AE_MODE_LMEM1));
2722 qat_ae_write_shared_cs_mode(sc, ae,
2723 __SHIFTOUT(image->ui_ae_mode, AE_MODE_SHARED_USTORE));
2724 qat_ae_set_reload_ustore(sc, ae, image->ui_reloadable_size,
2725 __SHIFTOUT(image->ui_ae_mode, AE_MODE_RELOAD_CTX_SHARED),
2726 qae->qae_reloc_ustore_dram);
2733 qat_aefw_uof_init(struct qat_softc *sc)
2738 for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
2744 qae = &(QAT_AE(sc, ae));
2746 for (i = 0; i < sc->sc_aefw_uof.qafu_num_imgs; i++) {
2747 if ((sc->sc_aefw_uof.qafu_imgs[i].qui_image->ui_ae_assigned &
2751 error = qat_aefw_uof_assign_image(sc, qae,
2752 &sc->sc_aefw_uof.qafu_imgs[i]);
2757 /* XXX UcLo_initNumUwordUsed */
2759 qae->qae_reloc_ustore_dram = UINT_MAX; /* XXX */
2761 error = qat_aefw_uof_init_ae(sc, ae);
2770 qat_aefw_load(struct qat_softc *sc)
2774 error = qat_aefw_load_mof(sc);
2778 error = qat_aefw_load_mmp(sc);
2782 error = qat_aefw_mof_parse(sc);
2784 device_printf(sc->sc_dev, "couldn't parse mof: %d\n", error);
2788 if (sc->sc_hw.qhw_fw_auth) {
2789 error = qat_aefw_suof_parse(sc);
2791 device_printf(sc->sc_dev, "couldn't parse suof: %d\n",
2796 error = qat_aefw_suof_write(sc);
2798 device_printf(sc->sc_dev,
2799 "could not write firmware: %d\n", error);
2804 error = qat_aefw_uof_parse(sc);
2806 device_printf(sc->sc_dev, "couldn't parse uof: %d\n",
2811 error = qat_aefw_uof_init(sc);
2813 device_printf(sc->sc_dev,
2814 "couldn't init for aefw: %d\n", error);
2818 error = qat_aefw_uof_write(sc);
2820 device_printf(sc->sc_dev,
2821 "Could not write firmware: %d\n", error);
2830 qat_aefw_unload(struct qat_softc *sc)
2832 qat_aefw_unload_mmp(sc);
2833 qat_aefw_unload_mof(sc);
2837 qat_aefw_start(struct qat_softc *sc, u_char ae, u_int ctx_mask)
2842 if (sc->sc_hw.qhw_fw_auth) {
2843 qat_cap_global_write_4(sc, FCU_CTRL, FCU_CTRL_CMD_START);
2845 DELAY(FW_AUTH_WAIT_PERIOD * 1000);
2846 fcu = qat_cap_global_read_4(sc, FCU_STATUS);
2847 if (fcu & FCU_STATUS_DONE)
2849 } while (retry++ < FW_AUTH_MAX_RETRY);
2851 device_printf(sc->sc_dev,
2852 "firmware start timeout: status %08x\n", fcu);
2855 qat_ae_ctx_indr_write(sc, ae, (~ctx_mask) & AE_ALL_CTX,
2856 CTX_WAKEUP_EVENTS_INDIRECT,
2857 CTX_WAKEUP_EVENTS_INDIRECT_SLEEP);
2858 qat_ae_enable_ctx(sc, ae, ctx_mask);
2865 qat_aefw_init_memory_one(struct qat_softc *sc, struct uof_init_mem *uim)
2867 struct qat_aefw_uof *qafu = &sc->sc_aefw_uof;
2868 struct qat_ae_batch_init_list *qabi_list;
2869 struct uof_mem_val_attr *memattr;
2876 memattr = (struct uof_mem_val_attr *)(uim + 1);
2878 switch (uim->uim_region) {
2880 if ((uim->uim_addr + uim->uim_num_bytes) > MAX_LMEM_REG * 4) {
2881 device_printf(sc->sc_dev,
2882 "Invalid lmem addr or bytes\n");
2885 if (uim->uim_scope != UOF_SCOPE_LOCAL)
2887 sym = qat_aefw_uof_string(sc, uim->uim_sym_name);
2888 ael = strtoul(sym, &ep, 10);
2889 if (ep == sym || ael > MAX_AE)
2891 if ((sc->sc_ae_mask & (1 << ael)) == 0)
2892 return 0; /* ae is fused out */
2894 curinit = &qafu->qafu_num_lm_init[ael];
2895 qabi_list = &qafu->qafu_lm_init[ael];
2897 for (i = 0; i < uim->uim_num_val_attr; i++, memattr++) {
2898 struct qat_ae_batch_init *qabi;
2900 qabi = qat_alloc_mem(sizeof(struct qat_ae_batch_init));
2902 STAILQ_INIT(qabi_list);
2903 STAILQ_INSERT_TAIL(qabi_list, qabi, qabi_next);
2905 qabi->qabi_ae = (u_int)ael;
2907 uim->uim_addr + memattr->umva_byte_offset;
2908 qabi->qabi_value = &memattr->umva_value;
2909 qabi->qabi_size = 4;
2910 qafu->qafu_num_lm_init_inst[ael] +=
2911 qat_ae_get_inst_num(qabi->qabi_size);
2913 if (*curinit >= MAX_LMEM_REG) {
2914 device_printf(sc->sc_dev,
2915 "Invalid lmem val attr\n");
2923 case SCRATCH_REGION:
2928 device_printf(sc->sc_dev,
2929 "unsupported memory region to init: %d\n",
2938 qat_aefw_free_lm_init(struct qat_softc *sc, u_char ae)
2940 struct qat_aefw_uof *qafu = &sc->sc_aefw_uof;
2941 struct qat_ae_batch_init *qabi;
2943 while ((qabi = STAILQ_FIRST(&qafu->qafu_lm_init[ae])) != NULL) {
2944 STAILQ_REMOVE_HEAD(&qafu->qafu_lm_init[ae], qabi_next);
2948 qafu->qafu_num_lm_init[ae] = 0;
2949 qafu->qafu_num_lm_init_inst[ae] = 0;
2953 qat_aefw_init_ustore(struct qat_softc *sc)
2959 int usz, end, start;
2962 fill = qat_alloc_mem(MAX_USTORE * sizeof(uint64_t));
2964 for (a = 0; a < sc->sc_aefw_uof.qafu_num_imgs; a++) {
2965 struct qat_uof_image *qui = &sc->sc_aefw_uof.qafu_imgs[a];
2966 struct uof_image *ui = qui->qui_image;
2968 for (i = 0; i < MAX_USTORE; i++)
2969 memcpy(&fill[i], ui->ui_fill_pattern, sizeof(uint64_t));
2971 * Compute do_not_init value as a value that will not be equal
2972 * to fill data when cast to an int
2975 if (dont_init == (uint32_t)fill[0])
2976 dont_init = 0xffffffff;
2978 for (p = 0; p < ui->ui_num_pages; p++) {
2979 struct qat_uof_page *qup = &qui->qui_pages[p];
2980 if (!qup->qup_def_page)
2983 for (i = qup->qup_beg_paddr;
2984 i < qup->qup_beg_paddr + qup->qup_num_micro_words;
2986 fill[i] = (uint64_t)dont_init;
2990 for (ae = 0; ae < sc->sc_ae_num; ae++) {
2991 MPASS(ae < UOF_MAX_NUM_OF_AE);
2992 if ((ui->ui_ae_assigned & (1 << ae)) == 0)
2995 if (QAT_AE(sc, ae).qae_shareable_ustore && (ae & 1)) {
2996 qat_ae_get_shared_ustore_ae(ae, &nae);
2997 if (ui->ui_ae_assigned & (1 << ae))
3000 usz = QAT_AE(sc, ae).qae_effect_ustore_size;
3002 /* initialize the areas not going to be overwritten */
3005 /* find next uword that needs to be initialized */
3006 for (start = end + 1; start < usz; start++) {
3007 if ((uint32_t)fill[start] != dont_init)
3010 /* see if there are no more such uwords */
3013 for (end = start + 1; end < usz; end++) {
3014 if ((uint32_t)fill[end] == dont_init)
3017 if (QAT_AE(sc, ae).qae_shareable_ustore) {
3018 error = ENOTSUP; /* XXX */
3021 error = qat_ae_ucode_write(sc, ae,
3022 start, end - start, &fill[start]);
3028 } while (end < usz);
3038 qat_aefw_init_reg(struct qat_softc *sc, u_char ae, u_char ctx_mask,
3039 enum aereg_type regtype, u_short regaddr, u_int value)
3048 case AEREG_SR_RD_REL:
3049 case AEREG_SR_WR_REL:
3051 case AEREG_DR_RD_REL:
3052 case AEREG_DR_WR_REL:
3053 case AEREG_NEIGH_REL:
3054 /* init for all valid ctx */
3055 for (ctx = 0; ctx < MAX_AE_CTX; ctx++) {
3056 if ((ctx_mask & (1 << ctx)) == 0)
3058 error = qat_aereg_rel_data_write(sc, ae, ctx, regtype,
3065 case AEREG_SR_RD_ABS:
3066 case AEREG_SR_WR_ABS:
3068 case AEREG_DR_RD_ABS:
3069 case AEREG_DR_WR_ABS:
3070 error = qat_aereg_abs_data_write(sc, ae, regtype,
3082 qat_aefw_init_reg_sym_expr(struct qat_softc *sc, u_char ae,
3083 struct qat_uof_image *qui)
3088 for (i = 0; i < qui->qui_num_init_reg_sym; i++) {
3089 struct uof_init_reg_sym *uirs = &qui->qui_init_reg_sym[i];
3091 if (uirs->uirs_value_type == EXPR_VAL) {
3093 device_printf(sc->sc_dev,
3094 "does not support initializing EXPR_VAL\n");
3097 expres = uirs->uirs_value;
3100 switch (uirs->uirs_init_type) {
3102 if (__SHIFTOUT(qui->qui_image->ui_ae_mode,
3103 AE_MODE_CTX_MODE) == MAX_AE_CTX) {
3104 ctx_mask = 0xff; /* 8-ctx mode */
3106 ctx_mask = 0x55; /* 4-ctx mode */
3108 qat_aefw_init_reg(sc, ae, ctx_mask,
3109 (enum aereg_type)uirs->uirs_reg_type,
3110 (u_short)uirs->uirs_addr_offset, expres);
3113 if (__SHIFTOUT(qui->qui_image->ui_ae_mode,
3114 AE_MODE_CTX_MODE) == MAX_AE_CTX) {
3115 ctx_mask = 0xff; /* 8-ctx mode */
3117 ctx_mask = 0x55; /* 4-ctx mode */
3119 if (((1 << uirs->uirs_ctx) & ctx_mask) == 0)
3121 qat_aefw_init_reg(sc, ae, 1 << uirs->uirs_ctx,
3122 (enum aereg_type)uirs->uirs_reg_type,
3123 (u_short)uirs->uirs_addr_offset, expres);
3126 case INIT_EXPR_ENDIAN_SWAP:
3128 device_printf(sc->sc_dev,
3129 "does not support initializing init_type %d\n",
3130 uirs->uirs_init_type);
3139 qat_aefw_init_memory(struct qat_softc *sc)
3141 struct qat_aefw_uof *qafu = &sc->sc_aefw_uof;
3142 size_t uimsz, initmemsz = qafu->qafu_init_mem_size;
3143 struct uof_init_mem *uim;
3147 uim = qafu->qafu_init_mem;
3148 for (i = 0; i < qafu->qafu_num_init_mem; i++) {
3149 uimsz = sizeof(struct uof_init_mem) +
3150 sizeof(struct uof_mem_val_attr) * uim->uim_num_val_attr;
3151 if (uimsz > initmemsz) {
3152 device_printf(sc->sc_dev,
3153 "invalid uof_init_mem or uof_mem_val_attr size\n");
3157 if (uim->uim_num_bytes > 0) {
3158 error = qat_aefw_init_memory_one(sc, uim);
3160 device_printf(sc->sc_dev,
3161 "Could not init ae memory: %d\n", error);
3165 uim = (struct uof_init_mem *)((uintptr_t)uim + uimsz);
3169 /* run Batch put LM API */
3170 for (ae = 0; ae < MAX_AE; ae++) {
3171 error = qat_ae_batch_put_lm(sc, ae, &qafu->qafu_lm_init[ae],
3172 qafu->qafu_num_lm_init_inst[ae]);
3174 device_printf(sc->sc_dev, "Could not put lm\n");
3176 qat_aefw_free_lm_init(sc, ae);
3179 error = qat_aefw_init_ustore(sc);
3181 /* XXX run Batch put LM API */
3187 qat_aefw_init_globals(struct qat_softc *sc)
3189 struct qat_aefw_uof *qafu = &sc->sc_aefw_uof;
3193 /* initialize the memory segments */
3194 if (qafu->qafu_num_init_mem > 0) {
3195 error = qat_aefw_init_memory(sc);
3199 error = qat_aefw_init_ustore(sc);
3204 /* XXX bind import variables with ivd values */
3206 /* XXX bind the uC global variables
3207 * local variables will done on-the-fly */
3208 for (i = 0; i < sc->sc_aefw_uof.qafu_num_imgs; i++) {
3209 for (p = 0; p < sc->sc_aefw_uof.qafu_imgs[i].qui_image->ui_num_pages; p++) {
3210 struct qat_uof_page *qup =
3211 &sc->sc_aefw_uof.qafu_imgs[i].qui_pages[p];
3212 if (qup->qup_num_uw_blocks &&
3213 (qup->qup_num_uc_var || qup->qup_num_imp_var)) {
3214 device_printf(sc->sc_dev,
3215 "not support uC global variables\n");
3221 for (ae = 0; ae < sc->sc_ae_num; ae++) {
3222 struct qat_ae *qae = &(QAT_AE(sc, ae));
3224 for (s = 0; s < qae->qae_num_slices; s++) {
3225 struct qat_ae_slice *qas = &qae->qae_slices[s];
3227 if (qas->qas_image == NULL)
3231 qat_aefw_init_reg_sym_expr(sc, ae, qas->qas_image);
3241 qat_aefw_get_uof_inst(struct qat_softc *sc, struct qat_uof_page *qup,
3247 /* find the block */
3248 for (i = 0; i < qup->qup_num_uw_blocks; i++) {
3249 struct qat_uof_uword_block *quub = &qup->qup_uw_blocks[i];
3251 if ((addr >= quub->quub_start_addr) &&
3252 (addr <= (quub->quub_start_addr +
3253 (quub->quub_num_words - 1)))) {
3254 /* unpack n bytes and assigned to the 64-bit uword value.
3255 note: the microwords are stored as packed bytes.
3257 addr -= quub->quub_start_addr;
3258 addr *= AEV2_PACKED_UWORD_BYTES;
3260 (void *)((uintptr_t)quub->quub_micro_words + addr),
3261 AEV2_PACKED_UWORD_BYTES);
3262 uinst = uinst & UWORD_MASK;
3272 qat_aefw_do_pagein(struct qat_softc *sc, u_char ae, struct qat_uof_page *qup)
3274 struct qat_ae *qae = &(QAT_AE(sc, ae));
3275 uint64_t fill, *ucode_cpybuf;
3276 u_int error, i, upaddr, uraddr, ninst, cpylen;
3278 if (qup->qup_num_uc_var || qup->qup_num_neigh_reg ||
3279 qup->qup_num_imp_var || qup->qup_num_imp_expr) {
3280 device_printf(sc->sc_dev,
3281 "does not support fixup locals\n");
3285 ucode_cpybuf = qat_alloc_mem(UWORD_CPYBUF_SIZE * sizeof(uint64_t));
3287 /* XXX get fill-pattern from an image -- they are all the same */
3288 memcpy(&fill, sc->sc_aefw_uof.qafu_imgs[0].qui_image->ui_fill_pattern,
3291 upaddr = qup->qup_beg_paddr;
3293 ninst = qup->qup_num_micro_words;
3295 cpylen = min(ninst, UWORD_CPYBUF_SIZE);
3297 /* load the buffer */
3298 for (i = 0; i < cpylen; i++) {
3299 /* keep below code structure in case there are
3300 * different handling for shared secnarios */
3301 if (!qae->qae_shareable_ustore) {
3302 /* qat_aefw_get_uof_inst() takes an address that
3303 * is relative to the start of the page.
3304 * So we don't need to add in the physical
3305 * offset of the page. */
3306 if (qup->qup_page_region != 0) {
3308 device_printf(sc->sc_dev,
3309 "region != 0 is not supported\n");
3310 qat_free_mem(ucode_cpybuf);
3313 /* for mixing case, it should take
3314 * physical address */
3315 ucode_cpybuf[i] = qat_aefw_get_uof_inst(
3316 sc, qup, upaddr + i);
3317 if (ucode_cpybuf[i] == INVLD_UWORD) {
3318 /* fill hole in the uof */
3319 ucode_cpybuf[i] = fill;
3324 qat_free_mem(ucode_cpybuf);
3329 /* copy the buffer to ustore */
3330 if (!qae->qae_shareable_ustore) {
3331 error = qat_ae_ucode_write(sc, ae, upaddr, cpylen,
3337 qat_free_mem(ucode_cpybuf);
3345 qat_free_mem(ucode_cpybuf);
3351 qat_aefw_uof_write_one(struct qat_softc *sc, struct qat_uof_image *qui)
3353 struct uof_image *ui = qui->qui_image;
3354 struct qat_ae_page *qap;
3357 u_char ae, ctx_mask;
3359 if (__SHIFTOUT(ui->ui_ae_mode, AE_MODE_CTX_MODE) == MAX_AE_CTX)
3360 ctx_mask = 0xff; /* 8-ctx mode */
3362 ctx_mask = 0x55; /* 4-ctx mode */
3364 /* load the default page and set assigned CTX PC
3365 * to the entrypoint address */
3366 for (ae = 0; ae < sc->sc_ae_num; ae++) {
3367 struct qat_ae *qae = &(QAT_AE(sc, ae));
3368 struct qat_ae_slice *qas;
3371 MPASS(ae < UOF_MAX_NUM_OF_AE);
3373 if ((ui->ui_ae_assigned & (1 << ae)) == 0)
3376 /* find the slice to which this image is assigned */
3377 for (s = 0; s < qae->qae_num_slices; s++) {
3378 qas = &qae->qae_slices[s];
3379 if (ui->ui_ctx_assigned & qas->qas_assigned_ctx_mask)
3382 if (s >= qae->qae_num_slices)
3385 qas = &qae->qae_slices[s];
3387 for (p = 0; p < ui->ui_num_pages; p++) {
3388 qap = &qas->qas_pages[p];
3390 /* Only load pages loaded by default */
3391 if (!qap->qap_page->qup_def_page)
3394 error = qat_aefw_do_pagein(sc, ae, qap->qap_page);
3399 metadata = qas->qas_image->qui_image->ui_app_metadata;
3400 if (metadata != 0xffffffff && bootverbose) {
3401 device_printf(sc->sc_dev,
3402 "loaded firmware: %s\n",
3403 qat_aefw_uof_string(sc, metadata));
3406 /* Assume starting page is page 0 */
3407 qap = &qas->qas_pages[0];
3408 for (c = 0; c < MAX_AE_CTX; c++) {
3409 if (ctx_mask & (1 << c))
3410 qas->qas_cur_pages[c] = qap;
3412 qas->qas_cur_pages[c] = NULL;
3415 /* set the live context */
3416 qae->qae_live_ctx_mask = ui->ui_ctx_assigned;
3418 /* set context PC to the image entrypoint address */
3419 error = qat_ae_write_pc(sc, ae, ui->ui_ctx_assigned,
3420 ui->ui_entry_address);
3425 /* XXX store the checksum for convenience */
3431 qat_aefw_uof_write(struct qat_softc *sc)
3436 error = qat_aefw_init_globals(sc);
3438 device_printf(sc->sc_dev,
3439 "Could not initialize globals\n");
3443 for (i = 0; i < sc->sc_aefw_uof.qafu_num_imgs; i++) {
3444 error = qat_aefw_uof_write_one(sc,
3445 &sc->sc_aefw_uof.qafu_imgs[i]);
3450 /* XXX UcLo_computeFreeUstore */