xref: /freebsd/sys/dev/qat_c2xxx/qat_ae.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1*b61a5730SWarner Losh /* SPDX-License-Identifier: BSD-2-Clause AND BSD-3-Clause */
2f4f56ff4SMark Johnston /*	$NetBSD: qat_ae.c,v 1.1 2019/11/20 09:37:46 hikaru Exp $	*/
3f4f56ff4SMark Johnston 
4f4f56ff4SMark Johnston /*
5f4f56ff4SMark Johnston  * Copyright (c) 2019 Internet Initiative Japan, Inc.
6f4f56ff4SMark Johnston  * All rights reserved.
7f4f56ff4SMark Johnston  *
8f4f56ff4SMark Johnston  * Redistribution and use in source and binary forms, with or without
9f4f56ff4SMark Johnston  * modification, are permitted provided that the following conditions
10f4f56ff4SMark Johnston  * are met:
11f4f56ff4SMark Johnston  * 1. Redistributions of source code must retain the above copyright
12f4f56ff4SMark Johnston  *    notice, this list of conditions and the following disclaimer.
13f4f56ff4SMark Johnston  * 2. Redistributions in binary form must reproduce the above copyright
14f4f56ff4SMark Johnston  *    notice, this list of conditions and the following disclaimer in the
15f4f56ff4SMark Johnston  *    documentation and/or other materials provided with the distribution.
16f4f56ff4SMark Johnston  *
17f4f56ff4SMark Johnston  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18f4f56ff4SMark Johnston  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19f4f56ff4SMark Johnston  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20f4f56ff4SMark Johnston  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21f4f56ff4SMark Johnston  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22f4f56ff4SMark Johnston  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23f4f56ff4SMark Johnston  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24f4f56ff4SMark Johnston  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25f4f56ff4SMark Johnston  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26f4f56ff4SMark Johnston  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27f4f56ff4SMark Johnston  * POSSIBILITY OF SUCH DAMAGE.
28f4f56ff4SMark Johnston  */
29f4f56ff4SMark Johnston 
30f4f56ff4SMark Johnston /*
31f4f56ff4SMark Johnston  *   Copyright(c) 2007-2019 Intel Corporation. All rights reserved.
32f4f56ff4SMark Johnston  *
33f4f56ff4SMark Johnston  *   Redistribution and use in source and binary forms, with or without
34f4f56ff4SMark Johnston  *   modification, are permitted provided that the following conditions
35f4f56ff4SMark Johnston  *   are met:
36f4f56ff4SMark Johnston  *
37f4f56ff4SMark Johnston  *     * Redistributions of source code must retain the above copyright
38f4f56ff4SMark Johnston  *       notice, this list of conditions and the following disclaimer.
39f4f56ff4SMark Johnston  *     * Redistributions in binary form must reproduce the above copyright
40f4f56ff4SMark Johnston  *       notice, this list of conditions and the following disclaimer in
41f4f56ff4SMark Johnston  *       the documentation and/or other materials provided with the
42f4f56ff4SMark Johnston  *       distribution.
43f4f56ff4SMark Johnston  *     * Neither the name of Intel Corporation nor the names of its
44f4f56ff4SMark Johnston  *       contributors may be used to endorse or promote products derived
45f4f56ff4SMark Johnston  *       from this software without specific prior written permission.
46f4f56ff4SMark Johnston  *
47f4f56ff4SMark Johnston  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
48f4f56ff4SMark Johnston  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
49f4f56ff4SMark Johnston  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
50f4f56ff4SMark Johnston  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
51f4f56ff4SMark Johnston  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52f4f56ff4SMark Johnston  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
53f4f56ff4SMark Johnston  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54f4f56ff4SMark Johnston  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55f4f56ff4SMark Johnston  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56f4f56ff4SMark Johnston  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
57f4f56ff4SMark Johnston  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58f4f56ff4SMark Johnston  */
59f4f56ff4SMark Johnston 
60f4f56ff4SMark Johnston #include <sys/cdefs.h>
61f4f56ff4SMark Johnston #if 0
62f4f56ff4SMark Johnston __KERNEL_RCSID(0, "$NetBSD: qat_ae.c,v 1.1 2019/11/20 09:37:46 hikaru Exp $");
63f4f56ff4SMark Johnston #endif
64f4f56ff4SMark Johnston 
65f4f56ff4SMark Johnston #include <sys/param.h>
66f4f56ff4SMark Johnston #include <sys/bus.h>
67f4f56ff4SMark Johnston #include <sys/firmware.h>
68f4f56ff4SMark Johnston #include <sys/limits.h>
69f4f56ff4SMark Johnston #include <sys/systm.h>
70f4f56ff4SMark Johnston 
71f4f56ff4SMark Johnston #include <machine/bus.h>
72f4f56ff4SMark Johnston 
73f4f56ff4SMark Johnston #include <dev/pci/pcireg.h>
74f4f56ff4SMark Johnston #include <dev/pci/pcivar.h>
75f4f56ff4SMark Johnston 
76f4f56ff4SMark Johnston #include "qatreg.h"
77f4f56ff4SMark Johnston #include "qatvar.h"
78f4f56ff4SMark Johnston #include "qat_aevar.h"
79f4f56ff4SMark Johnston 
80f4f56ff4SMark Johnston static int	qat_ae_write_4(struct qat_softc *, u_char, bus_size_t,
81f4f56ff4SMark Johnston 		    uint32_t);
82f4f56ff4SMark Johnston static int	qat_ae_read_4(struct qat_softc *, u_char, bus_size_t,
83f4f56ff4SMark Johnston 		    uint32_t *);
84f4f56ff4SMark Johnston static void	qat_ae_ctx_indr_write(struct qat_softc *, u_char, uint32_t,
85f4f56ff4SMark Johnston 		    bus_size_t, uint32_t);
86f4f56ff4SMark Johnston static int	qat_ae_ctx_indr_read(struct qat_softc *, u_char, uint32_t,
87f4f56ff4SMark Johnston 		    bus_size_t, uint32_t *);
88f4f56ff4SMark Johnston 
89f4f56ff4SMark Johnston static u_short	qat_aereg_get_10bit_addr(enum aereg_type, u_short);
90f4f56ff4SMark Johnston static int	qat_aereg_rel_data_write(struct qat_softc *, u_char, u_char,
91f4f56ff4SMark Johnston 		    enum aereg_type, u_short, uint32_t);
92f4f56ff4SMark Johnston static int	qat_aereg_rel_data_read(struct qat_softc *, u_char, u_char,
93f4f56ff4SMark Johnston 		    enum aereg_type, u_short, uint32_t *);
94f4f56ff4SMark Johnston static int	qat_aereg_rel_rdxfer_write(struct qat_softc *, u_char, u_char,
95f4f56ff4SMark Johnston 		    enum aereg_type, u_short, uint32_t);
96f4f56ff4SMark Johnston static int	qat_aereg_rel_wrxfer_write(struct qat_softc *, u_char, u_char,
97f4f56ff4SMark Johnston 		    enum aereg_type, u_short, uint32_t);
98f4f56ff4SMark Johnston static int	qat_aereg_rel_nn_write(struct qat_softc *, u_char, u_char,
99f4f56ff4SMark Johnston 		    enum aereg_type, u_short, uint32_t);
100f4f56ff4SMark Johnston static int	qat_aereg_abs_to_rel(struct qat_softc *, u_char, u_short,
101f4f56ff4SMark Johnston 		    u_short *, u_char *);
102f4f56ff4SMark Johnston static int	qat_aereg_abs_data_write(struct qat_softc *, u_char,
103f4f56ff4SMark Johnston 		    enum aereg_type, u_short, uint32_t);
104f4f56ff4SMark Johnston 
105f4f56ff4SMark Johnston static void	qat_ae_enable_ctx(struct qat_softc *, u_char, u_int);
106f4f56ff4SMark Johnston static void	qat_ae_disable_ctx(struct qat_softc *, u_char, u_int);
107f4f56ff4SMark Johnston static void	qat_ae_write_ctx_mode(struct qat_softc *, u_char, u_char);
108f4f56ff4SMark Johnston static void	qat_ae_write_nn_mode(struct qat_softc *, u_char, u_char);
109f4f56ff4SMark Johnston static void	qat_ae_write_lm_mode(struct qat_softc *, u_char,
110f4f56ff4SMark Johnston 		    enum aereg_type, u_char);
111f4f56ff4SMark Johnston static void	qat_ae_write_shared_cs_mode0(struct qat_softc *, u_char,
112f4f56ff4SMark Johnston 		    u_char);
113f4f56ff4SMark Johnston static void	qat_ae_write_shared_cs_mode(struct qat_softc *, u_char, u_char);
114f4f56ff4SMark Johnston static int	qat_ae_set_reload_ustore(struct qat_softc *, u_char, u_int, int,
115f4f56ff4SMark Johnston 		    u_int);
116f4f56ff4SMark Johnston 
117f4f56ff4SMark Johnston static enum qat_ae_status qat_ae_get_status(struct qat_softc *, u_char);
118f4f56ff4SMark Johnston static int	qat_ae_is_active(struct qat_softc *, u_char);
119f4f56ff4SMark Johnston static int	qat_ae_wait_num_cycles(struct qat_softc *, u_char, int, int);
120f4f56ff4SMark Johnston 
121f4f56ff4SMark Johnston static int	qat_ae_clear_reset(struct qat_softc *);
122f4f56ff4SMark Johnston static int	qat_ae_check(struct qat_softc *);
123f4f56ff4SMark Johnston static int	qat_ae_reset_timestamp(struct qat_softc *);
124f4f56ff4SMark Johnston static void	qat_ae_clear_xfer(struct qat_softc *);
125f4f56ff4SMark Johnston static int	qat_ae_clear_gprs(struct qat_softc *);
126f4f56ff4SMark Johnston 
127f4f56ff4SMark Johnston static void	qat_ae_get_shared_ustore_ae(u_char, u_char *);
128f4f56ff4SMark Johnston static u_int	qat_ae_ucode_parity64(uint64_t);
129f4f56ff4SMark Johnston static uint64_t	qat_ae_ucode_set_ecc(uint64_t);
130f4f56ff4SMark Johnston static int	qat_ae_ucode_write(struct qat_softc *, u_char, u_int, u_int,
131f4f56ff4SMark Johnston 		    const uint64_t *);
132f4f56ff4SMark Johnston static int	qat_ae_ucode_read(struct qat_softc *, u_char, u_int, u_int,
133f4f56ff4SMark Johnston 		    uint64_t *);
134f4f56ff4SMark Johnston static u_int	qat_ae_concat_ucode(uint64_t *, u_int, u_int, u_int, u_int *);
135f4f56ff4SMark Johnston static int	qat_ae_exec_ucode(struct qat_softc *, u_char, u_char,
136f4f56ff4SMark Johnston 		    uint64_t *, u_int, int, u_int, u_int *);
137f4f56ff4SMark Johnston static int	qat_ae_exec_ucode_init_lm(struct qat_softc *, u_char, u_char,
138f4f56ff4SMark Johnston 		    int *, uint64_t *, u_int,
139f4f56ff4SMark Johnston 		    u_int *, u_int *, u_int *, u_int *, u_int *);
140f4f56ff4SMark Johnston static int	qat_ae_restore_init_lm_gprs(struct qat_softc *, u_char, u_char,
141f4f56ff4SMark Johnston 		    u_int, u_int, u_int, u_int, u_int);
142f4f56ff4SMark Johnston static int	qat_ae_get_inst_num(int);
143f4f56ff4SMark Johnston static int	qat_ae_batch_put_lm(struct qat_softc *, u_char,
144f4f56ff4SMark Johnston 		    struct qat_ae_batch_init_list *, size_t);
145f4f56ff4SMark Johnston static int	qat_ae_write_pc(struct qat_softc *, u_char, u_int, u_int);
146f4f56ff4SMark Johnston 
147f4f56ff4SMark Johnston static u_int	qat_aefw_csum(char *, int);
148f4f56ff4SMark Johnston static const char *qat_aefw_uof_string(struct qat_softc *, size_t);
149f4f56ff4SMark Johnston static struct uof_chunk_hdr *qat_aefw_uof_find_chunk(struct qat_softc *,
150f4f56ff4SMark Johnston 		    const char *, struct uof_chunk_hdr *);
151f4f56ff4SMark Johnston 
152f4f56ff4SMark Johnston static int	qat_aefw_load_mof(struct qat_softc *);
153f4f56ff4SMark Johnston static void	qat_aefw_unload_mof(struct qat_softc *);
154f4f56ff4SMark Johnston static int	qat_aefw_load_mmp(struct qat_softc *);
155f4f56ff4SMark Johnston static void	qat_aefw_unload_mmp(struct qat_softc *);
156f4f56ff4SMark Johnston 
157f4f56ff4SMark Johnston static int	qat_aefw_mof_find_uof0(struct qat_softc *,
158f4f56ff4SMark Johnston 		    struct mof_uof_hdr *, struct mof_uof_chunk_hdr *,
159f4f56ff4SMark Johnston 		    u_int, size_t, const char *,
160f4f56ff4SMark Johnston 		    size_t *, void **);
161f4f56ff4SMark Johnston static int	qat_aefw_mof_find_uof(struct qat_softc *);
162f4f56ff4SMark Johnston static int	qat_aefw_mof_parse(struct qat_softc *);
163f4f56ff4SMark Johnston 
164f4f56ff4SMark Johnston static int	qat_aefw_uof_parse_image(struct qat_softc *,
165f4f56ff4SMark Johnston 		    struct qat_uof_image *, struct uof_chunk_hdr *uch);
166f4f56ff4SMark Johnston static int	qat_aefw_uof_parse_images(struct qat_softc *);
167f4f56ff4SMark Johnston static int	qat_aefw_uof_parse(struct qat_softc *);
168f4f56ff4SMark Johnston 
169f4f56ff4SMark Johnston static int	qat_aefw_alloc_auth_dmamem(struct qat_softc *, char *, size_t,
170f4f56ff4SMark Johnston 		    struct qat_dmamem *);
171f4f56ff4SMark Johnston static int	qat_aefw_auth(struct qat_softc *, struct qat_dmamem *);
172f4f56ff4SMark Johnston static int	qat_aefw_suof_load(struct qat_softc *sc,
173f4f56ff4SMark Johnston 		    struct qat_dmamem *dma);
174f4f56ff4SMark Johnston static int	qat_aefw_suof_parse_image(struct qat_softc *,
175f4f56ff4SMark Johnston 		    struct qat_suof_image *, struct suof_chunk_hdr *);
176f4f56ff4SMark Johnston static int	qat_aefw_suof_parse(struct qat_softc *);
177f4f56ff4SMark Johnston static int	qat_aefw_suof_write(struct qat_softc *);
178f4f56ff4SMark Johnston 
179f4f56ff4SMark Johnston static int	qat_aefw_uof_assign_image(struct qat_softc *, struct qat_ae *,
180f4f56ff4SMark Johnston 		    struct qat_uof_image *);
181f4f56ff4SMark Johnston static int	qat_aefw_uof_init_ae(struct qat_softc *, u_char);
182f4f56ff4SMark Johnston static int	qat_aefw_uof_init(struct qat_softc *);
183f4f56ff4SMark Johnston 
184f4f56ff4SMark Johnston static int	qat_aefw_init_memory_one(struct qat_softc *,
185f4f56ff4SMark Johnston 		    struct uof_init_mem *);
186f4f56ff4SMark Johnston static void	qat_aefw_free_lm_init(struct qat_softc *, u_char);
187f4f56ff4SMark Johnston static int	qat_aefw_init_ustore(struct qat_softc *);
188f4f56ff4SMark Johnston static int	qat_aefw_init_reg(struct qat_softc *, u_char, u_char,
189f4f56ff4SMark Johnston 		    enum aereg_type, u_short, u_int);
190f4f56ff4SMark Johnston static int	qat_aefw_init_reg_sym_expr(struct qat_softc *, u_char,
191f4f56ff4SMark Johnston 		    struct qat_uof_image *);
192f4f56ff4SMark Johnston static int	qat_aefw_init_memory(struct qat_softc *);
193f4f56ff4SMark Johnston static int	qat_aefw_init_globals(struct qat_softc *);
194f4f56ff4SMark Johnston static uint64_t	qat_aefw_get_uof_inst(struct qat_softc *,
195f4f56ff4SMark Johnston 		    struct qat_uof_page *, u_int);
196f4f56ff4SMark Johnston static int	qat_aefw_do_pagein(struct qat_softc *, u_char,
197f4f56ff4SMark Johnston 		    struct qat_uof_page *);
198f4f56ff4SMark Johnston static int	qat_aefw_uof_write_one(struct qat_softc *,
199f4f56ff4SMark Johnston 		    struct qat_uof_image *);
200f4f56ff4SMark Johnston static int	qat_aefw_uof_write(struct qat_softc *);
201f4f56ff4SMark Johnston 
202f4f56ff4SMark Johnston static int
qat_ae_write_4(struct qat_softc * sc,u_char ae,bus_size_t offset,uint32_t value)203f4f56ff4SMark Johnston qat_ae_write_4(struct qat_softc *sc, u_char ae, bus_size_t offset,
204f4f56ff4SMark Johnston 	uint32_t value)
205f4f56ff4SMark Johnston {
206f4f56ff4SMark Johnston 	int times = TIMEOUT_AE_CSR;
207f4f56ff4SMark Johnston 
208f4f56ff4SMark Johnston 	do {
209f4f56ff4SMark Johnston 		qat_ae_local_write_4(sc, ae, offset, value);
210f4f56ff4SMark Johnston 		if ((qat_ae_local_read_4(sc, ae, LOCAL_CSR_STATUS) &
211f4f56ff4SMark Johnston 		    LOCAL_CSR_STATUS_STATUS) == 0)
212f4f56ff4SMark Johnston 			return 0;
213f4f56ff4SMark Johnston 
214f4f56ff4SMark Johnston 	} while (times--);
215f4f56ff4SMark Johnston 
216f4f56ff4SMark Johnston 	device_printf(sc->sc_dev,
217f4f56ff4SMark Johnston 	    "couldn't write AE CSR: ae 0x%hhx offset 0x%lx\n", ae, (long)offset);
218f4f56ff4SMark Johnston 	return EFAULT;
219f4f56ff4SMark Johnston }
220f4f56ff4SMark Johnston 
221f4f56ff4SMark Johnston static int
qat_ae_read_4(struct qat_softc * sc,u_char ae,bus_size_t offset,uint32_t * value)222f4f56ff4SMark Johnston qat_ae_read_4(struct qat_softc *sc, u_char ae, bus_size_t offset,
223f4f56ff4SMark Johnston 	uint32_t *value)
224f4f56ff4SMark Johnston {
225f4f56ff4SMark Johnston 	int times = TIMEOUT_AE_CSR;
226f4f56ff4SMark Johnston 	uint32_t v;
227f4f56ff4SMark Johnston 
228f4f56ff4SMark Johnston 	do {
229f4f56ff4SMark Johnston 		v = qat_ae_local_read_4(sc, ae, offset);
230f4f56ff4SMark Johnston 		if ((qat_ae_local_read_4(sc, ae, LOCAL_CSR_STATUS) &
231f4f56ff4SMark Johnston 		    LOCAL_CSR_STATUS_STATUS) == 0) {
232f4f56ff4SMark Johnston 			*value = v;
233f4f56ff4SMark Johnston 			return 0;
234f4f56ff4SMark Johnston 		}
235f4f56ff4SMark Johnston 	} while (times--);
236f4f56ff4SMark Johnston 
237f4f56ff4SMark Johnston 	device_printf(sc->sc_dev,
238f4f56ff4SMark Johnston 	    "couldn't read AE CSR: ae 0x%hhx offset 0x%lx\n", ae, (long)offset);
239f4f56ff4SMark Johnston 	return EFAULT;
240f4f56ff4SMark Johnston }
241f4f56ff4SMark Johnston 
242f4f56ff4SMark Johnston static void
qat_ae_ctx_indr_write(struct qat_softc * sc,u_char ae,uint32_t ctx_mask,bus_size_t offset,uint32_t value)243f4f56ff4SMark Johnston qat_ae_ctx_indr_write(struct qat_softc *sc, u_char ae, uint32_t ctx_mask,
244f4f56ff4SMark Johnston     bus_size_t offset, uint32_t value)
245f4f56ff4SMark Johnston {
246f4f56ff4SMark Johnston 	int ctx;
247f4f56ff4SMark Johnston 	uint32_t ctxptr;
248f4f56ff4SMark Johnston 
249f4f56ff4SMark Johnston 	MPASS(offset == CTX_FUTURE_COUNT_INDIRECT ||
250f4f56ff4SMark Johnston 	    offset == FUTURE_COUNT_SIGNAL_INDIRECT ||
251f4f56ff4SMark Johnston 	    offset == CTX_STS_INDIRECT ||
252f4f56ff4SMark Johnston 	    offset == CTX_WAKEUP_EVENTS_INDIRECT ||
253f4f56ff4SMark Johnston 	    offset == CTX_SIG_EVENTS_INDIRECT ||
254f4f56ff4SMark Johnston 	    offset == LM_ADDR_0_INDIRECT ||
255f4f56ff4SMark Johnston 	    offset == LM_ADDR_1_INDIRECT ||
256f4f56ff4SMark Johnston 	    offset == INDIRECT_LM_ADDR_0_BYTE_INDEX ||
257f4f56ff4SMark Johnston 	    offset == INDIRECT_LM_ADDR_1_BYTE_INDEX);
258f4f56ff4SMark Johnston 
259f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CSR_CTX_POINTER, &ctxptr);
260f4f56ff4SMark Johnston 	for (ctx = 0; ctx < MAX_AE_CTX; ctx++) {
261f4f56ff4SMark Johnston 		if ((ctx_mask & (1 << ctx)) == 0)
262f4f56ff4SMark Johnston 			continue;
263f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CSR_CTX_POINTER, ctx);
264f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, offset, value);
265f4f56ff4SMark Johnston 	}
266f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, CSR_CTX_POINTER, ctxptr);
267f4f56ff4SMark Johnston }
268f4f56ff4SMark Johnston 
269f4f56ff4SMark Johnston static int
qat_ae_ctx_indr_read(struct qat_softc * sc,u_char ae,uint32_t ctx,bus_size_t offset,uint32_t * value)270f4f56ff4SMark Johnston qat_ae_ctx_indr_read(struct qat_softc *sc, u_char ae, uint32_t ctx,
271f4f56ff4SMark Johnston     bus_size_t offset, uint32_t *value)
272f4f56ff4SMark Johnston {
273f4f56ff4SMark Johnston 	int error;
274f4f56ff4SMark Johnston 	uint32_t ctxptr;
275f4f56ff4SMark Johnston 
276f4f56ff4SMark Johnston 	MPASS(offset == CTX_FUTURE_COUNT_INDIRECT ||
277f4f56ff4SMark Johnston 	    offset == FUTURE_COUNT_SIGNAL_INDIRECT ||
278f4f56ff4SMark Johnston 	    offset == CTX_STS_INDIRECT ||
279f4f56ff4SMark Johnston 	    offset == CTX_WAKEUP_EVENTS_INDIRECT ||
280f4f56ff4SMark Johnston 	    offset == CTX_SIG_EVENTS_INDIRECT ||
281f4f56ff4SMark Johnston 	    offset == LM_ADDR_0_INDIRECT ||
282f4f56ff4SMark Johnston 	    offset == LM_ADDR_1_INDIRECT ||
283f4f56ff4SMark Johnston 	    offset == INDIRECT_LM_ADDR_0_BYTE_INDEX ||
284f4f56ff4SMark Johnston 	    offset == INDIRECT_LM_ADDR_1_BYTE_INDEX);
285f4f56ff4SMark Johnston 
286f4f56ff4SMark Johnston 	/* save the ctx ptr */
287f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CSR_CTX_POINTER, &ctxptr);
288f4f56ff4SMark Johnston 	if ((ctxptr & CSR_CTX_POINTER_CONTEXT) !=
289f4f56ff4SMark Johnston 	    (ctx & CSR_CTX_POINTER_CONTEXT))
290f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CSR_CTX_POINTER, ctx);
291f4f56ff4SMark Johnston 
292f4f56ff4SMark Johnston 	error = qat_ae_read_4(sc, ae, offset, value);
293f4f56ff4SMark Johnston 
294f4f56ff4SMark Johnston 	/* restore ctx ptr */
295f4f56ff4SMark Johnston 	if ((ctxptr & CSR_CTX_POINTER_CONTEXT) !=
296f4f56ff4SMark Johnston 	    (ctx & CSR_CTX_POINTER_CONTEXT))
297f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CSR_CTX_POINTER, ctxptr);
298f4f56ff4SMark Johnston 
299f4f56ff4SMark Johnston 	return error;
300f4f56ff4SMark Johnston }
301f4f56ff4SMark Johnston 
302f4f56ff4SMark Johnston static u_short
qat_aereg_get_10bit_addr(enum aereg_type regtype,u_short reg)303f4f56ff4SMark Johnston qat_aereg_get_10bit_addr(enum aereg_type regtype, u_short reg)
304f4f56ff4SMark Johnston {
305f4f56ff4SMark Johnston 	u_short addr;
306f4f56ff4SMark Johnston 
307f4f56ff4SMark Johnston 	switch (regtype) {
308f4f56ff4SMark Johnston 	case AEREG_GPA_ABS:
309f4f56ff4SMark Johnston 	case AEREG_GPB_ABS:
310f4f56ff4SMark Johnston 		addr = (reg & 0x7f) | 0x80;
311f4f56ff4SMark Johnston 		break;
312f4f56ff4SMark Johnston 	case AEREG_GPA_REL:
313f4f56ff4SMark Johnston 	case AEREG_GPB_REL:
314f4f56ff4SMark Johnston 		addr = reg & 0x1f;
315f4f56ff4SMark Johnston 		break;
316f4f56ff4SMark Johnston 	case AEREG_SR_RD_REL:
317f4f56ff4SMark Johnston 	case AEREG_SR_WR_REL:
318f4f56ff4SMark Johnston 	case AEREG_SR_REL:
319f4f56ff4SMark Johnston 		addr = 0x180 | (reg & 0x1f);
320f4f56ff4SMark Johnston 		break;
321f4f56ff4SMark Johnston 	case AEREG_SR_INDX:
322f4f56ff4SMark Johnston 		addr = 0x140 | ((reg & 0x3) << 1);
323f4f56ff4SMark Johnston 		break;
324f4f56ff4SMark Johnston 	case AEREG_DR_RD_REL:
325f4f56ff4SMark Johnston 	case AEREG_DR_WR_REL:
326f4f56ff4SMark Johnston 	case AEREG_DR_REL:
327f4f56ff4SMark Johnston 		addr = 0x1c0 | (reg & 0x1f);
328f4f56ff4SMark Johnston 		break;
329f4f56ff4SMark Johnston 	case AEREG_DR_INDX:
330f4f56ff4SMark Johnston 		addr = 0x100 | ((reg & 0x3) << 1);
331f4f56ff4SMark Johnston 		break;
332f4f56ff4SMark Johnston 	case AEREG_NEIGH_INDX:
333f4f56ff4SMark Johnston 		addr = 0x241 | ((reg & 0x3) << 1);
334f4f56ff4SMark Johnston 		break;
335f4f56ff4SMark Johnston 	case AEREG_NEIGH_REL:
336f4f56ff4SMark Johnston 		addr = 0x280 | (reg & 0x1f);
337f4f56ff4SMark Johnston 		break;
338f4f56ff4SMark Johnston 	case AEREG_LMEM0:
339f4f56ff4SMark Johnston 		addr = 0x200;
340f4f56ff4SMark Johnston 		break;
341f4f56ff4SMark Johnston 	case AEREG_LMEM1:
342f4f56ff4SMark Johnston 		addr = 0x220;
343f4f56ff4SMark Johnston 		break;
344f4f56ff4SMark Johnston 	case AEREG_NO_DEST:
345f4f56ff4SMark Johnston 		addr = 0x300 | (reg & 0xff);
346f4f56ff4SMark Johnston 		break;
347f4f56ff4SMark Johnston 	default:
348f4f56ff4SMark Johnston 		addr = AEREG_BAD_REGADDR;
349f4f56ff4SMark Johnston 		break;
350f4f56ff4SMark Johnston 	}
351f4f56ff4SMark Johnston 	return (addr);
352f4f56ff4SMark Johnston }
353f4f56ff4SMark Johnston 
354f4f56ff4SMark Johnston static int
qat_aereg_rel_data_write(struct qat_softc * sc,u_char ae,u_char ctx,enum aereg_type regtype,u_short relreg,uint32_t value)355f4f56ff4SMark Johnston qat_aereg_rel_data_write(struct qat_softc *sc, u_char ae, u_char ctx,
356f4f56ff4SMark Johnston     enum aereg_type regtype, u_short relreg, uint32_t value)
357f4f56ff4SMark Johnston {
358f4f56ff4SMark Johnston 	uint16_t srchi, srclo, destaddr, data16hi, data16lo;
359f4f56ff4SMark Johnston 	uint64_t inst[] = {
360f4f56ff4SMark Johnston 		0x0F440000000ull,	/* immed_w1[reg, val_hi16] */
361f4f56ff4SMark Johnston 		0x0F040000000ull,	/* immed_w0[reg, val_lo16] */
362f4f56ff4SMark Johnston 		0x0F0000C0300ull,	/* nop */
363f4f56ff4SMark Johnston 		0x0E000010000ull	/* ctx_arb[kill] */
364f4f56ff4SMark Johnston 	};
365f4f56ff4SMark Johnston 	const int ninst = nitems(inst);
366f4f56ff4SMark Johnston 	const int imm_w1 = 0, imm_w0 = 1;
367f4f56ff4SMark Johnston 	unsigned int ctxen;
368f4f56ff4SMark Johnston 	uint16_t mask;
369f4f56ff4SMark Johnston 
370f4f56ff4SMark Johnston 	/* This logic only works for GPRs and LM index registers,
371f4f56ff4SMark Johnston 	   not NN or XFER registers! */
372f4f56ff4SMark Johnston 	MPASS(regtype == AEREG_GPA_REL || regtype == AEREG_GPB_REL ||
373f4f56ff4SMark Johnston 	    regtype == AEREG_LMEM0 || regtype == AEREG_LMEM1);
374f4f56ff4SMark Johnston 
375f4f56ff4SMark Johnston 	if ((regtype == AEREG_GPA_REL) || (regtype == AEREG_GPB_REL)) {
376f4f56ff4SMark Johnston 		/* determine the context mode */
377f4f56ff4SMark Johnston 		qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
378f4f56ff4SMark Johnston 		if (ctxen & CTX_ENABLES_INUSE_CONTEXTS) {
379f4f56ff4SMark Johnston 			/* 4-ctx mode */
380f4f56ff4SMark Johnston 			if (ctx & 0x1)
381f4f56ff4SMark Johnston 				return EINVAL;
382f4f56ff4SMark Johnston 			mask = 0x1f;
383f4f56ff4SMark Johnston 		} else {
384f4f56ff4SMark Johnston 			/* 8-ctx mode */
385f4f56ff4SMark Johnston 			mask = 0x0f;
386f4f56ff4SMark Johnston 		}
387f4f56ff4SMark Johnston 		if (relreg & ~mask)
388f4f56ff4SMark Johnston 			return EINVAL;
389f4f56ff4SMark Johnston 	}
390f4f56ff4SMark Johnston 	if ((destaddr = qat_aereg_get_10bit_addr(regtype, relreg)) ==
391f4f56ff4SMark Johnston 	    AEREG_BAD_REGADDR) {
392f4f56ff4SMark Johnston 		return EINVAL;
393f4f56ff4SMark Johnston 	}
394f4f56ff4SMark Johnston 
395f4f56ff4SMark Johnston 	data16lo = 0xffff & value;
396f4f56ff4SMark Johnston 	data16hi = 0xffff & (value >> 16);
397f4f56ff4SMark Johnston 	srchi = qat_aereg_get_10bit_addr(AEREG_NO_DEST,
398f4f56ff4SMark Johnston 		(uint16_t)(0xff & data16hi));
399f4f56ff4SMark Johnston 	srclo = qat_aereg_get_10bit_addr(AEREG_NO_DEST,
400f4f56ff4SMark Johnston 		(uint16_t)(0xff & data16lo));
401f4f56ff4SMark Johnston 
402f4f56ff4SMark Johnston 	switch (regtype) {
403f4f56ff4SMark Johnston 	case AEREG_GPA_REL:	/* A rel source */
404f4f56ff4SMark Johnston 		inst[imm_w1] = inst[imm_w1] | ((data16hi >> 8) << 20) |
405f4f56ff4SMark Johnston 		    ((srchi & 0x3ff) << 10) | (destaddr & 0x3ff);
406f4f56ff4SMark Johnston 		inst[imm_w0] = inst[imm_w0] | ((data16lo >> 8) << 20) |
407f4f56ff4SMark Johnston 		    ((srclo & 0x3ff) << 10) | (destaddr & 0x3ff);
408f4f56ff4SMark Johnston 		break;
409f4f56ff4SMark Johnston 	default:
410f4f56ff4SMark Johnston 		inst[imm_w1] = inst[imm_w1] | ((data16hi >> 8) << 20) |
411f4f56ff4SMark Johnston 		    ((destaddr & 0x3ff) << 10) | (srchi & 0x3ff);
412f4f56ff4SMark Johnston 		inst[imm_w0] = inst[imm_w0] | ((data16lo >> 8) << 20) |
413f4f56ff4SMark Johnston 		    ((destaddr & 0x3ff) << 10) | (srclo & 0x3ff);
414f4f56ff4SMark Johnston 		break;
415f4f56ff4SMark Johnston 	}
416f4f56ff4SMark Johnston 
417f4f56ff4SMark Johnston 	return qat_ae_exec_ucode(sc, ae, ctx, inst, ninst, 1, ninst * 5, NULL);
418f4f56ff4SMark Johnston }
419f4f56ff4SMark Johnston 
420f4f56ff4SMark Johnston static int
qat_aereg_rel_data_read(struct qat_softc * sc,u_char ae,u_char ctx,enum aereg_type regtype,u_short relreg,uint32_t * value)421f4f56ff4SMark Johnston qat_aereg_rel_data_read(struct qat_softc *sc, u_char ae, u_char ctx,
422f4f56ff4SMark Johnston     enum aereg_type regtype, u_short relreg, uint32_t *value)
423f4f56ff4SMark Johnston {
424f4f56ff4SMark Johnston 	uint64_t inst, savucode;
425f4f56ff4SMark Johnston 	uint32_t ctxen, misc, nmisc, savctx, ctxarbctl, ulo, uhi;
426f4f56ff4SMark Johnston 	u_int uaddr, ustore_addr;
427f4f56ff4SMark Johnston 	int error;
428f4f56ff4SMark Johnston 	u_short mask, regaddr;
429f4f56ff4SMark Johnston 	u_char nae;
430f4f56ff4SMark Johnston 
431f4f56ff4SMark Johnston 	MPASS(regtype == AEREG_GPA_REL || regtype == AEREG_GPB_REL ||
432f4f56ff4SMark Johnston 	    regtype == AEREG_SR_REL || regtype == AEREG_SR_RD_REL ||
433f4f56ff4SMark Johnston 	    regtype == AEREG_DR_REL || regtype == AEREG_DR_RD_REL ||
434f4f56ff4SMark Johnston 	    regtype == AEREG_LMEM0 || regtype == AEREG_LMEM1);
435f4f56ff4SMark Johnston 
436f4f56ff4SMark Johnston 	if ((regtype == AEREG_GPA_REL) || (regtype == AEREG_GPB_REL) ||
437f4f56ff4SMark Johnston 	    (regtype == AEREG_SR_REL) || (regtype == AEREG_SR_RD_REL) ||
438f4f56ff4SMark Johnston 	    (regtype == AEREG_DR_REL) || (regtype == AEREG_DR_RD_REL))
439f4f56ff4SMark Johnston 	{
440f4f56ff4SMark Johnston 		/* determine the context mode */
441f4f56ff4SMark Johnston 		qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
442f4f56ff4SMark Johnston 		if (ctxen & CTX_ENABLES_INUSE_CONTEXTS) {
443f4f56ff4SMark Johnston 			/* 4-ctx mode */
444f4f56ff4SMark Johnston 			if (ctx & 0x1)
445f4f56ff4SMark Johnston 				return EINVAL;
446f4f56ff4SMark Johnston 			mask = 0x1f;
447f4f56ff4SMark Johnston 		} else {
448f4f56ff4SMark Johnston 			/* 8-ctx mode */
449f4f56ff4SMark Johnston 			mask = 0x0f;
450f4f56ff4SMark Johnston 		}
451f4f56ff4SMark Johnston 		if (relreg & ~mask)
452f4f56ff4SMark Johnston 			return EINVAL;
453f4f56ff4SMark Johnston 	}
454f4f56ff4SMark Johnston 	if ((regaddr = qat_aereg_get_10bit_addr(regtype, relreg)) ==
455f4f56ff4SMark Johnston 	    AEREG_BAD_REGADDR) {
456f4f56ff4SMark Johnston 		return EINVAL;
457f4f56ff4SMark Johnston 	}
458f4f56ff4SMark Johnston 
459f4f56ff4SMark Johnston 	/* instruction -- alu[--, --, B, reg] */
460f4f56ff4SMark Johnston 	switch (regtype) {
461f4f56ff4SMark Johnston 	case AEREG_GPA_REL:
462f4f56ff4SMark Johnston 		/* A rel source */
463f4f56ff4SMark Johnston 		inst = 0xA070000000ull | (regaddr & 0x3ff);
464f4f56ff4SMark Johnston 		break;
465f4f56ff4SMark Johnston 	default:
466f4f56ff4SMark Johnston 		inst = (0xA030000000ull | ((regaddr & 0x3ff) << 10));
467f4f56ff4SMark Johnston 		break;
468f4f56ff4SMark Johnston 	}
469f4f56ff4SMark Johnston 
470f4f56ff4SMark Johnston 	/* backup shared control store bit, and force AE to
471f4f56ff4SMark Johnston 	 * none-shared mode before executing ucode snippet */
472f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &misc);
473f4f56ff4SMark Johnston 	if (misc & AE_MISC_CONTROL_SHARE_CS) {
474f4f56ff4SMark Johnston 		qat_ae_get_shared_ustore_ae(ae, &nae);
475f4f56ff4SMark Johnston 		if ((1 << nae) & sc->sc_ae_mask && qat_ae_is_active(sc, nae))
476f4f56ff4SMark Johnston 			return EBUSY;
477f4f56ff4SMark Johnston 	}
478f4f56ff4SMark Johnston 
479f4f56ff4SMark Johnston 	nmisc = misc & ~AE_MISC_CONTROL_SHARE_CS;
480f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, AE_MISC_CONTROL, nmisc);
481f4f56ff4SMark Johnston 
482f4f56ff4SMark Johnston 	/* read current context */
483f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS, &savctx);
484f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CTX_ARB_CNTL, &ctxarbctl);
485f4f56ff4SMark Johnston 
486f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
487f4f56ff4SMark Johnston 	/* prevent clearing the W1C bits: the breakpoint bit,
488f4f56ff4SMark Johnston 	ECC error bit, and Parity error bit */
489f4f56ff4SMark Johnston 	ctxen &= CTX_ENABLES_IGNORE_W1C_MASK;
490f4f56ff4SMark Johnston 
491f4f56ff4SMark Johnston 	/* change the context */
492f4f56ff4SMark Johnston 	if (ctx != (savctx & ACTIVE_CTX_STATUS_ACNO))
493f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS,
494f4f56ff4SMark Johnston 		    ctx & ACTIVE_CTX_STATUS_ACNO);
495f4f56ff4SMark Johnston 	/* save a ustore location */
496f4f56ff4SMark Johnston 	if ((error = qat_ae_ucode_read(sc, ae, 0, 1, &savucode)) != 0) {
497f4f56ff4SMark Johnston 		/* restore AE_MISC_CONTROL csr */
498f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, AE_MISC_CONTROL, misc);
499f4f56ff4SMark Johnston 
500f4f56ff4SMark Johnston 		/* restore the context */
501f4f56ff4SMark Johnston 		if (ctx != (savctx & ACTIVE_CTX_STATUS_ACNO)) {
502f4f56ff4SMark Johnston 			qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS,
503f4f56ff4SMark Johnston 			    savctx & ACTIVE_CTX_STATUS_ACNO);
504f4f56ff4SMark Johnston 		}
505f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CTX_ARB_CNTL, ctxarbctl);
506f4f56ff4SMark Johnston 
507f4f56ff4SMark Johnston 		return (error);
508f4f56ff4SMark Johnston 	}
509f4f56ff4SMark Johnston 
510f4f56ff4SMark Johnston 	/* turn off ustore parity */
511f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, CTX_ENABLES,
512f4f56ff4SMark Johnston 	    ctxen & (~CTX_ENABLES_CNTL_STORE_PARITY_ENABLE));
513f4f56ff4SMark Johnston 
514f4f56ff4SMark Johnston 	/* save ustore-addr csr */
515f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, USTORE_ADDRESS, &ustore_addr);
516f4f56ff4SMark Johnston 
517f4f56ff4SMark Johnston 	/* write the ALU instruction to ustore, enable ecs bit */
518f4f56ff4SMark Johnston 	uaddr = 0 | USTORE_ADDRESS_ECS;
519f4f56ff4SMark Johnston 
520f4f56ff4SMark Johnston 	/* set the uaddress */
521f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, USTORE_ADDRESS, uaddr);
522f4f56ff4SMark Johnston 	inst = qat_ae_ucode_set_ecc(inst);
523f4f56ff4SMark Johnston 
524f4f56ff4SMark Johnston 	ulo = (uint32_t)(inst & 0xffffffff);
525f4f56ff4SMark Johnston 	uhi = (uint32_t)(inst >> 32);
526f4f56ff4SMark Johnston 
527f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, USTORE_DATA_LOWER, ulo);
528f4f56ff4SMark Johnston 
529f4f56ff4SMark Johnston 	/* this will auto increment the address */
530f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, USTORE_DATA_UPPER, uhi);
531f4f56ff4SMark Johnston 
532f4f56ff4SMark Johnston 	/* set the uaddress */
533f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, USTORE_ADDRESS, uaddr);
534f4f56ff4SMark Johnston 
535f4f56ff4SMark Johnston 	/* delay for at least 8 cycles */
536f4f56ff4SMark Johnston 	qat_ae_wait_num_cycles(sc, ae, 0x8, 0);
537f4f56ff4SMark Johnston 
538f4f56ff4SMark Johnston 	/* read ALU output -- the instruction should have been executed
539f4f56ff4SMark Johnston 	prior to clearing the ECS in putUwords */
540f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, ALU_OUT, value);
541f4f56ff4SMark Johnston 
542f4f56ff4SMark Johnston 	/* restore ustore-addr csr */
543f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, USTORE_ADDRESS, ustore_addr);
544f4f56ff4SMark Johnston 
545f4f56ff4SMark Johnston 	/* restore the ustore */
546f4f56ff4SMark Johnston 	error = qat_ae_ucode_write(sc, ae, 0, 1, &savucode);
547f4f56ff4SMark Johnston 
548f4f56ff4SMark Johnston 	/* restore the context */
549f4f56ff4SMark Johnston 	if (ctx != (savctx & ACTIVE_CTX_STATUS_ACNO)) {
550f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS,
551f4f56ff4SMark Johnston 		    savctx & ACTIVE_CTX_STATUS_ACNO);
552f4f56ff4SMark Johnston 	}
553f4f56ff4SMark Johnston 
554f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, CTX_ARB_CNTL, ctxarbctl);
555f4f56ff4SMark Johnston 
556f4f56ff4SMark Johnston 	/* restore AE_MISC_CONTROL csr */
557f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, AE_MISC_CONTROL, misc);
558f4f56ff4SMark Johnston 
559f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, CTX_ENABLES, ctxen);
560f4f56ff4SMark Johnston 
561f4f56ff4SMark Johnston 	return error;
562f4f56ff4SMark Johnston }
563f4f56ff4SMark Johnston 
564f4f56ff4SMark Johnston static int
qat_aereg_rel_rdxfer_write(struct qat_softc * sc,u_char ae,u_char ctx,enum aereg_type regtype,u_short relreg,uint32_t value)565f4f56ff4SMark Johnston qat_aereg_rel_rdxfer_write(struct qat_softc *sc, u_char ae, u_char ctx,
566f4f56ff4SMark Johnston     enum aereg_type regtype, u_short relreg, uint32_t value)
567f4f56ff4SMark Johnston {
568f4f56ff4SMark Johnston 	bus_size_t addr;
569f4f56ff4SMark Johnston 	int error;
570f4f56ff4SMark Johnston 	uint32_t ctxen;
571f4f56ff4SMark Johnston 	u_short mask;
572f4f56ff4SMark Johnston 	u_short dr_offset;
573f4f56ff4SMark Johnston 
574f4f56ff4SMark Johnston 	MPASS(regtype == AEREG_SR_REL || regtype == AEREG_DR_REL ||
575f4f56ff4SMark Johnston 	    regtype == AEREG_SR_RD_REL || regtype == AEREG_DR_RD_REL);
576f4f56ff4SMark Johnston 
577f4f56ff4SMark Johnston 	error = qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
578f4f56ff4SMark Johnston 	if (ctxen & CTX_ENABLES_INUSE_CONTEXTS) {
579f4f56ff4SMark Johnston 		if (ctx & 0x1) {
580f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
581f4f56ff4SMark Johnston 			    "bad ctx argument in 4-ctx mode,ctx=0x%x\n", ctx);
582f4f56ff4SMark Johnston 			return EINVAL;
583f4f56ff4SMark Johnston 		}
584f4f56ff4SMark Johnston 		mask = 0x1f;
585f4f56ff4SMark Johnston 		dr_offset = 0x20;
586f4f56ff4SMark Johnston 
587f4f56ff4SMark Johnston 	} else {
588f4f56ff4SMark Johnston 		mask = 0x0f;
589f4f56ff4SMark Johnston 		dr_offset = 0x10;
590f4f56ff4SMark Johnston 	}
591f4f56ff4SMark Johnston 
592f4f56ff4SMark Johnston 	if (relreg & ~mask)
593f4f56ff4SMark Johnston 		return EINVAL;
594f4f56ff4SMark Johnston 
595f4f56ff4SMark Johnston 	addr = relreg + (ctx << 0x5);
596f4f56ff4SMark Johnston 
597f4f56ff4SMark Johnston 	switch (regtype) {
598f4f56ff4SMark Johnston 	case AEREG_SR_REL:
599f4f56ff4SMark Johnston 	case AEREG_SR_RD_REL:
600f4f56ff4SMark Johnston 		qat_ae_xfer_write_4(sc, ae, addr, value);
601f4f56ff4SMark Johnston 		break;
602f4f56ff4SMark Johnston 	case AEREG_DR_REL:
603f4f56ff4SMark Johnston 	case AEREG_DR_RD_REL:
604f4f56ff4SMark Johnston 		qat_ae_xfer_write_4(sc, ae, addr + dr_offset, value);
605f4f56ff4SMark Johnston 		break;
606f4f56ff4SMark Johnston 	default:
607f4f56ff4SMark Johnston 		error = EINVAL;
608f4f56ff4SMark Johnston 	}
609f4f56ff4SMark Johnston 
610f4f56ff4SMark Johnston 	return error;
611f4f56ff4SMark Johnston }
612f4f56ff4SMark Johnston 
613f4f56ff4SMark Johnston static int
qat_aereg_rel_wrxfer_write(struct qat_softc * sc,u_char ae,u_char ctx,enum aereg_type regtype,u_short relreg,uint32_t value)614f4f56ff4SMark Johnston qat_aereg_rel_wrxfer_write(struct qat_softc *sc, u_char ae, u_char ctx,
615f4f56ff4SMark Johnston     enum aereg_type regtype, u_short relreg, uint32_t value)
616f4f56ff4SMark Johnston {
617f4f56ff4SMark Johnston 
618f4f56ff4SMark Johnston 	panic("notyet");
619f4f56ff4SMark Johnston 
620f4f56ff4SMark Johnston 	return 0;
621f4f56ff4SMark Johnston }
622f4f56ff4SMark Johnston 
623f4f56ff4SMark Johnston static int
qat_aereg_rel_nn_write(struct qat_softc * sc,u_char ae,u_char ctx,enum aereg_type regtype,u_short relreg,uint32_t value)624f4f56ff4SMark Johnston qat_aereg_rel_nn_write(struct qat_softc *sc, u_char ae, u_char ctx,
625f4f56ff4SMark Johnston     enum aereg_type regtype, u_short relreg, uint32_t value)
626f4f56ff4SMark Johnston {
627f4f56ff4SMark Johnston 
628f4f56ff4SMark Johnston 	panic("notyet");
629f4f56ff4SMark Johnston 
630f4f56ff4SMark Johnston 	return 0;
631f4f56ff4SMark Johnston }
632f4f56ff4SMark Johnston 
633f4f56ff4SMark Johnston static int
qat_aereg_abs_to_rel(struct qat_softc * sc,u_char ae,u_short absreg,u_short * relreg,u_char * ctx)634f4f56ff4SMark Johnston qat_aereg_abs_to_rel(struct qat_softc *sc, u_char ae,
635f4f56ff4SMark Johnston 	u_short absreg, u_short *relreg, u_char *ctx)
636f4f56ff4SMark Johnston {
637f4f56ff4SMark Johnston 	uint32_t ctxen;
638f4f56ff4SMark Johnston 
639f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
640f4f56ff4SMark Johnston 	if (ctxen & CTX_ENABLES_INUSE_CONTEXTS) {
641f4f56ff4SMark Johnston 		/* 4-ctx mode */
642f4f56ff4SMark Johnston 		*relreg = absreg & 0x1f;
643f4f56ff4SMark Johnston 		*ctx = (absreg >> 0x4) & 0x6;
644f4f56ff4SMark Johnston 	} else {
645f4f56ff4SMark Johnston 		/* 8-ctx mode */
646f4f56ff4SMark Johnston 		*relreg = absreg & 0x0f;
647f4f56ff4SMark Johnston 		*ctx = (absreg >> 0x4) & 0x7;
648f4f56ff4SMark Johnston 	}
649f4f56ff4SMark Johnston 
650f4f56ff4SMark Johnston 	return 0;
651f4f56ff4SMark Johnston }
652f4f56ff4SMark Johnston 
653f4f56ff4SMark Johnston static int
qat_aereg_abs_data_write(struct qat_softc * sc,u_char ae,enum aereg_type regtype,u_short absreg,uint32_t value)654f4f56ff4SMark Johnston qat_aereg_abs_data_write(struct qat_softc *sc, u_char ae,
655f4f56ff4SMark Johnston 	enum aereg_type regtype, u_short absreg, uint32_t value)
656f4f56ff4SMark Johnston {
657f4f56ff4SMark Johnston 	int error;
658f4f56ff4SMark Johnston 	u_short relreg;
659f4f56ff4SMark Johnston 	u_char ctx;
660f4f56ff4SMark Johnston 
661f4f56ff4SMark Johnston 	qat_aereg_abs_to_rel(sc, ae, absreg, &relreg, &ctx);
662f4f56ff4SMark Johnston 
663f4f56ff4SMark Johnston 	switch (regtype) {
664f4f56ff4SMark Johnston 	case AEREG_GPA_ABS:
665f4f56ff4SMark Johnston 		MPASS(absreg < MAX_GPR_REG);
666f4f56ff4SMark Johnston 		error = qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPA_REL,
667f4f56ff4SMark Johnston 		    relreg, value);
668f4f56ff4SMark Johnston 		break;
669f4f56ff4SMark Johnston 	case AEREG_GPB_ABS:
670f4f56ff4SMark Johnston 		MPASS(absreg < MAX_GPR_REG);
671f4f56ff4SMark Johnston 		error = qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPB_REL,
672f4f56ff4SMark Johnston 		    relreg, value);
673f4f56ff4SMark Johnston 		break;
674f4f56ff4SMark Johnston 	case AEREG_DR_RD_ABS:
675f4f56ff4SMark Johnston 		MPASS(absreg < MAX_XFER_REG);
676f4f56ff4SMark Johnston 		error = qat_aereg_rel_rdxfer_write(sc, ae, ctx, AEREG_DR_RD_REL,
677f4f56ff4SMark Johnston 		    relreg, value);
678f4f56ff4SMark Johnston 		break;
679f4f56ff4SMark Johnston 	case AEREG_SR_RD_ABS:
680f4f56ff4SMark Johnston 		MPASS(absreg < MAX_XFER_REG);
681f4f56ff4SMark Johnston 		error = qat_aereg_rel_rdxfer_write(sc, ae, ctx, AEREG_SR_RD_REL,
682f4f56ff4SMark Johnston 		    relreg, value);
683f4f56ff4SMark Johnston 		break;
684f4f56ff4SMark Johnston 	case AEREG_DR_WR_ABS:
685f4f56ff4SMark Johnston 		MPASS(absreg < MAX_XFER_REG);
686f4f56ff4SMark Johnston 		error = qat_aereg_rel_wrxfer_write(sc, ae, ctx, AEREG_DR_WR_REL,
687f4f56ff4SMark Johnston 		    relreg, value);
688f4f56ff4SMark Johnston 		break;
689f4f56ff4SMark Johnston 	case AEREG_SR_WR_ABS:
690f4f56ff4SMark Johnston 		MPASS(absreg < MAX_XFER_REG);
691f4f56ff4SMark Johnston 		error = qat_aereg_rel_wrxfer_write(sc, ae, ctx, AEREG_SR_WR_REL,
692f4f56ff4SMark Johnston 		    relreg, value);
693f4f56ff4SMark Johnston 		break;
694f4f56ff4SMark Johnston 	case AEREG_NEIGH_ABS:
695f4f56ff4SMark Johnston 		MPASS(absreg < MAX_NN_REG);
696f4f56ff4SMark Johnston 		if (absreg >= MAX_NN_REG)
697f4f56ff4SMark Johnston 			return EINVAL;
698f4f56ff4SMark Johnston 		error = qat_aereg_rel_nn_write(sc, ae, ctx, AEREG_NEIGH_REL,
699f4f56ff4SMark Johnston 		    relreg, value);
700f4f56ff4SMark Johnston 		break;
701f4f56ff4SMark Johnston 	default:
702f4f56ff4SMark Johnston 		panic("Invalid Register Type");
703f4f56ff4SMark Johnston 	}
704f4f56ff4SMark Johnston 
705f4f56ff4SMark Johnston 	return error;
706f4f56ff4SMark Johnston }
707f4f56ff4SMark Johnston 
708f4f56ff4SMark Johnston static void
qat_ae_enable_ctx(struct qat_softc * sc,u_char ae,u_int ctx_mask)709f4f56ff4SMark Johnston qat_ae_enable_ctx(struct qat_softc *sc, u_char ae, u_int ctx_mask)
710f4f56ff4SMark Johnston {
711f4f56ff4SMark Johnston 	uint32_t ctxen;
712f4f56ff4SMark Johnston 
713f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
714f4f56ff4SMark Johnston 	ctxen &= CTX_ENABLES_IGNORE_W1C_MASK;
715f4f56ff4SMark Johnston 
716f4f56ff4SMark Johnston 	if (ctxen & CTX_ENABLES_INUSE_CONTEXTS) {
717f4f56ff4SMark Johnston 		ctx_mask &= 0x55;
718f4f56ff4SMark Johnston 	} else {
719f4f56ff4SMark Johnston 		ctx_mask &= 0xff;
720f4f56ff4SMark Johnston 	}
721f4f56ff4SMark Johnston 
722f4f56ff4SMark Johnston 	ctxen |= __SHIFTIN(ctx_mask, CTX_ENABLES_ENABLE);
723f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, CTX_ENABLES, ctxen);
724f4f56ff4SMark Johnston }
725f4f56ff4SMark Johnston 
726f4f56ff4SMark Johnston static void
qat_ae_disable_ctx(struct qat_softc * sc,u_char ae,u_int ctx_mask)727f4f56ff4SMark Johnston qat_ae_disable_ctx(struct qat_softc *sc, u_char ae, u_int ctx_mask)
728f4f56ff4SMark Johnston {
729f4f56ff4SMark Johnston 	uint32_t ctxen;
730f4f56ff4SMark Johnston 
731f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
732f4f56ff4SMark Johnston 	ctxen &= CTX_ENABLES_IGNORE_W1C_MASK;
733f4f56ff4SMark Johnston 	ctxen &= ~(__SHIFTIN(ctx_mask & AE_ALL_CTX, CTX_ENABLES_ENABLE));
734f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, CTX_ENABLES, ctxen);
735f4f56ff4SMark Johnston }
736f4f56ff4SMark Johnston 
737f4f56ff4SMark Johnston static void
qat_ae_write_ctx_mode(struct qat_softc * sc,u_char ae,u_char mode)738f4f56ff4SMark Johnston qat_ae_write_ctx_mode(struct qat_softc *sc, u_char ae, u_char mode)
739f4f56ff4SMark Johnston {
740f4f56ff4SMark Johnston 	uint32_t val, nval;
741f4f56ff4SMark Johnston 
742f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CTX_ENABLES, &val);
743f4f56ff4SMark Johnston 	val &= CTX_ENABLES_IGNORE_W1C_MASK;
744f4f56ff4SMark Johnston 
745f4f56ff4SMark Johnston 	if (mode == 4)
746f4f56ff4SMark Johnston 		nval = val | CTX_ENABLES_INUSE_CONTEXTS;
747f4f56ff4SMark Johnston 	else
748f4f56ff4SMark Johnston 		nval = val & ~CTX_ENABLES_INUSE_CONTEXTS;
749f4f56ff4SMark Johnston 
750f4f56ff4SMark Johnston 	if (val != nval)
751f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CTX_ENABLES, nval);
752f4f56ff4SMark Johnston }
753f4f56ff4SMark Johnston 
754f4f56ff4SMark Johnston static void
qat_ae_write_nn_mode(struct qat_softc * sc,u_char ae,u_char mode)755f4f56ff4SMark Johnston qat_ae_write_nn_mode(struct qat_softc *sc, u_char ae, u_char mode)
756f4f56ff4SMark Johnston {
757f4f56ff4SMark Johnston 	uint32_t val, nval;
758f4f56ff4SMark Johnston 
759f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CTX_ENABLES, &val);
760f4f56ff4SMark Johnston 	val &= CTX_ENABLES_IGNORE_W1C_MASK;
761f4f56ff4SMark Johnston 
762f4f56ff4SMark Johnston 	if (mode)
763f4f56ff4SMark Johnston 		nval = val | CTX_ENABLES_NN_MODE;
764f4f56ff4SMark Johnston 	else
765f4f56ff4SMark Johnston 		nval = val & ~CTX_ENABLES_NN_MODE;
766f4f56ff4SMark Johnston 
767f4f56ff4SMark Johnston 	if (val != nval)
768f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CTX_ENABLES, nval);
769f4f56ff4SMark Johnston }
770f4f56ff4SMark Johnston 
771f4f56ff4SMark Johnston static void
qat_ae_write_lm_mode(struct qat_softc * sc,u_char ae,enum aereg_type lm,u_char mode)772f4f56ff4SMark Johnston qat_ae_write_lm_mode(struct qat_softc *sc, u_char ae,
773f4f56ff4SMark Johnston 	enum aereg_type lm, u_char mode)
774f4f56ff4SMark Johnston {
775f4f56ff4SMark Johnston 	uint32_t val, nval;
776f4f56ff4SMark Johnston 	uint32_t bit;
777f4f56ff4SMark Johnston 
778f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CTX_ENABLES, &val);
779f4f56ff4SMark Johnston 	val &= CTX_ENABLES_IGNORE_W1C_MASK;
780f4f56ff4SMark Johnston 
781f4f56ff4SMark Johnston 	switch (lm) {
782f4f56ff4SMark Johnston 	case AEREG_LMEM0:
783f4f56ff4SMark Johnston 		bit = CTX_ENABLES_LMADDR_0_GLOBAL;
784f4f56ff4SMark Johnston 		break;
785f4f56ff4SMark Johnston 	case AEREG_LMEM1:
786f4f56ff4SMark Johnston 		bit = CTX_ENABLES_LMADDR_1_GLOBAL;
787f4f56ff4SMark Johnston 		break;
788f4f56ff4SMark Johnston 	default:
789f4f56ff4SMark Johnston 		panic("invalid lmem reg type");
790f4f56ff4SMark Johnston 		break;
791f4f56ff4SMark Johnston 	}
792f4f56ff4SMark Johnston 
793f4f56ff4SMark Johnston 	if (mode)
794f4f56ff4SMark Johnston 		nval = val | bit;
795f4f56ff4SMark Johnston 	else
796f4f56ff4SMark Johnston 		nval = val & ~bit;
797f4f56ff4SMark Johnston 
798f4f56ff4SMark Johnston 	if (val != nval)
799f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CTX_ENABLES, nval);
800f4f56ff4SMark Johnston }
801f4f56ff4SMark Johnston 
802f4f56ff4SMark Johnston static void
qat_ae_write_shared_cs_mode0(struct qat_softc * sc,u_char ae,u_char mode)803f4f56ff4SMark Johnston qat_ae_write_shared_cs_mode0(struct qat_softc *sc, u_char ae, u_char mode)
804f4f56ff4SMark Johnston {
805f4f56ff4SMark Johnston 	uint32_t val, nval;
806f4f56ff4SMark Johnston 
807f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &val);
808f4f56ff4SMark Johnston 
809f4f56ff4SMark Johnston 	if (mode == 1)
810f4f56ff4SMark Johnston 		nval = val | AE_MISC_CONTROL_SHARE_CS;
811f4f56ff4SMark Johnston 	else
812f4f56ff4SMark Johnston 		nval = val & ~AE_MISC_CONTROL_SHARE_CS;
813f4f56ff4SMark Johnston 
814f4f56ff4SMark Johnston 	if (val != nval)
815f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, AE_MISC_CONTROL, nval);
816f4f56ff4SMark Johnston }
817f4f56ff4SMark Johnston 
818f4f56ff4SMark Johnston static void
qat_ae_write_shared_cs_mode(struct qat_softc * sc,u_char ae,u_char mode)819f4f56ff4SMark Johnston qat_ae_write_shared_cs_mode(struct qat_softc *sc, u_char ae, u_char mode)
820f4f56ff4SMark Johnston {
821f4f56ff4SMark Johnston 	u_char nae;
822f4f56ff4SMark Johnston 
823f4f56ff4SMark Johnston 	qat_ae_get_shared_ustore_ae(ae, &nae);
824f4f56ff4SMark Johnston 
825f4f56ff4SMark Johnston 	qat_ae_write_shared_cs_mode0(sc, ae, mode);
826f4f56ff4SMark Johnston 
827f4f56ff4SMark Johnston 	if ((sc->sc_ae_mask & (1 << nae))) {
828f4f56ff4SMark Johnston 		qat_ae_write_shared_cs_mode0(sc, nae, mode);
829f4f56ff4SMark Johnston 	}
830f4f56ff4SMark Johnston }
831f4f56ff4SMark Johnston 
832f4f56ff4SMark Johnston static int
qat_ae_set_reload_ustore(struct qat_softc * sc,u_char ae,u_int reload_size,int shared_mode,u_int ustore_dram_addr)833f4f56ff4SMark Johnston qat_ae_set_reload_ustore(struct qat_softc *sc, u_char ae,
834f4f56ff4SMark Johnston 	u_int reload_size, int shared_mode, u_int ustore_dram_addr)
835f4f56ff4SMark Johnston {
836f4f56ff4SMark Johnston 	uint32_t val, cs_reload;
837f4f56ff4SMark Johnston 
838f4f56ff4SMark Johnston 	switch (reload_size) {
839f4f56ff4SMark Johnston 	case 0:
840f4f56ff4SMark Johnston 		cs_reload = 0x0;
841f4f56ff4SMark Johnston 		break;
842f4f56ff4SMark Johnston 	case QAT_2K:
843f4f56ff4SMark Johnston 		cs_reload = 0x1;
844f4f56ff4SMark Johnston 		break;
845f4f56ff4SMark Johnston 	case QAT_4K:
846f4f56ff4SMark Johnston 		cs_reload = 0x2;
847f4f56ff4SMark Johnston 		break;
848f4f56ff4SMark Johnston 	case QAT_8K:
849f4f56ff4SMark Johnston 		cs_reload = 0x3;
850f4f56ff4SMark Johnston 		break;
851f4f56ff4SMark Johnston 	default:
852f4f56ff4SMark Johnston 		return EINVAL;
853f4f56ff4SMark Johnston 	}
854f4f56ff4SMark Johnston 
855f4f56ff4SMark Johnston 	if (cs_reload)
856f4f56ff4SMark Johnston 		QAT_AE(sc, ae).qae_ustore_dram_addr = ustore_dram_addr;
857f4f56ff4SMark Johnston 
858f4f56ff4SMark Johnston 	QAT_AE(sc, ae).qae_reload_size = reload_size;
859f4f56ff4SMark Johnston 
860f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &val);
861f4f56ff4SMark Johnston 	val &= ~(AE_MISC_CONTROL_ONE_CTX_RELOAD |
862f4f56ff4SMark Johnston 	    AE_MISC_CONTROL_CS_RELOAD | AE_MISC_CONTROL_SHARE_CS);
863f4f56ff4SMark Johnston 	val |= __SHIFTIN(cs_reload, AE_MISC_CONTROL_CS_RELOAD) |
864f4f56ff4SMark Johnston 	    __SHIFTIN(shared_mode, AE_MISC_CONTROL_ONE_CTX_RELOAD);
865f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, AE_MISC_CONTROL, val);
866f4f56ff4SMark Johnston 
867f4f56ff4SMark Johnston 	return 0;
868f4f56ff4SMark Johnston }
869f4f56ff4SMark Johnston 
870f4f56ff4SMark Johnston static enum qat_ae_status
qat_ae_get_status(struct qat_softc * sc,u_char ae)871f4f56ff4SMark Johnston qat_ae_get_status(struct qat_softc *sc, u_char ae)
872f4f56ff4SMark Johnston {
873f4f56ff4SMark Johnston 	int error;
874f4f56ff4SMark Johnston 	uint32_t val = 0;
875f4f56ff4SMark Johnston 
876f4f56ff4SMark Johnston 	error = qat_ae_read_4(sc, ae, CTX_ENABLES, &val);
877f4f56ff4SMark Johnston 	if (error || val & CTX_ENABLES_ENABLE)
878f4f56ff4SMark Johnston 		return QAT_AE_ENABLED;
879f4f56ff4SMark Johnston 
880f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS, &val);
881f4f56ff4SMark Johnston 	if (val & ACTIVE_CTX_STATUS_ABO)
882f4f56ff4SMark Johnston 		return QAT_AE_ACTIVE;
883f4f56ff4SMark Johnston 
884f4f56ff4SMark Johnston 	return QAT_AE_DISABLED;
885f4f56ff4SMark Johnston }
886f4f56ff4SMark Johnston 
887f4f56ff4SMark Johnston 
888f4f56ff4SMark Johnston static int
qat_ae_is_active(struct qat_softc * sc,u_char ae)889f4f56ff4SMark Johnston qat_ae_is_active(struct qat_softc *sc, u_char ae)
890f4f56ff4SMark Johnston {
891f4f56ff4SMark Johnston 	uint32_t val;
892f4f56ff4SMark Johnston 
893f4f56ff4SMark Johnston 	if (qat_ae_get_status(sc, ae) != QAT_AE_DISABLED)
894f4f56ff4SMark Johnston 		return 1;
895f4f56ff4SMark Johnston 
896f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS, &val);
897f4f56ff4SMark Johnston 	if (val & ACTIVE_CTX_STATUS_ABO)
898f4f56ff4SMark Johnston 		return 1;
899f4f56ff4SMark Johnston 	else
900f4f56ff4SMark Johnston 		return 0;
901f4f56ff4SMark Johnston }
902f4f56ff4SMark Johnston 
903f4f56ff4SMark Johnston /* returns 1 if actually waited for specified number of cycles */
904f4f56ff4SMark Johnston static int
qat_ae_wait_num_cycles(struct qat_softc * sc,u_char ae,int cycles,int check)905f4f56ff4SMark Johnston qat_ae_wait_num_cycles(struct qat_softc *sc, u_char ae, int cycles, int check)
906f4f56ff4SMark Johnston {
907f4f56ff4SMark Johnston 	uint32_t cnt, actx;
908f4f56ff4SMark Johnston 	int pcnt, ccnt, elapsed, times;
909f4f56ff4SMark Johnston 
910f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, PROFILE_COUNT, &cnt);
911f4f56ff4SMark Johnston 	pcnt = cnt & 0xffff;
912f4f56ff4SMark Johnston 
913f4f56ff4SMark Johnston 	times = TIMEOUT_AE_CHECK;
914f4f56ff4SMark Johnston 	do {
915f4f56ff4SMark Johnston 		qat_ae_read_4(sc, ae, PROFILE_COUNT, &cnt);
916f4f56ff4SMark Johnston 		ccnt = cnt & 0xffff;
917f4f56ff4SMark Johnston 
918f4f56ff4SMark Johnston 		elapsed = ccnt - pcnt;
919f4f56ff4SMark Johnston 		if (elapsed == 0) {
920f4f56ff4SMark Johnston 			times--;
921f4f56ff4SMark Johnston 		}
922f4f56ff4SMark Johnston 		if (times <= 0) {
923f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
924f4f56ff4SMark Johnston 			    "qat_ae_wait_num_cycles timeout\n");
925f4f56ff4SMark Johnston 			return -1;
926f4f56ff4SMark Johnston 		}
927f4f56ff4SMark Johnston 
928f4f56ff4SMark Johnston 		if (elapsed < 0)
929f4f56ff4SMark Johnston 			elapsed += 0x10000;
930f4f56ff4SMark Johnston 
931f4f56ff4SMark Johnston 		if (elapsed >= CYCLES_FROM_READY2EXE && check) {
932f4f56ff4SMark Johnston 			if (qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS,
933f4f56ff4SMark Johnston 			    &actx) == 0) {
934f4f56ff4SMark Johnston 				if ((actx & ACTIVE_CTX_STATUS_ABO) == 0)
935f4f56ff4SMark Johnston 					return 0;
936f4f56ff4SMark Johnston 			}
937f4f56ff4SMark Johnston 		}
938f4f56ff4SMark Johnston 	} while (cycles > elapsed);
939f4f56ff4SMark Johnston 
940f4f56ff4SMark Johnston 	if (check && qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS, &actx) == 0) {
941f4f56ff4SMark Johnston 		if ((actx & ACTIVE_CTX_STATUS_ABO) == 0)
942f4f56ff4SMark Johnston 			return 0;
943f4f56ff4SMark Johnston 	}
944f4f56ff4SMark Johnston 
945f4f56ff4SMark Johnston 	return 1;
946f4f56ff4SMark Johnston }
947f4f56ff4SMark Johnston 
948f4f56ff4SMark Johnston int
qat_ae_init(struct qat_softc * sc)949f4f56ff4SMark Johnston qat_ae_init(struct qat_softc *sc)
950f4f56ff4SMark Johnston {
951f4f56ff4SMark Johnston 	int error;
952f4f56ff4SMark Johnston 	uint32_t mask, val = 0;
953f4f56ff4SMark Johnston 	u_char ae;
954f4f56ff4SMark Johnston 
955f4f56ff4SMark Johnston 	/* XXX adf_initSysMemInfo */
956f4f56ff4SMark Johnston 
957f4f56ff4SMark Johnston 	/* XXX Disable clock gating for some chip if debug mode */
958f4f56ff4SMark Johnston 
959f4f56ff4SMark Johnston 	for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
960f4f56ff4SMark Johnston 		struct qat_ae *qae = &sc->sc_ae[ae];
961f4f56ff4SMark Johnston 		if (!(mask & 1))
962f4f56ff4SMark Johnston 			continue;
963f4f56ff4SMark Johnston 
964f4f56ff4SMark Johnston 		qae->qae_ustore_size = USTORE_SIZE;
965f4f56ff4SMark Johnston 
966f4f56ff4SMark Johnston 		qae->qae_free_addr = 0;
967f4f56ff4SMark Johnston 		qae->qae_free_size = USTORE_SIZE;
968f4f56ff4SMark Johnston 		qae->qae_live_ctx_mask = AE_ALL_CTX;
969f4f56ff4SMark Johnston 		qae->qae_ustore_dram_addr = 0;
970f4f56ff4SMark Johnston 		qae->qae_reload_size = 0;
971f4f56ff4SMark Johnston 	}
972f4f56ff4SMark Johnston 
973f4f56ff4SMark Johnston 	/* XXX Enable attention interrupt */
974f4f56ff4SMark Johnston 
975f4f56ff4SMark Johnston 	error = qat_ae_clear_reset(sc);
976f4f56ff4SMark Johnston 	if (error)
977f4f56ff4SMark Johnston 		return error;
978f4f56ff4SMark Johnston 
979f4f56ff4SMark Johnston 	qat_ae_clear_xfer(sc);
980f4f56ff4SMark Johnston 
981f4f56ff4SMark Johnston 	if (!sc->sc_hw.qhw_fw_auth) {
982f4f56ff4SMark Johnston 		error = qat_ae_clear_gprs(sc);
983f4f56ff4SMark Johnston 		if (error)
984f4f56ff4SMark Johnston 			return error;
985f4f56ff4SMark Johnston 	}
986f4f56ff4SMark Johnston 
987f4f56ff4SMark Johnston 	/* Set SIGNATURE_ENABLE[0] to 0x1 in order to enable ALU_OUT csr */
988f4f56ff4SMark Johnston 	for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
989f4f56ff4SMark Johnston 		if (!(mask & 1))
990f4f56ff4SMark Johnston 			continue;
991f4f56ff4SMark Johnston 		qat_ae_read_4(sc, ae, SIGNATURE_ENABLE, &val);
992f4f56ff4SMark Johnston 		val |= 0x1;
993f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, SIGNATURE_ENABLE, val);
994f4f56ff4SMark Johnston 	}
995f4f56ff4SMark Johnston 
996f4f56ff4SMark Johnston 	error = qat_ae_clear_reset(sc);
997f4f56ff4SMark Johnston 	if (error)
998f4f56ff4SMark Johnston 		return error;
999f4f56ff4SMark Johnston 
1000f4f56ff4SMark Johnston 	/* XXX XXX XXX Clean MMP memory if mem scrub is supported */
1001f4f56ff4SMark Johnston 	/* halMem_ScrubMMPMemory */
1002f4f56ff4SMark Johnston 
1003f4f56ff4SMark Johnston 	return 0;
1004f4f56ff4SMark Johnston }
1005f4f56ff4SMark Johnston 
1006f4f56ff4SMark Johnston int
qat_ae_start(struct qat_softc * sc)1007f4f56ff4SMark Johnston qat_ae_start(struct qat_softc *sc)
1008f4f56ff4SMark Johnston {
1009f4f56ff4SMark Johnston 	int error;
1010f4f56ff4SMark Johnston 	u_char ae;
1011f4f56ff4SMark Johnston 
1012f4f56ff4SMark Johnston 	for (ae = 0; ae < sc->sc_ae_num; ae++) {
1013f4f56ff4SMark Johnston 		if ((sc->sc_ae_mask & (1 << ae)) == 0)
1014f4f56ff4SMark Johnston 			continue;
1015f4f56ff4SMark Johnston 
1016f4f56ff4SMark Johnston 		error = qat_aefw_start(sc, ae, 0xff);
1017f4f56ff4SMark Johnston 		if (error)
1018f4f56ff4SMark Johnston 			return error;
1019f4f56ff4SMark Johnston 	}
1020f4f56ff4SMark Johnston 
1021f4f56ff4SMark Johnston 	return 0;
1022f4f56ff4SMark Johnston }
1023f4f56ff4SMark Johnston 
1024f4f56ff4SMark Johnston void
qat_ae_cluster_intr(void * arg)1025f4f56ff4SMark Johnston qat_ae_cluster_intr(void *arg)
1026f4f56ff4SMark Johnston {
1027f4f56ff4SMark Johnston 	/* Nothing to implement until we support SRIOV. */
1028f4f56ff4SMark Johnston 	printf("qat_ae_cluster_intr\n");
1029f4f56ff4SMark Johnston }
1030f4f56ff4SMark Johnston 
1031f4f56ff4SMark Johnston static int
qat_ae_clear_reset(struct qat_softc * sc)1032f4f56ff4SMark Johnston qat_ae_clear_reset(struct qat_softc *sc)
1033f4f56ff4SMark Johnston {
1034f4f56ff4SMark Johnston 	int error;
1035f4f56ff4SMark Johnston 	uint32_t times, reset, clock, reg, mask;
1036f4f56ff4SMark Johnston 	u_char ae;
1037f4f56ff4SMark Johnston 
1038f4f56ff4SMark Johnston 	reset = qat_cap_global_read_4(sc, CAP_GLOBAL_CTL_RESET);
1039f4f56ff4SMark Johnston 	reset &= ~(__SHIFTIN(sc->sc_ae_mask, CAP_GLOBAL_CTL_RESET_AE_MASK));
1040f4f56ff4SMark Johnston 	reset &= ~(__SHIFTIN(sc->sc_accel_mask, CAP_GLOBAL_CTL_RESET_ACCEL_MASK));
1041f4f56ff4SMark Johnston 	times = TIMEOUT_AE_RESET;
1042f4f56ff4SMark Johnston 	do {
1043f4f56ff4SMark Johnston 		qat_cap_global_write_4(sc, CAP_GLOBAL_CTL_RESET, reset);
1044f4f56ff4SMark Johnston 		if ((times--) == 0) {
1045f4f56ff4SMark Johnston 			device_printf(sc->sc_dev, "couldn't reset AEs\n");
1046f4f56ff4SMark Johnston 			return EBUSY;
1047f4f56ff4SMark Johnston 		}
1048f4f56ff4SMark Johnston 		reg = qat_cap_global_read_4(sc, CAP_GLOBAL_CTL_RESET);
1049f4f56ff4SMark Johnston 	} while ((__SHIFTIN(sc->sc_ae_mask, CAP_GLOBAL_CTL_RESET_AE_MASK) |
1050f4f56ff4SMark Johnston 	    __SHIFTIN(sc->sc_accel_mask, CAP_GLOBAL_CTL_RESET_ACCEL_MASK))
1051f4f56ff4SMark Johnston 	    & reg);
1052f4f56ff4SMark Johnston 
1053f4f56ff4SMark Johnston 	/* Enable clock for AE and QAT */
1054f4f56ff4SMark Johnston 	clock = qat_cap_global_read_4(sc, CAP_GLOBAL_CTL_CLK_EN);
1055f4f56ff4SMark Johnston 	clock |= __SHIFTIN(sc->sc_ae_mask, CAP_GLOBAL_CTL_CLK_EN_AE_MASK);
1056f4f56ff4SMark Johnston 	clock |= __SHIFTIN(sc->sc_accel_mask, CAP_GLOBAL_CTL_CLK_EN_ACCEL_MASK);
1057f4f56ff4SMark Johnston 	qat_cap_global_write_4(sc, CAP_GLOBAL_CTL_CLK_EN, clock);
1058f4f56ff4SMark Johnston 
1059f4f56ff4SMark Johnston 	error = qat_ae_check(sc);
1060f4f56ff4SMark Johnston 	if (error)
1061f4f56ff4SMark Johnston 		return error;
1062f4f56ff4SMark Johnston 
1063f4f56ff4SMark Johnston 	/*
1064f4f56ff4SMark Johnston 	 * Set undefined power-up/reset states to reasonable default values...
1065f4f56ff4SMark Johnston 	 * just to make sure we're starting from a known point
1066f4f56ff4SMark Johnston 	 */
1067f4f56ff4SMark Johnston 	for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
1068f4f56ff4SMark Johnston 		if (!(mask & 1))
1069f4f56ff4SMark Johnston 			continue;
1070f4f56ff4SMark Johnston 
1071f4f56ff4SMark Johnston 		/* init the ctx_enable */
1072f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CTX_ENABLES,
1073f4f56ff4SMark Johnston 		    CTX_ENABLES_INIT);
1074f4f56ff4SMark Johnston 
1075f4f56ff4SMark Johnston 		/* initialize the PCs */
1076f4f56ff4SMark Johnston 		qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1077f4f56ff4SMark Johnston 		    CTX_STS_INDIRECT,
1078f4f56ff4SMark Johnston 		    UPC_MASK & CTX_STS_INDIRECT_UPC_INIT);
1079f4f56ff4SMark Johnston 
1080f4f56ff4SMark Johnston 		/* init the ctx_arb */
1081f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CTX_ARB_CNTL,
1082f4f56ff4SMark Johnston 		    CTX_ARB_CNTL_INIT);
1083f4f56ff4SMark Johnston 
1084f4f56ff4SMark Johnston 		/* enable cc */
1085f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CC_ENABLE,
1086f4f56ff4SMark Johnston 		    CC_ENABLE_INIT);
1087f4f56ff4SMark Johnston 		qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1088f4f56ff4SMark Johnston 		    CTX_WAKEUP_EVENTS_INDIRECT,
1089f4f56ff4SMark Johnston 		    CTX_WAKEUP_EVENTS_INDIRECT_INIT);
1090f4f56ff4SMark Johnston 		qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1091f4f56ff4SMark Johnston 		    CTX_SIG_EVENTS_INDIRECT,
1092f4f56ff4SMark Johnston 		    CTX_SIG_EVENTS_INDIRECT_INIT);
1093f4f56ff4SMark Johnston 	}
1094f4f56ff4SMark Johnston 
1095f4f56ff4SMark Johnston 	if ((sc->sc_ae_mask != 0) &&
1096f4f56ff4SMark Johnston 	    sc->sc_flags & QAT_FLAG_ESRAM_ENABLE_AUTO_INIT) {
1097f4f56ff4SMark Johnston 		/* XXX XXX XXX init eSram only when this is boot time */
1098f4f56ff4SMark Johnston 	}
1099f4f56ff4SMark Johnston 
1100f4f56ff4SMark Johnston 	if ((sc->sc_ae_mask != 0) &&
1101f4f56ff4SMark Johnston 	    sc->sc_flags & QAT_FLAG_SHRAM_WAIT_READY) {
1102f4f56ff4SMark Johnston 		/* XXX XXX XXX wait shram to complete initialization */
1103f4f56ff4SMark Johnston 	}
1104f4f56ff4SMark Johnston 
1105f4f56ff4SMark Johnston 	qat_ae_reset_timestamp(sc);
1106f4f56ff4SMark Johnston 
1107f4f56ff4SMark Johnston 	return 0;
1108f4f56ff4SMark Johnston }
1109f4f56ff4SMark Johnston 
1110f4f56ff4SMark Johnston static int
qat_ae_check(struct qat_softc * sc)1111f4f56ff4SMark Johnston qat_ae_check(struct qat_softc *sc)
1112f4f56ff4SMark Johnston {
1113f4f56ff4SMark Johnston 	int error, times, ae;
1114f4f56ff4SMark Johnston 	uint32_t cnt, pcnt, mask;
1115f4f56ff4SMark Johnston 
1116f4f56ff4SMark Johnston 	for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
1117f4f56ff4SMark Johnston 		if (!(mask & 1))
1118f4f56ff4SMark Johnston 			continue;
1119f4f56ff4SMark Johnston 
1120f4f56ff4SMark Johnston 		times = TIMEOUT_AE_CHECK;
1121f4f56ff4SMark Johnston 		error = qat_ae_read_4(sc, ae, PROFILE_COUNT, &cnt);
1122f4f56ff4SMark Johnston 		if (error) {
1123f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
1124f4f56ff4SMark Johnston 			    "couldn't access AE %d CSR\n", ae);
1125f4f56ff4SMark Johnston 			return error;
1126f4f56ff4SMark Johnston 		}
1127f4f56ff4SMark Johnston 		pcnt = cnt & 0xffff;
1128f4f56ff4SMark Johnston 
1129f4f56ff4SMark Johnston 		while (1) {
1130f4f56ff4SMark Johnston 			error = qat_ae_read_4(sc, ae,
1131f4f56ff4SMark Johnston 			    PROFILE_COUNT, &cnt);
1132f4f56ff4SMark Johnston 			if (error) {
1133f4f56ff4SMark Johnston 				device_printf(sc->sc_dev,
1134f4f56ff4SMark Johnston 				    "couldn't access AE %d CSR\n", ae);
1135f4f56ff4SMark Johnston 				return error;
1136f4f56ff4SMark Johnston 			}
1137f4f56ff4SMark Johnston 			cnt &= 0xffff;
1138f4f56ff4SMark Johnston 			if (cnt == pcnt)
1139f4f56ff4SMark Johnston 				times--;
1140f4f56ff4SMark Johnston 			else
1141f4f56ff4SMark Johnston 				break;
1142f4f56ff4SMark Johnston 			if (times <= 0) {
1143f4f56ff4SMark Johnston 				device_printf(sc->sc_dev,
1144f4f56ff4SMark Johnston 				    "AE %d CSR is useless\n", ae);
1145f4f56ff4SMark Johnston 				return EFAULT;
1146f4f56ff4SMark Johnston 			}
1147f4f56ff4SMark Johnston 		}
1148f4f56ff4SMark Johnston 	}
1149f4f56ff4SMark Johnston 
1150f4f56ff4SMark Johnston 	return 0;
1151f4f56ff4SMark Johnston }
1152f4f56ff4SMark Johnston 
1153f4f56ff4SMark Johnston static int
qat_ae_reset_timestamp(struct qat_softc * sc)1154f4f56ff4SMark Johnston qat_ae_reset_timestamp(struct qat_softc *sc)
1155f4f56ff4SMark Johnston {
1156f4f56ff4SMark Johnston 	uint32_t misc, mask;
1157f4f56ff4SMark Johnston 	u_char ae;
1158f4f56ff4SMark Johnston 
1159f4f56ff4SMark Johnston 	/* stop the timestamp timers */
1160f4f56ff4SMark Johnston 	misc = qat_cap_global_read_4(sc, CAP_GLOBAL_CTL_MISC);
1161f4f56ff4SMark Johnston 	if (misc & CAP_GLOBAL_CTL_MISC_TIMESTAMP_EN) {
1162f4f56ff4SMark Johnston 		qat_cap_global_write_4(sc, CAP_GLOBAL_CTL_MISC,
1163f4f56ff4SMark Johnston 		    misc & (~CAP_GLOBAL_CTL_MISC_TIMESTAMP_EN));
1164f4f56ff4SMark Johnston 	}
1165f4f56ff4SMark Johnston 
1166f4f56ff4SMark Johnston 	for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
1167f4f56ff4SMark Johnston 		if (!(mask & 1))
1168f4f56ff4SMark Johnston 			continue;
1169f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, TIMESTAMP_LOW, 0);
1170f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, TIMESTAMP_HIGH, 0);
1171f4f56ff4SMark Johnston 	}
1172f4f56ff4SMark Johnston 
1173f4f56ff4SMark Johnston 	/* start timestamp timers */
1174f4f56ff4SMark Johnston 	qat_cap_global_write_4(sc, CAP_GLOBAL_CTL_MISC,
1175f4f56ff4SMark Johnston 	    misc | CAP_GLOBAL_CTL_MISC_TIMESTAMP_EN);
1176f4f56ff4SMark Johnston 
1177f4f56ff4SMark Johnston 	return 0;
1178f4f56ff4SMark Johnston }
1179f4f56ff4SMark Johnston 
1180f4f56ff4SMark Johnston static void
qat_ae_clear_xfer(struct qat_softc * sc)1181f4f56ff4SMark Johnston qat_ae_clear_xfer(struct qat_softc *sc)
1182f4f56ff4SMark Johnston {
1183f4f56ff4SMark Johnston 	u_int mask, reg;
1184f4f56ff4SMark Johnston 	u_char ae;
1185f4f56ff4SMark Johnston 
1186f4f56ff4SMark Johnston 	for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
1187f4f56ff4SMark Johnston 		if (!(mask & 1))
1188f4f56ff4SMark Johnston 			continue;
1189f4f56ff4SMark Johnston 
1190f4f56ff4SMark Johnston 		for (reg = 0; reg < MAX_GPR_REG; reg++) {
1191f4f56ff4SMark Johnston 			qat_aereg_abs_data_write(sc, ae, AEREG_SR_RD_ABS,
1192f4f56ff4SMark Johnston 			    reg, 0);
1193f4f56ff4SMark Johnston 			qat_aereg_abs_data_write(sc, ae, AEREG_DR_RD_ABS,
1194f4f56ff4SMark Johnston 			    reg, 0);
1195f4f56ff4SMark Johnston 		}
1196f4f56ff4SMark Johnston 	}
1197f4f56ff4SMark Johnston }
1198f4f56ff4SMark Johnston 
1199f4f56ff4SMark Johnston static int
qat_ae_clear_gprs(struct qat_softc * sc)1200f4f56ff4SMark Johnston qat_ae_clear_gprs(struct qat_softc *sc)
1201f4f56ff4SMark Johnston {
1202f4f56ff4SMark Johnston 	uint32_t val;
1203f4f56ff4SMark Johnston 	uint32_t saved_ctx = 0;
1204f4f56ff4SMark Johnston 	int times = TIMEOUT_AE_CHECK, rv;
1205f4f56ff4SMark Johnston 	u_char ae;
1206f4f56ff4SMark Johnston 	u_int mask;
1207f4f56ff4SMark Johnston 
1208f4f56ff4SMark Johnston 	for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
1209f4f56ff4SMark Johnston 		if (!(mask & 1))
1210f4f56ff4SMark Johnston 			continue;
1211f4f56ff4SMark Johnston 
1212f4f56ff4SMark Johnston 		/* turn off share control store bit */
1213f4f56ff4SMark Johnston 		val = qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &val);
1214f4f56ff4SMark Johnston 		val &= ~AE_MISC_CONTROL_SHARE_CS;
1215f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, AE_MISC_CONTROL, val);
1216f4f56ff4SMark Johnston 
1217f4f56ff4SMark Johnston 		/* turn off ucode parity */
1218f4f56ff4SMark Johnston 		/* make sure nn_mode is set to self */
1219f4f56ff4SMark Johnston 		qat_ae_read_4(sc, ae, CTX_ENABLES, &val);
1220f4f56ff4SMark Johnston 		val &= CTX_ENABLES_IGNORE_W1C_MASK;
1221f4f56ff4SMark Johnston 		val |= CTX_ENABLES_NN_MODE;
1222f4f56ff4SMark Johnston 		val &= ~CTX_ENABLES_CNTL_STORE_PARITY_ENABLE;
1223f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CTX_ENABLES, val);
1224f4f56ff4SMark Johnston 
1225f4f56ff4SMark Johnston 		/* copy instructions to ustore */
1226f4f56ff4SMark Johnston 		qat_ae_ucode_write(sc, ae, 0, nitems(ae_clear_gprs_inst),
1227f4f56ff4SMark Johnston 		    ae_clear_gprs_inst);
1228f4f56ff4SMark Johnston 
1229f4f56ff4SMark Johnston 		/* set PC */
1230f4f56ff4SMark Johnston 		qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX, CTX_STS_INDIRECT,
1231f4f56ff4SMark Johnston 		    UPC_MASK & CTX_STS_INDIRECT_UPC_INIT);
1232f4f56ff4SMark Johnston 
1233f4f56ff4SMark Johnston 		/* save current context */
1234f4f56ff4SMark Johnston 		qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS, &saved_ctx);
1235f4f56ff4SMark Johnston 		/* change the active context */
1236f4f56ff4SMark Johnston 		/* start the context from ctx 0 */
1237f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS, 0);
1238f4f56ff4SMark Johnston 
1239f4f56ff4SMark Johnston 		/* wakeup-event voluntary */
1240f4f56ff4SMark Johnston 		qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1241f4f56ff4SMark Johnston 		    CTX_WAKEUP_EVENTS_INDIRECT,
1242f4f56ff4SMark Johnston 		    CTX_WAKEUP_EVENTS_INDIRECT_VOLUNTARY);
1243f4f56ff4SMark Johnston 		/* clean signals */
1244f4f56ff4SMark Johnston 		qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1245f4f56ff4SMark Johnston 		    CTX_SIG_EVENTS_INDIRECT, 0);
1246f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CTX_SIG_EVENTS_ACTIVE, 0);
1247f4f56ff4SMark Johnston 
1248f4f56ff4SMark Johnston 		qat_ae_enable_ctx(sc, ae, AE_ALL_CTX);
1249f4f56ff4SMark Johnston 	}
1250f4f56ff4SMark Johnston 
1251f4f56ff4SMark Johnston 	for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
1252f4f56ff4SMark Johnston 		if (!(mask & 1))
1253f4f56ff4SMark Johnston 			continue;
1254f4f56ff4SMark Johnston 		/* wait for AE to finish */
1255f4f56ff4SMark Johnston 		do {
1256f4f56ff4SMark Johnston 			rv = qat_ae_wait_num_cycles(sc, ae, AE_EXEC_CYCLE, 1);
1257f4f56ff4SMark Johnston 		} while (rv && times--);
1258f4f56ff4SMark Johnston 		if (times <= 0) {
1259f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
1260f4f56ff4SMark Johnston 			    "qat_ae_clear_gprs timeout");
1261f4f56ff4SMark Johnston 			return ETIMEDOUT;
1262f4f56ff4SMark Johnston 		}
1263f4f56ff4SMark Johnston 		qat_ae_disable_ctx(sc, ae, AE_ALL_CTX);
1264f4f56ff4SMark Johnston 		/* change the active context */
1265f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS,
1266f4f56ff4SMark Johnston 		    saved_ctx & ACTIVE_CTX_STATUS_ACNO);
1267f4f56ff4SMark Johnston 		/* init the ctx_enable */
1268f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CTX_ENABLES, CTX_ENABLES_INIT);
1269f4f56ff4SMark Johnston 		/* initialize the PCs */
1270f4f56ff4SMark Johnston 		qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1271f4f56ff4SMark Johnston 		    CTX_STS_INDIRECT, UPC_MASK & CTX_STS_INDIRECT_UPC_INIT);
1272f4f56ff4SMark Johnston 		/* init the ctx_arb */
1273f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CTX_ARB_CNTL, CTX_ARB_CNTL_INIT);
1274f4f56ff4SMark Johnston 		/* enable cc */
1275f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CC_ENABLE, CC_ENABLE_INIT);
1276f4f56ff4SMark Johnston 		qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX,
1277f4f56ff4SMark Johnston 		    CTX_WAKEUP_EVENTS_INDIRECT, CTX_WAKEUP_EVENTS_INDIRECT_INIT);
1278f4f56ff4SMark Johnston 		qat_ae_ctx_indr_write(sc, ae, AE_ALL_CTX, CTX_SIG_EVENTS_INDIRECT,
1279f4f56ff4SMark Johnston 		    CTX_SIG_EVENTS_INDIRECT_INIT);
1280f4f56ff4SMark Johnston 	}
1281f4f56ff4SMark Johnston 
1282f4f56ff4SMark Johnston 	return 0;
1283f4f56ff4SMark Johnston }
1284f4f56ff4SMark Johnston 
1285f4f56ff4SMark Johnston static void
qat_ae_get_shared_ustore_ae(u_char ae,u_char * nae)1286f4f56ff4SMark Johnston qat_ae_get_shared_ustore_ae(u_char ae, u_char *nae)
1287f4f56ff4SMark Johnston {
1288f4f56ff4SMark Johnston 	if (ae & 0x1)
1289f4f56ff4SMark Johnston 		*nae = ae - 1;
1290f4f56ff4SMark Johnston 	else
1291f4f56ff4SMark Johnston 		*nae = ae + 1;
1292f4f56ff4SMark Johnston }
1293f4f56ff4SMark Johnston 
1294f4f56ff4SMark Johnston static u_int
qat_ae_ucode_parity64(uint64_t ucode)1295f4f56ff4SMark Johnston qat_ae_ucode_parity64(uint64_t ucode)
1296f4f56ff4SMark Johnston {
1297f4f56ff4SMark Johnston 
1298f4f56ff4SMark Johnston 	ucode ^= ucode >> 1;
1299f4f56ff4SMark Johnston 	ucode ^= ucode >> 2;
1300f4f56ff4SMark Johnston 	ucode ^= ucode >> 4;
1301f4f56ff4SMark Johnston 	ucode ^= ucode >> 8;
1302f4f56ff4SMark Johnston 	ucode ^= ucode >> 16;
1303f4f56ff4SMark Johnston 	ucode ^= ucode >> 32;
1304f4f56ff4SMark Johnston 
1305f4f56ff4SMark Johnston 	return ((u_int)(ucode & 1));
1306f4f56ff4SMark Johnston }
1307f4f56ff4SMark Johnston 
1308f4f56ff4SMark Johnston static uint64_t
qat_ae_ucode_set_ecc(uint64_t ucode)1309f4f56ff4SMark Johnston qat_ae_ucode_set_ecc(uint64_t ucode)
1310f4f56ff4SMark Johnston {
1311f4f56ff4SMark Johnston 	static const uint64_t
1312f4f56ff4SMark Johnston 		bit0mask=0xff800007fffULL, bit1mask=0x1f801ff801fULL,
1313f4f56ff4SMark Johnston 		bit2mask=0xe387e0781e1ULL, bit3mask=0x7cb8e388e22ULL,
1314f4f56ff4SMark Johnston 		bit4mask=0xaf5b2c93244ULL, bit5mask=0xf56d5525488ULL,
1315f4f56ff4SMark Johnston 		bit6mask=0xdaf69a46910ULL;
1316f4f56ff4SMark Johnston 
1317f4f56ff4SMark Johnston 	/* clear the ecc bits */
1318f4f56ff4SMark Johnston 	ucode &= ~(0x7fULL << USTORE_ECC_BIT_0);
1319f4f56ff4SMark Johnston 
1320f4f56ff4SMark Johnston 	ucode |= (uint64_t)qat_ae_ucode_parity64(bit0mask & ucode) <<
1321f4f56ff4SMark Johnston 	    USTORE_ECC_BIT_0;
1322f4f56ff4SMark Johnston 	ucode |= (uint64_t)qat_ae_ucode_parity64(bit1mask & ucode) <<
1323f4f56ff4SMark Johnston 	    USTORE_ECC_BIT_1;
1324f4f56ff4SMark Johnston 	ucode |= (uint64_t)qat_ae_ucode_parity64(bit2mask & ucode) <<
1325f4f56ff4SMark Johnston 	    USTORE_ECC_BIT_2;
1326f4f56ff4SMark Johnston 	ucode |= (uint64_t)qat_ae_ucode_parity64(bit3mask & ucode) <<
1327f4f56ff4SMark Johnston 	    USTORE_ECC_BIT_3;
1328f4f56ff4SMark Johnston 	ucode |= (uint64_t)qat_ae_ucode_parity64(bit4mask & ucode) <<
1329f4f56ff4SMark Johnston 	    USTORE_ECC_BIT_4;
1330f4f56ff4SMark Johnston 	ucode |= (uint64_t)qat_ae_ucode_parity64(bit5mask & ucode) <<
1331f4f56ff4SMark Johnston 	    USTORE_ECC_BIT_5;
1332f4f56ff4SMark Johnston 	ucode |= (uint64_t)qat_ae_ucode_parity64(bit6mask & ucode) <<
1333f4f56ff4SMark Johnston 	    USTORE_ECC_BIT_6;
1334f4f56ff4SMark Johnston 
1335f4f56ff4SMark Johnston 	return (ucode);
1336f4f56ff4SMark Johnston }
1337f4f56ff4SMark Johnston 
1338f4f56ff4SMark Johnston static int
qat_ae_ucode_write(struct qat_softc * sc,u_char ae,u_int uaddr,u_int ninst,const uint64_t * ucode)1339f4f56ff4SMark Johnston qat_ae_ucode_write(struct qat_softc *sc, u_char ae, u_int uaddr, u_int ninst,
1340f4f56ff4SMark Johnston 	const uint64_t *ucode)
1341f4f56ff4SMark Johnston {
1342f4f56ff4SMark Johnston 	uint64_t tmp;
1343f4f56ff4SMark Johnston 	uint32_t ustore_addr, ulo, uhi;
1344f4f56ff4SMark Johnston 	int i;
1345f4f56ff4SMark Johnston 
1346f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, USTORE_ADDRESS, &ustore_addr);
1347f4f56ff4SMark Johnston 	uaddr |= USTORE_ADDRESS_ECS;
1348f4f56ff4SMark Johnston 
1349f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, USTORE_ADDRESS, uaddr);
1350f4f56ff4SMark Johnston 	for (i = 0; i < ninst; i++) {
1351f4f56ff4SMark Johnston 		tmp = qat_ae_ucode_set_ecc(ucode[i]);
1352f4f56ff4SMark Johnston 		ulo = (uint32_t)(tmp & 0xffffffff);
1353f4f56ff4SMark Johnston 		uhi = (uint32_t)(tmp >> 32);
1354f4f56ff4SMark Johnston 
1355f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, USTORE_DATA_LOWER, ulo);
1356f4f56ff4SMark Johnston 		/* this will auto increment the address */
1357f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, USTORE_DATA_UPPER, uhi);
1358f4f56ff4SMark Johnston 	}
1359f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, USTORE_ADDRESS, ustore_addr);
1360f4f56ff4SMark Johnston 
1361f4f56ff4SMark Johnston 	return 0;
1362f4f56ff4SMark Johnston }
1363f4f56ff4SMark Johnston 
1364f4f56ff4SMark Johnston static int
qat_ae_ucode_read(struct qat_softc * sc,u_char ae,u_int uaddr,u_int ninst,uint64_t * ucode)1365f4f56ff4SMark Johnston qat_ae_ucode_read(struct qat_softc *sc, u_char ae, u_int uaddr, u_int ninst,
1366f4f56ff4SMark Johnston     uint64_t *ucode)
1367f4f56ff4SMark Johnston {
1368f4f56ff4SMark Johnston 	uint32_t misc, ustore_addr, ulo, uhi;
1369f4f56ff4SMark Johnston 	u_int ii;
1370f4f56ff4SMark Johnston 	u_char nae;
1371f4f56ff4SMark Johnston 
1372f4f56ff4SMark Johnston 	if (qat_ae_get_status(sc, ae) != QAT_AE_DISABLED)
1373f4f56ff4SMark Johnston 		return EBUSY;
1374f4f56ff4SMark Johnston 
1375f4f56ff4SMark Johnston 	/* determine whether it neighbour AE runs in shared control store
1376f4f56ff4SMark Johnston 	 * status */
1377f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &misc);
1378f4f56ff4SMark Johnston 	if (misc & AE_MISC_CONTROL_SHARE_CS) {
1379f4f56ff4SMark Johnston 		qat_ae_get_shared_ustore_ae(ae, &nae);
1380f4f56ff4SMark Johnston 		if ((sc->sc_ae_mask & (1 << nae)) && qat_ae_is_active(sc, nae))
1381f4f56ff4SMark Johnston 			return EBUSY;
1382f4f56ff4SMark Johnston 	}
1383f4f56ff4SMark Johnston 
1384f4f56ff4SMark Johnston 	/* if reloadable, then get it all from dram-ustore */
1385f4f56ff4SMark Johnston 	if (__SHIFTOUT(misc, AE_MISC_CONTROL_CS_RELOAD))
1386f4f56ff4SMark Johnston 		panic("notyet"); /* XXX getReloadUwords */
1387f4f56ff4SMark Johnston 
1388f4f56ff4SMark Johnston 	/* disable SHARE_CS bit to workaround silicon bug */
1389f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, AE_MISC_CONTROL, misc & 0xfffffffb);
1390f4f56ff4SMark Johnston 
1391f4f56ff4SMark Johnston 	MPASS(uaddr + ninst <= USTORE_SIZE);
1392f4f56ff4SMark Johnston 
1393f4f56ff4SMark Johnston 	/* save ustore-addr csr */
1394f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, USTORE_ADDRESS, &ustore_addr);
1395f4f56ff4SMark Johnston 
1396f4f56ff4SMark Johnston 	uaddr |= USTORE_ADDRESS_ECS;	/* enable ecs bit */
1397f4f56ff4SMark Johnston 	for (ii = 0; ii < ninst; ii++) {
1398f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, USTORE_ADDRESS, uaddr);
1399f4f56ff4SMark Johnston 
1400f4f56ff4SMark Johnston 		uaddr++;
1401f4f56ff4SMark Johnston 		qat_ae_read_4(sc, ae, USTORE_DATA_LOWER, &ulo);
1402f4f56ff4SMark Johnston 		qat_ae_read_4(sc, ae, USTORE_DATA_UPPER, &uhi);
1403f4f56ff4SMark Johnston 		ucode[ii] = uhi;
1404f4f56ff4SMark Johnston 		ucode[ii] = (ucode[ii] << 32) | ulo;
1405f4f56ff4SMark Johnston 	}
1406f4f56ff4SMark Johnston 
1407f4f56ff4SMark Johnston 	/* restore SHARE_CS bit to workaround silicon bug */
1408f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, AE_MISC_CONTROL, misc);
1409f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, USTORE_ADDRESS, ustore_addr);
1410f4f56ff4SMark Johnston 
1411f4f56ff4SMark Johnston 	return 0;
1412f4f56ff4SMark Johnston }
1413f4f56ff4SMark Johnston 
1414f4f56ff4SMark Johnston static u_int
qat_ae_concat_ucode(uint64_t * ucode,u_int ninst,u_int size,u_int addr,u_int * value)1415f4f56ff4SMark Johnston qat_ae_concat_ucode(uint64_t *ucode, u_int ninst, u_int size, u_int addr,
1416f4f56ff4SMark Johnston     u_int *value)
1417f4f56ff4SMark Johnston {
1418f4f56ff4SMark Johnston 	const uint64_t *inst_arr;
1419f4f56ff4SMark Johnston 	u_int ninst0, curvalue;
1420f4f56ff4SMark Johnston 	int ii, vali, fixup, usize = 0;
1421f4f56ff4SMark Johnston 
1422f4f56ff4SMark Johnston 	if (size == 0)
1423f4f56ff4SMark Johnston 		return 0;
1424f4f56ff4SMark Johnston 
1425f4f56ff4SMark Johnston 	ninst0 = ninst;
1426f4f56ff4SMark Johnston 	vali = 0;
1427f4f56ff4SMark Johnston 	curvalue = value[vali++];
1428f4f56ff4SMark Johnston 
1429f4f56ff4SMark Johnston 	switch (size) {
1430f4f56ff4SMark Johnston 	case 0x1:
1431f4f56ff4SMark Johnston 		inst_arr = ae_inst_1b;
1432f4f56ff4SMark Johnston 		usize = nitems(ae_inst_1b);
1433f4f56ff4SMark Johnston 		break;
1434f4f56ff4SMark Johnston 	case 0x2:
1435f4f56ff4SMark Johnston 		inst_arr = ae_inst_2b;
1436f4f56ff4SMark Johnston 		usize = nitems(ae_inst_2b);
1437f4f56ff4SMark Johnston 		break;
1438f4f56ff4SMark Johnston 	case 0x3:
1439f4f56ff4SMark Johnston 		inst_arr = ae_inst_3b;
1440f4f56ff4SMark Johnston 		usize = nitems(ae_inst_3b);
1441f4f56ff4SMark Johnston 		break;
1442f4f56ff4SMark Johnston 	default:
1443f4f56ff4SMark Johnston 		inst_arr = ae_inst_4b;
1444f4f56ff4SMark Johnston 		usize = nitems(ae_inst_4b);
1445f4f56ff4SMark Johnston 		break;
1446f4f56ff4SMark Johnston 	}
1447f4f56ff4SMark Johnston 
1448f4f56ff4SMark Johnston 	fixup = ninst;
1449f4f56ff4SMark Johnston 	for (ii = 0; ii < usize; ii++)
1450f4f56ff4SMark Johnston 		ucode[ninst++] = inst_arr[ii];
1451f4f56ff4SMark Johnston 
1452f4f56ff4SMark Johnston 	INSERT_IMMED_GPRA_CONST(ucode[fixup], (addr));
1453f4f56ff4SMark Johnston 	fixup++;
1454f4f56ff4SMark Johnston 	INSERT_IMMED_GPRA_CONST(ucode[fixup], 0);
1455f4f56ff4SMark Johnston 	fixup++;
1456f4f56ff4SMark Johnston 	INSERT_IMMED_GPRB_CONST(ucode[fixup], (curvalue >> 0));
1457f4f56ff4SMark Johnston 	fixup++;
1458f4f56ff4SMark Johnston 	INSERT_IMMED_GPRB_CONST(ucode[fixup], (curvalue >> 16));
1459f4f56ff4SMark Johnston 	/* XXX fixup++ ? */
1460f4f56ff4SMark Johnston 
1461f4f56ff4SMark Johnston 	if (size <= 0x4)
1462f4f56ff4SMark Johnston 		return (ninst - ninst0);
1463f4f56ff4SMark Johnston 
1464f4f56ff4SMark Johnston 	size -= sizeof(u_int);
1465f4f56ff4SMark Johnston 	while (size >= sizeof(u_int)) {
1466f4f56ff4SMark Johnston 		curvalue = value[vali++];
1467f4f56ff4SMark Johnston 		fixup = ninst;
1468f4f56ff4SMark Johnston 		ucode[ninst++] = ae_inst_4b[0x2];
1469f4f56ff4SMark Johnston 		ucode[ninst++] = ae_inst_4b[0x3];
1470f4f56ff4SMark Johnston 		ucode[ninst++] = ae_inst_4b[0x8];
1471f4f56ff4SMark Johnston 		INSERT_IMMED_GPRB_CONST(ucode[fixup], (curvalue >> 16));
1472f4f56ff4SMark Johnston 		fixup++;
1473f4f56ff4SMark Johnston 		INSERT_IMMED_GPRB_CONST(ucode[fixup], (curvalue >> 0));
1474f4f56ff4SMark Johnston 		/* XXX fixup++ ? */
1475f4f56ff4SMark Johnston 
1476f4f56ff4SMark Johnston 		addr += sizeof(u_int);
1477f4f56ff4SMark Johnston 		size -= sizeof(u_int);
1478f4f56ff4SMark Johnston 	}
1479f4f56ff4SMark Johnston 	/* call this function recusive when the left size less than 4 */
1480f4f56ff4SMark Johnston 	ninst +=
1481f4f56ff4SMark Johnston 	    qat_ae_concat_ucode(ucode, ninst, size, addr, value + vali);
1482f4f56ff4SMark Johnston 
1483f4f56ff4SMark Johnston 	return (ninst - ninst0);
1484f4f56ff4SMark Johnston }
1485f4f56ff4SMark Johnston 
1486f4f56ff4SMark Johnston static int
qat_ae_exec_ucode(struct qat_softc * sc,u_char ae,u_char ctx,uint64_t * ucode,u_int ninst,int cond_code_off,u_int max_cycles,u_int * endpc)1487f4f56ff4SMark Johnston qat_ae_exec_ucode(struct qat_softc *sc, u_char ae, u_char ctx,
1488f4f56ff4SMark Johnston     uint64_t *ucode, u_int ninst, int cond_code_off, u_int max_cycles,
1489f4f56ff4SMark Johnston     u_int *endpc)
1490f4f56ff4SMark Johnston {
1491f4f56ff4SMark Johnston 	int error = 0, share_cs = 0;
1492f4f56ff4SMark Johnston 	uint64_t savucode[MAX_EXEC_INST];
1493f4f56ff4SMark Johnston 	uint32_t indr_lm_addr_0, indr_lm_addr_1;
1494f4f56ff4SMark Johnston 	uint32_t indr_lm_addr_byte_0, indr_lm_addr_byte_1;
1495f4f56ff4SMark Johnston 	uint32_t indr_future_cnt_sig;
1496f4f56ff4SMark Johnston 	uint32_t indr_sig, active_sig;
1497f4f56ff4SMark Johnston 	uint32_t wakeup_ev, savpc, savcc, savctx, ctxarbctl;
1498f4f56ff4SMark Johnston 	uint32_t misc, nmisc, ctxen;
1499f4f56ff4SMark Johnston 	u_char nae;
1500f4f56ff4SMark Johnston 
1501f4f56ff4SMark Johnston 	MPASS(ninst <= USTORE_SIZE);
1502f4f56ff4SMark Johnston 
1503f4f56ff4SMark Johnston 	if (qat_ae_is_active(sc, ae))
1504f4f56ff4SMark Johnston 		return EBUSY;
1505f4f56ff4SMark Johnston 
1506f4f56ff4SMark Johnston 	/* save current LM addr */
1507f4f56ff4SMark Johnston 	qat_ae_ctx_indr_read(sc, ae, ctx, LM_ADDR_0_INDIRECT, &indr_lm_addr_0);
1508f4f56ff4SMark Johnston 	qat_ae_ctx_indr_read(sc, ae, ctx, LM_ADDR_1_INDIRECT, &indr_lm_addr_1);
1509f4f56ff4SMark Johnston 	qat_ae_ctx_indr_read(sc, ae, ctx, INDIRECT_LM_ADDR_0_BYTE_INDEX,
1510f4f56ff4SMark Johnston 	    &indr_lm_addr_byte_0);
1511f4f56ff4SMark Johnston 	qat_ae_ctx_indr_read(sc, ae, ctx, INDIRECT_LM_ADDR_1_BYTE_INDEX,
1512f4f56ff4SMark Johnston 	    &indr_lm_addr_byte_1);
1513f4f56ff4SMark Johnston 
1514f4f56ff4SMark Johnston 	/* backup shared control store bit, and force AE to
1515f4f56ff4SMark Johnston 	   none-shared mode before executing ucode snippet */
1516f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &misc);
1517f4f56ff4SMark Johnston 	if (misc & AE_MISC_CONTROL_SHARE_CS) {
1518f4f56ff4SMark Johnston 		share_cs = 1;
1519f4f56ff4SMark Johnston 		qat_ae_get_shared_ustore_ae(ae, &nae);
1520f4f56ff4SMark Johnston 		if ((sc->sc_ae_mask & (1 << nae)) && qat_ae_is_active(sc, nae))
1521f4f56ff4SMark Johnston 			return EBUSY;
1522f4f56ff4SMark Johnston 	}
1523f4f56ff4SMark Johnston 	nmisc = misc & ~AE_MISC_CONTROL_SHARE_CS;
1524f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, AE_MISC_CONTROL, nmisc);
1525f4f56ff4SMark Johnston 
1526f4f56ff4SMark Johnston 	/* save current states: */
1527f4f56ff4SMark Johnston 	if (ninst <= MAX_EXEC_INST) {
1528f4f56ff4SMark Johnston 		error = qat_ae_ucode_read(sc, ae, 0, ninst, savucode);
1529f4f56ff4SMark Johnston 		if (error) {
1530f4f56ff4SMark Johnston 			qat_ae_write_4(sc, ae, AE_MISC_CONTROL, misc);
1531f4f56ff4SMark Johnston 			return error;
1532f4f56ff4SMark Johnston 		}
1533f4f56ff4SMark Johnston 	}
1534f4f56ff4SMark Johnston 
1535f4f56ff4SMark Johnston 	/* save wakeup-events */
1536f4f56ff4SMark Johnston 	qat_ae_ctx_indr_read(sc, ae, ctx, CTX_WAKEUP_EVENTS_INDIRECT,
1537f4f56ff4SMark Johnston 	    &wakeup_ev);
1538f4f56ff4SMark Johnston 	/* save PC */
1539f4f56ff4SMark Johnston 	qat_ae_ctx_indr_read(sc, ae, ctx, CTX_STS_INDIRECT, &savpc);
1540f4f56ff4SMark Johnston 	savpc &= UPC_MASK;
1541f4f56ff4SMark Johnston 
1542f4f56ff4SMark Johnston 	/* save ctx enables */
1543f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CTX_ENABLES, &ctxen);
1544f4f56ff4SMark Johnston 	ctxen &= CTX_ENABLES_IGNORE_W1C_MASK;
1545f4f56ff4SMark Johnston 	/* save conditional-code */
1546f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CC_ENABLE, &savcc);
1547f4f56ff4SMark Johnston 	/* save current context */
1548f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, ACTIVE_CTX_STATUS, &savctx);
1549f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CTX_ARB_CNTL, &ctxarbctl);
1550f4f56ff4SMark Johnston 
1551f4f56ff4SMark Johnston 	/* save indirect csrs */
1552f4f56ff4SMark Johnston 	qat_ae_ctx_indr_read(sc, ae, ctx, FUTURE_COUNT_SIGNAL_INDIRECT,
1553f4f56ff4SMark Johnston 	    &indr_future_cnt_sig);
1554f4f56ff4SMark Johnston 	qat_ae_ctx_indr_read(sc, ae, ctx, CTX_SIG_EVENTS_INDIRECT, &indr_sig);
1555f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, CTX_SIG_EVENTS_ACTIVE, &active_sig);
1556f4f56ff4SMark Johnston 
1557f4f56ff4SMark Johnston 	/* turn off ucode parity */
1558f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, CTX_ENABLES,
1559f4f56ff4SMark Johnston 	    ctxen & ~CTX_ENABLES_CNTL_STORE_PARITY_ENABLE);
1560f4f56ff4SMark Johnston 
1561f4f56ff4SMark Johnston 	/* copy instructions to ustore */
1562f4f56ff4SMark Johnston 	qat_ae_ucode_write(sc, ae, 0, ninst, ucode);
1563f4f56ff4SMark Johnston 	/* set PC */
1564f4f56ff4SMark Johnston 	qat_ae_ctx_indr_write(sc, ae, 1 << ctx, CTX_STS_INDIRECT, 0);
1565f4f56ff4SMark Johnston 	/* change the active context */
1566f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS,
1567f4f56ff4SMark Johnston 	    ctx & ACTIVE_CTX_STATUS_ACNO);
1568f4f56ff4SMark Johnston 
1569f4f56ff4SMark Johnston 	if (cond_code_off) {
1570f4f56ff4SMark Johnston 		/* disable conditional-code*/
1571f4f56ff4SMark Johnston 		qat_ae_write_4(sc, ae, CC_ENABLE, savcc & 0xffffdfff);
1572f4f56ff4SMark Johnston 	}
1573f4f56ff4SMark Johnston 
1574f4f56ff4SMark Johnston 	/* wakeup-event voluntary */
1575f4f56ff4SMark Johnston 	qat_ae_ctx_indr_write(sc, ae, 1 << ctx,
1576f4f56ff4SMark Johnston 	    CTX_WAKEUP_EVENTS_INDIRECT, CTX_WAKEUP_EVENTS_INDIRECT_VOLUNTARY);
1577f4f56ff4SMark Johnston 
1578f4f56ff4SMark Johnston 	/* clean signals */
1579f4f56ff4SMark Johnston 	qat_ae_ctx_indr_write(sc, ae, 1 << ctx, CTX_SIG_EVENTS_INDIRECT, 0);
1580f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, CTX_SIG_EVENTS_ACTIVE, 0);
1581f4f56ff4SMark Johnston 
1582f4f56ff4SMark Johnston 	/* enable context */
1583f4f56ff4SMark Johnston 	qat_ae_enable_ctx(sc, ae, 1 << ctx);
1584f4f56ff4SMark Johnston 
1585f4f56ff4SMark Johnston 	/* wait for it to finish */
1586f4f56ff4SMark Johnston 	if (qat_ae_wait_num_cycles(sc, ae, max_cycles, 1) != 0)
1587f4f56ff4SMark Johnston 		error = ETIMEDOUT;
1588f4f56ff4SMark Johnston 
1589f4f56ff4SMark Johnston 	/* see if we need to get the current PC */
1590f4f56ff4SMark Johnston 	if (endpc != NULL) {
1591f4f56ff4SMark Johnston 		uint32_t ctx_status;
1592f4f56ff4SMark Johnston 
1593f4f56ff4SMark Johnston 		qat_ae_ctx_indr_read(sc, ae, ctx, CTX_STS_INDIRECT,
1594f4f56ff4SMark Johnston 		    &ctx_status);
1595f4f56ff4SMark Johnston 		*endpc = ctx_status & UPC_MASK;
1596f4f56ff4SMark Johnston 	}
1597f4f56ff4SMark Johnston #if 0
1598f4f56ff4SMark Johnston 	{
1599f4f56ff4SMark Johnston 		uint32_t ctx_status;
1600f4f56ff4SMark Johnston 
1601f4f56ff4SMark Johnston 		qat_ae_ctx_indr_read(sc, ae, ctx, CTX_STS_INDIRECT,
1602f4f56ff4SMark Johnston 		    &ctx_status);
1603f4f56ff4SMark Johnston 		printf("%s: endpc 0x%08x\n", __func__,
1604f4f56ff4SMark Johnston 		    ctx_status & UPC_MASK);
1605f4f56ff4SMark Johnston 	}
1606f4f56ff4SMark Johnston #endif
1607f4f56ff4SMark Johnston 
1608f4f56ff4SMark Johnston 	/* retore to previous states: */
1609f4f56ff4SMark Johnston 	/* disable context */
1610f4f56ff4SMark Johnston 	qat_ae_disable_ctx(sc, ae, 1 << ctx);
1611f4f56ff4SMark Johnston 	if (ninst <= MAX_EXEC_INST) {
1612f4f56ff4SMark Johnston 		/* instructions */
1613f4f56ff4SMark Johnston 		qat_ae_ucode_write(sc, ae, 0, ninst, savucode);
1614f4f56ff4SMark Johnston 	}
1615f4f56ff4SMark Johnston 	/* wakeup-events */
1616f4f56ff4SMark Johnston 	qat_ae_ctx_indr_write(sc, ae, 1 << ctx, CTX_WAKEUP_EVENTS_INDIRECT,
1617f4f56ff4SMark Johnston 	    wakeup_ev);
1618f4f56ff4SMark Johnston 	qat_ae_ctx_indr_write(sc, ae, 1 << ctx, CTX_STS_INDIRECT, savpc);
1619f4f56ff4SMark Johnston 
1620f4f56ff4SMark Johnston 	/* only restore shared control store bit,
1621f4f56ff4SMark Johnston 	   other bit might be changed by AE code snippet */
1622f4f56ff4SMark Johnston 	qat_ae_read_4(sc, ae, AE_MISC_CONTROL, &misc);
1623f4f56ff4SMark Johnston 	if (share_cs)
1624f4f56ff4SMark Johnston 		nmisc = misc | AE_MISC_CONTROL_SHARE_CS;
1625f4f56ff4SMark Johnston 	else
1626f4f56ff4SMark Johnston 		nmisc = misc & ~AE_MISC_CONTROL_SHARE_CS;
1627f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, AE_MISC_CONTROL, nmisc);
1628f4f56ff4SMark Johnston 	/* conditional-code */
1629f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, CC_ENABLE, savcc);
1630f4f56ff4SMark Johnston 	/* change the active context */
1631f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, ACTIVE_CTX_STATUS,
1632f4f56ff4SMark Johnston 	    savctx & ACTIVE_CTX_STATUS_ACNO);
1633f4f56ff4SMark Johnston 	/* restore the nxt ctx to run */
1634f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, CTX_ARB_CNTL, ctxarbctl);
1635f4f56ff4SMark Johnston 	/* restore current LM addr */
1636f4f56ff4SMark Johnston 	qat_ae_ctx_indr_write(sc, ae, 1 << ctx, LM_ADDR_0_INDIRECT,
1637f4f56ff4SMark Johnston 	    indr_lm_addr_0);
1638f4f56ff4SMark Johnston 	qat_ae_ctx_indr_write(sc, ae, 1 << ctx, LM_ADDR_1_INDIRECT,
1639f4f56ff4SMark Johnston 	    indr_lm_addr_1);
1640f4f56ff4SMark Johnston 	qat_ae_ctx_indr_write(sc, ae, 1 << ctx, INDIRECT_LM_ADDR_0_BYTE_INDEX,
1641f4f56ff4SMark Johnston 	    indr_lm_addr_byte_0);
1642f4f56ff4SMark Johnston 	qat_ae_ctx_indr_write(sc, ae, 1 << ctx, INDIRECT_LM_ADDR_1_BYTE_INDEX,
1643f4f56ff4SMark Johnston 	    indr_lm_addr_byte_1);
1644f4f56ff4SMark Johnston 
1645f4f56ff4SMark Johnston 	/* restore indirect csrs */
1646f4f56ff4SMark Johnston 	qat_ae_ctx_indr_write(sc, ae, 1 << ctx, FUTURE_COUNT_SIGNAL_INDIRECT,
1647f4f56ff4SMark Johnston 	    indr_future_cnt_sig);
1648f4f56ff4SMark Johnston 	qat_ae_ctx_indr_write(sc, ae, 1 << ctx, CTX_SIG_EVENTS_INDIRECT,
1649f4f56ff4SMark Johnston 	    indr_sig);
1650f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, CTX_SIG_EVENTS_ACTIVE, active_sig);
1651f4f56ff4SMark Johnston 
1652f4f56ff4SMark Johnston 	/* ctx-enables */
1653f4f56ff4SMark Johnston 	qat_ae_write_4(sc, ae, CTX_ENABLES, ctxen);
1654f4f56ff4SMark Johnston 
1655f4f56ff4SMark Johnston 	return error;
1656f4f56ff4SMark Johnston }
1657f4f56ff4SMark Johnston 
1658f4f56ff4SMark Johnston static int
qat_ae_exec_ucode_init_lm(struct qat_softc * sc,u_char ae,u_char ctx,int * first_exec,uint64_t * ucode,u_int ninst,u_int * gpr_a0,u_int * gpr_a1,u_int * gpr_a2,u_int * gpr_b0,u_int * gpr_b1)1659f4f56ff4SMark Johnston qat_ae_exec_ucode_init_lm(struct qat_softc *sc, u_char ae, u_char ctx,
1660f4f56ff4SMark Johnston     int *first_exec, uint64_t *ucode, u_int ninst,
1661f4f56ff4SMark Johnston     u_int *gpr_a0, u_int *gpr_a1, u_int *gpr_a2, u_int *gpr_b0, u_int *gpr_b1)
1662f4f56ff4SMark Johnston {
1663f4f56ff4SMark Johnston 
1664f4f56ff4SMark Johnston 	if (*first_exec) {
1665f4f56ff4SMark Johnston 		qat_aereg_rel_data_read(sc, ae, ctx, AEREG_GPA_REL, 0, gpr_a0);
1666f4f56ff4SMark Johnston 		qat_aereg_rel_data_read(sc, ae, ctx, AEREG_GPA_REL, 1, gpr_a1);
1667f4f56ff4SMark Johnston 		qat_aereg_rel_data_read(sc, ae, ctx, AEREG_GPA_REL, 2, gpr_a2);
1668f4f56ff4SMark Johnston 		qat_aereg_rel_data_read(sc, ae, ctx, AEREG_GPB_REL, 0, gpr_b0);
1669f4f56ff4SMark Johnston 		qat_aereg_rel_data_read(sc, ae, ctx, AEREG_GPB_REL, 1, gpr_b1);
1670f4f56ff4SMark Johnston 		*first_exec = 0;
1671f4f56ff4SMark Johnston 	}
1672f4f56ff4SMark Johnston 
1673f4f56ff4SMark Johnston 	return qat_ae_exec_ucode(sc, ae, ctx, ucode, ninst, 1, ninst * 5, NULL);
1674f4f56ff4SMark Johnston }
1675f4f56ff4SMark Johnston 
1676f4f56ff4SMark Johnston static int
qat_ae_restore_init_lm_gprs(struct qat_softc * sc,u_char ae,u_char ctx,u_int gpr_a0,u_int gpr_a1,u_int gpr_a2,u_int gpr_b0,u_int gpr_b1)1677f4f56ff4SMark Johnston qat_ae_restore_init_lm_gprs(struct qat_softc *sc, u_char ae, u_char ctx,
1678f4f56ff4SMark Johnston     u_int gpr_a0, u_int gpr_a1, u_int gpr_a2, u_int gpr_b0, u_int gpr_b1)
1679f4f56ff4SMark Johnston {
1680f4f56ff4SMark Johnston 	qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPA_REL, 0, gpr_a0);
1681f4f56ff4SMark Johnston 	qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPA_REL, 1, gpr_a1);
1682f4f56ff4SMark Johnston 	qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPA_REL, 2, gpr_a2);
1683f4f56ff4SMark Johnston 	qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPB_REL, 0, gpr_b0);
1684f4f56ff4SMark Johnston 	qat_aereg_rel_data_write(sc, ae, ctx, AEREG_GPB_REL, 1, gpr_b1);
1685f4f56ff4SMark Johnston 
1686f4f56ff4SMark Johnston 	return 0;
1687f4f56ff4SMark Johnston }
1688f4f56ff4SMark Johnston 
1689f4f56ff4SMark Johnston static int
qat_ae_get_inst_num(int lmsize)1690f4f56ff4SMark Johnston qat_ae_get_inst_num(int lmsize)
1691f4f56ff4SMark Johnston {
1692f4f56ff4SMark Johnston 	int ninst, left;
1693f4f56ff4SMark Johnston 
1694f4f56ff4SMark Johnston 	if (lmsize == 0)
1695f4f56ff4SMark Johnston 		return 0;
1696f4f56ff4SMark Johnston 
1697f4f56ff4SMark Johnston 	left = lmsize % sizeof(u_int);
1698f4f56ff4SMark Johnston 
1699f4f56ff4SMark Johnston 	if (left) {
1700f4f56ff4SMark Johnston 		ninst = nitems(ae_inst_1b) +
1701f4f56ff4SMark Johnston 		    qat_ae_get_inst_num(lmsize - left);
1702f4f56ff4SMark Johnston 	} else {
1703f4f56ff4SMark Johnston 		/* 3 instruction is needed for further code */
1704f4f56ff4SMark Johnston 		ninst = (lmsize - sizeof(u_int)) * 3 / 4 + nitems(ae_inst_4b);
1705f4f56ff4SMark Johnston 	}
1706f4f56ff4SMark Johnston 
1707f4f56ff4SMark Johnston 	return (ninst);
1708f4f56ff4SMark Johnston }
1709f4f56ff4SMark Johnston 
1710f4f56ff4SMark Johnston static int
qat_ae_batch_put_lm(struct qat_softc * sc,u_char ae,struct qat_ae_batch_init_list * qabi_list,size_t nqabi)1711f4f56ff4SMark Johnston qat_ae_batch_put_lm(struct qat_softc *sc, u_char ae,
1712f4f56ff4SMark Johnston     struct qat_ae_batch_init_list *qabi_list, size_t nqabi)
1713f4f56ff4SMark Johnston {
1714f4f56ff4SMark Johnston 	struct qat_ae_batch_init *qabi;
1715f4f56ff4SMark Johnston 	size_t alloc_ninst, ninst;
1716f4f56ff4SMark Johnston 	uint64_t *ucode;
1717f4f56ff4SMark Johnston 	u_int gpr_a0, gpr_a1, gpr_a2, gpr_b0, gpr_b1;
1718f4f56ff4SMark Johnston 	int insnsz, error = 0, execed = 0, first_exec = 1;
1719f4f56ff4SMark Johnston 
1720f4f56ff4SMark Johnston 	if (STAILQ_FIRST(qabi_list) == NULL)
1721f4f56ff4SMark Johnston 		return 0;
1722f4f56ff4SMark Johnston 
1723f4f56ff4SMark Johnston 	alloc_ninst = min(USTORE_SIZE, nqabi);
1724f4f56ff4SMark Johnston 	ucode = qat_alloc_mem(sizeof(uint64_t) * alloc_ninst);
1725f4f56ff4SMark Johnston 
1726f4f56ff4SMark Johnston 	ninst = 0;
1727f4f56ff4SMark Johnston 	STAILQ_FOREACH(qabi, qabi_list, qabi_next) {
1728f4f56ff4SMark Johnston 		insnsz = qat_ae_get_inst_num(qabi->qabi_size);
1729f4f56ff4SMark Johnston 		if (insnsz + ninst > alloc_ninst) {
1730f4f56ff4SMark Johnston 			/* add ctx_arb[kill] */
1731f4f56ff4SMark Johnston 			ucode[ninst++] = 0x0E000010000ull;
1732f4f56ff4SMark Johnston 			execed = 1;
1733f4f56ff4SMark Johnston 
1734f4f56ff4SMark Johnston 			error = qat_ae_exec_ucode_init_lm(sc, ae, 0,
1735f4f56ff4SMark Johnston 			    &first_exec, ucode, ninst,
1736f4f56ff4SMark Johnston 			    &gpr_a0, &gpr_a1, &gpr_a2, &gpr_b0, &gpr_b1);
1737f4f56ff4SMark Johnston 			if (error) {
1738f4f56ff4SMark Johnston 				qat_ae_restore_init_lm_gprs(sc, ae, 0,
1739f4f56ff4SMark Johnston 				    gpr_a0, gpr_a1, gpr_a2, gpr_b0, gpr_b1);
1740f4f56ff4SMark Johnston 				qat_free_mem(ucode);
1741f4f56ff4SMark Johnston 				return error;
1742f4f56ff4SMark Johnston 			}
1743f4f56ff4SMark Johnston 			/* run microExec to execute the microcode */
1744f4f56ff4SMark Johnston 			ninst = 0;
1745f4f56ff4SMark Johnston 		}
1746f4f56ff4SMark Johnston 		ninst += qat_ae_concat_ucode(ucode, ninst,
1747f4f56ff4SMark Johnston 		    qabi->qabi_size, qabi->qabi_addr, qabi->qabi_value);
1748f4f56ff4SMark Johnston 	}
1749f4f56ff4SMark Johnston 
1750f4f56ff4SMark Johnston 	if (ninst > 0) {
1751f4f56ff4SMark Johnston 		ucode[ninst++] = 0x0E000010000ull;
1752f4f56ff4SMark Johnston 		execed = 1;
1753f4f56ff4SMark Johnston 
1754f4f56ff4SMark Johnston 		error = qat_ae_exec_ucode_init_lm(sc, ae, 0,
1755f4f56ff4SMark Johnston 		    &first_exec, ucode, ninst,
1756f4f56ff4SMark Johnston 		    &gpr_a0, &gpr_a1, &gpr_a2, &gpr_b0, &gpr_b1);
1757f4f56ff4SMark Johnston 	}
1758f4f56ff4SMark Johnston 	if (execed) {
1759f4f56ff4SMark Johnston 		qat_ae_restore_init_lm_gprs(sc, ae, 0,
1760f4f56ff4SMark Johnston 		    gpr_a0, gpr_a1, gpr_a2, gpr_b0, gpr_b1);
1761f4f56ff4SMark Johnston 	}
1762f4f56ff4SMark Johnston 
1763f4f56ff4SMark Johnston 	qat_free_mem(ucode);
1764f4f56ff4SMark Johnston 
1765f4f56ff4SMark Johnston 	return error;
1766f4f56ff4SMark Johnston }
1767f4f56ff4SMark Johnston 
1768f4f56ff4SMark Johnston static int
qat_ae_write_pc(struct qat_softc * sc,u_char ae,u_int ctx_mask,u_int upc)1769f4f56ff4SMark Johnston qat_ae_write_pc(struct qat_softc *sc, u_char ae, u_int ctx_mask, u_int upc)
1770f4f56ff4SMark Johnston {
1771f4f56ff4SMark Johnston 
1772f4f56ff4SMark Johnston 	if (qat_ae_is_active(sc, ae))
1773f4f56ff4SMark Johnston 		return EBUSY;
1774f4f56ff4SMark Johnston 
1775f4f56ff4SMark Johnston 	qat_ae_ctx_indr_write(sc, ae, ctx_mask, CTX_STS_INDIRECT,
1776f4f56ff4SMark Johnston 	    UPC_MASK & upc);
1777f4f56ff4SMark Johnston 	return 0;
1778f4f56ff4SMark Johnston }
1779f4f56ff4SMark Johnston 
1780f4f56ff4SMark Johnston static inline u_int
qat_aefw_csum_calc(u_int reg,int ch)1781f4f56ff4SMark Johnston qat_aefw_csum_calc(u_int reg, int ch)
1782f4f56ff4SMark Johnston {
1783f4f56ff4SMark Johnston 	int i;
1784f4f56ff4SMark Johnston 	u_int topbit = CRC_BITMASK(CRC_WIDTH - 1);
1785f4f56ff4SMark Johnston 	u_int inbyte = (u_int)((reg >> 0x18) ^ ch);
1786f4f56ff4SMark Johnston 
1787f4f56ff4SMark Johnston 	reg ^= inbyte << (CRC_WIDTH - 0x8);
1788f4f56ff4SMark Johnston 	for (i = 0; i < 0x8; i++) {
1789f4f56ff4SMark Johnston 		if (reg & topbit)
1790f4f56ff4SMark Johnston 			reg = (reg << 1) ^ CRC_POLY;
1791f4f56ff4SMark Johnston 		else
1792f4f56ff4SMark Johnston 			reg <<= 1;
1793f4f56ff4SMark Johnston 	}
1794f4f56ff4SMark Johnston 
1795f4f56ff4SMark Johnston 	return (reg & CRC_WIDTHMASK(CRC_WIDTH));
1796f4f56ff4SMark Johnston }
1797f4f56ff4SMark Johnston 
1798f4f56ff4SMark Johnston static u_int
qat_aefw_csum(char * buf,int size)1799f4f56ff4SMark Johnston qat_aefw_csum(char *buf, int size)
1800f4f56ff4SMark Johnston {
1801f4f56ff4SMark Johnston 	u_int csum = 0;
1802f4f56ff4SMark Johnston 
1803f4f56ff4SMark Johnston 	while (size--) {
1804f4f56ff4SMark Johnston 		csum = qat_aefw_csum_calc(csum, *buf++);
1805f4f56ff4SMark Johnston 	}
1806f4f56ff4SMark Johnston 
1807f4f56ff4SMark Johnston 	return csum;
1808f4f56ff4SMark Johnston }
1809f4f56ff4SMark Johnston 
1810f4f56ff4SMark Johnston static const char *
qat_aefw_uof_string(struct qat_softc * sc,size_t offset)1811f4f56ff4SMark Johnston qat_aefw_uof_string(struct qat_softc *sc, size_t offset)
1812f4f56ff4SMark Johnston {
1813f4f56ff4SMark Johnston 	if (offset >= sc->sc_aefw_uof.qafu_str_tab_size)
1814f4f56ff4SMark Johnston 		return NULL;
1815f4f56ff4SMark Johnston 	if (sc->sc_aefw_uof.qafu_str_tab == NULL)
1816f4f56ff4SMark Johnston 		return NULL;
1817f4f56ff4SMark Johnston 
1818f4f56ff4SMark Johnston 	return (const char *)((uintptr_t)sc->sc_aefw_uof.qafu_str_tab + offset);
1819f4f56ff4SMark Johnston }
1820f4f56ff4SMark Johnston 
1821f4f56ff4SMark Johnston static struct uof_chunk_hdr *
qat_aefw_uof_find_chunk(struct qat_softc * sc,const char * id,struct uof_chunk_hdr * cur)1822f4f56ff4SMark Johnston qat_aefw_uof_find_chunk(struct qat_softc *sc,
1823f4f56ff4SMark Johnston 	const char *id, struct uof_chunk_hdr *cur)
1824f4f56ff4SMark Johnston {
1825f4f56ff4SMark Johnston 	struct uof_obj_hdr *uoh = sc->sc_aefw_uof.qafu_obj_hdr;
1826f4f56ff4SMark Johnston 	struct uof_chunk_hdr *uch;
1827f4f56ff4SMark Johnston 	int i;
1828f4f56ff4SMark Johnston 
1829f4f56ff4SMark Johnston 	uch = (struct uof_chunk_hdr *)(uoh + 1);
1830f4f56ff4SMark Johnston 	for (i = 0; i < uoh->uoh_num_chunks; i++, uch++) {
1831f4f56ff4SMark Johnston 		if (uch->uch_offset + uch->uch_size > sc->sc_aefw_uof.qafu_size)
1832f4f56ff4SMark Johnston 			return NULL;
1833f4f56ff4SMark Johnston 
1834f4f56ff4SMark Johnston 		if (cur < uch && !strncmp(uch->uch_id, id, UOF_OBJ_ID_LEN))
1835f4f56ff4SMark Johnston 			return uch;
1836f4f56ff4SMark Johnston 	}
1837f4f56ff4SMark Johnston 
1838f4f56ff4SMark Johnston 	return NULL;
1839f4f56ff4SMark Johnston }
1840f4f56ff4SMark Johnston 
1841f4f56ff4SMark Johnston static int
qat_aefw_load_mof(struct qat_softc * sc)1842f4f56ff4SMark Johnston qat_aefw_load_mof(struct qat_softc *sc)
1843f4f56ff4SMark Johnston {
1844f4f56ff4SMark Johnston 	const struct firmware *fw;
1845f4f56ff4SMark Johnston 
1846f4f56ff4SMark Johnston 	fw = firmware_get(sc->sc_hw.qhw_mof_fwname);
1847f4f56ff4SMark Johnston 	if (fw == NULL) {
1848f4f56ff4SMark Johnston 		device_printf(sc->sc_dev, "couldn't load MOF firmware %s\n",
1849f4f56ff4SMark Johnston 		    sc->sc_hw.qhw_mof_fwname);
1850f4f56ff4SMark Johnston 		return ENXIO;
1851f4f56ff4SMark Johnston 	}
1852f4f56ff4SMark Johnston 
1853f4f56ff4SMark Johnston 	sc->sc_fw_mof = qat_alloc_mem(fw->datasize);
1854f4f56ff4SMark Johnston 	sc->sc_fw_mof_size = fw->datasize;
1855f4f56ff4SMark Johnston 	memcpy(sc->sc_fw_mof, fw->data, fw->datasize);
1856f4f56ff4SMark Johnston 	firmware_put(fw, FIRMWARE_UNLOAD);
1857f4f56ff4SMark Johnston 	return 0;
1858f4f56ff4SMark Johnston }
1859f4f56ff4SMark Johnston 
1860f4f56ff4SMark Johnston static void
qat_aefw_unload_mof(struct qat_softc * sc)1861f4f56ff4SMark Johnston qat_aefw_unload_mof(struct qat_softc *sc)
1862f4f56ff4SMark Johnston {
1863f4f56ff4SMark Johnston 	if (sc->sc_fw_mof != NULL) {
1864f4f56ff4SMark Johnston 		qat_free_mem(sc->sc_fw_mof);
1865f4f56ff4SMark Johnston 		sc->sc_fw_mof = NULL;
1866f4f56ff4SMark Johnston 	}
1867f4f56ff4SMark Johnston }
1868f4f56ff4SMark Johnston 
1869f4f56ff4SMark Johnston static int
qat_aefw_load_mmp(struct qat_softc * sc)1870f4f56ff4SMark Johnston qat_aefw_load_mmp(struct qat_softc *sc)
1871f4f56ff4SMark Johnston {
1872f4f56ff4SMark Johnston 	const struct firmware *fw;
1873f4f56ff4SMark Johnston 
1874f4f56ff4SMark Johnston 	fw = firmware_get(sc->sc_hw.qhw_mmp_fwname);
1875f4f56ff4SMark Johnston 	if (fw == NULL) {
1876f4f56ff4SMark Johnston 		device_printf(sc->sc_dev, "couldn't load MOF firmware %s\n",
1877f4f56ff4SMark Johnston 		    sc->sc_hw.qhw_mmp_fwname);
1878f4f56ff4SMark Johnston 		return ENXIO;
1879f4f56ff4SMark Johnston 	}
1880f4f56ff4SMark Johnston 
1881f4f56ff4SMark Johnston 	sc->sc_fw_mmp = qat_alloc_mem(fw->datasize);
1882f4f56ff4SMark Johnston 	sc->sc_fw_mmp_size = fw->datasize;
1883f4f56ff4SMark Johnston 	memcpy(sc->sc_fw_mmp, fw->data, fw->datasize);
1884f4f56ff4SMark Johnston 	firmware_put(fw, FIRMWARE_UNLOAD);
1885f4f56ff4SMark Johnston 	return 0;
1886f4f56ff4SMark Johnston }
1887f4f56ff4SMark Johnston 
1888f4f56ff4SMark Johnston static void
qat_aefw_unload_mmp(struct qat_softc * sc)1889f4f56ff4SMark Johnston qat_aefw_unload_mmp(struct qat_softc *sc)
1890f4f56ff4SMark Johnston {
1891f4f56ff4SMark Johnston 	if (sc->sc_fw_mmp != NULL) {
1892f4f56ff4SMark Johnston 		qat_free_mem(sc->sc_fw_mmp);
1893f4f56ff4SMark Johnston 		sc->sc_fw_mmp = NULL;
1894f4f56ff4SMark Johnston 	}
1895f4f56ff4SMark Johnston }
1896f4f56ff4SMark Johnston 
1897f4f56ff4SMark Johnston static int
qat_aefw_mof_find_uof0(struct qat_softc * sc,struct mof_uof_hdr * muh,struct mof_uof_chunk_hdr * head,u_int nchunk,size_t size,const char * id,size_t * fwsize,void ** fwptr)1898f4f56ff4SMark Johnston qat_aefw_mof_find_uof0(struct qat_softc *sc,
1899f4f56ff4SMark Johnston 	struct mof_uof_hdr *muh, struct mof_uof_chunk_hdr *head,
1900f4f56ff4SMark Johnston 	u_int nchunk, size_t size, const char *id,
1901f4f56ff4SMark Johnston 	size_t *fwsize, void **fwptr)
1902f4f56ff4SMark Johnston {
1903f4f56ff4SMark Johnston 	int i;
1904f4f56ff4SMark Johnston 	char *uof_name;
1905f4f56ff4SMark Johnston 
1906f4f56ff4SMark Johnston 	for (i = 0; i < nchunk; i++) {
1907f4f56ff4SMark Johnston 		struct mof_uof_chunk_hdr *much = &head[i];
1908f4f56ff4SMark Johnston 
1909f4f56ff4SMark Johnston 		if (strncmp(much->much_id, id, MOF_OBJ_ID_LEN))
1910f4f56ff4SMark Johnston 			return EINVAL;
1911f4f56ff4SMark Johnston 
1912f4f56ff4SMark Johnston 		if (much->much_offset + much->much_size > size)
1913f4f56ff4SMark Johnston 			return EINVAL;
1914f4f56ff4SMark Johnston 
1915f4f56ff4SMark Johnston 		if (sc->sc_mof.qmf_sym_size <= much->much_name)
1916f4f56ff4SMark Johnston 			return EINVAL;
1917f4f56ff4SMark Johnston 
1918f4f56ff4SMark Johnston 		uof_name = (char *)((uintptr_t)sc->sc_mof.qmf_sym +
1919f4f56ff4SMark Johnston 		    much->much_name);
1920f4f56ff4SMark Johnston 
1921f4f56ff4SMark Johnston 		if (!strcmp(uof_name, sc->sc_fw_uof_name)) {
1922f4f56ff4SMark Johnston 			*fwptr = (void *)((uintptr_t)muh +
1923f4f56ff4SMark Johnston 			    (uintptr_t)much->much_offset);
1924f4f56ff4SMark Johnston 			*fwsize = (size_t)much->much_size;
1925f4f56ff4SMark Johnston 			return 0;
1926f4f56ff4SMark Johnston 		}
1927f4f56ff4SMark Johnston 	}
1928f4f56ff4SMark Johnston 
1929f4f56ff4SMark Johnston 	return ENOENT;
1930f4f56ff4SMark Johnston }
1931f4f56ff4SMark Johnston 
1932f4f56ff4SMark Johnston static int
qat_aefw_mof_find_uof(struct qat_softc * sc)1933f4f56ff4SMark Johnston qat_aefw_mof_find_uof(struct qat_softc *sc)
1934f4f56ff4SMark Johnston {
1935f4f56ff4SMark Johnston 	struct mof_uof_hdr *uof_hdr, *suof_hdr;
1936f4f56ff4SMark Johnston 	u_int nuof_chunks = 0, nsuof_chunks = 0;
1937f4f56ff4SMark Johnston 	int error;
1938f4f56ff4SMark Johnston 
1939f4f56ff4SMark Johnston 	uof_hdr = sc->sc_mof.qmf_uof_objs;
1940f4f56ff4SMark Johnston 	suof_hdr = sc->sc_mof.qmf_suof_objs;
1941f4f56ff4SMark Johnston 
1942f4f56ff4SMark Johnston 	if (uof_hdr != NULL) {
1943f4f56ff4SMark Johnston 		if (uof_hdr->muh_max_chunks < uof_hdr->muh_num_chunks) {
1944f4f56ff4SMark Johnston 			return EINVAL;
1945f4f56ff4SMark Johnston 		}
1946f4f56ff4SMark Johnston 		nuof_chunks = uof_hdr->muh_num_chunks;
1947f4f56ff4SMark Johnston 	}
1948f4f56ff4SMark Johnston 	if (suof_hdr != NULL) {
1949f4f56ff4SMark Johnston 		if (suof_hdr->muh_max_chunks < suof_hdr->muh_num_chunks)
1950f4f56ff4SMark Johnston 			return EINVAL;
1951f4f56ff4SMark Johnston 		nsuof_chunks = suof_hdr->muh_num_chunks;
1952f4f56ff4SMark Johnston 	}
1953f4f56ff4SMark Johnston 
1954f4f56ff4SMark Johnston 	if (nuof_chunks + nsuof_chunks == 0)
1955f4f56ff4SMark Johnston 		return EINVAL;
1956f4f56ff4SMark Johnston 
1957f4f56ff4SMark Johnston 	if (uof_hdr != NULL) {
1958f4f56ff4SMark Johnston 		error = qat_aefw_mof_find_uof0(sc, uof_hdr,
1959f4f56ff4SMark Johnston 		    (struct mof_uof_chunk_hdr *)(uof_hdr + 1), nuof_chunks,
1960f4f56ff4SMark Johnston 		    sc->sc_mof.qmf_uof_objs_size, UOF_IMAG,
1961f4f56ff4SMark Johnston 		    &sc->sc_fw_uof_size, &sc->sc_fw_uof);
1962f4f56ff4SMark Johnston 		if (error && error != ENOENT)
1963f4f56ff4SMark Johnston 			return error;
1964f4f56ff4SMark Johnston 	}
1965f4f56ff4SMark Johnston 
1966f4f56ff4SMark Johnston 	if (suof_hdr != NULL) {
1967f4f56ff4SMark Johnston 		error = qat_aefw_mof_find_uof0(sc, suof_hdr,
1968f4f56ff4SMark Johnston 		    (struct mof_uof_chunk_hdr *)(suof_hdr + 1), nsuof_chunks,
1969f4f56ff4SMark Johnston 		    sc->sc_mof.qmf_suof_objs_size, SUOF_IMAG,
1970f4f56ff4SMark Johnston 		    &sc->sc_fw_suof_size, &sc->sc_fw_suof);
1971f4f56ff4SMark Johnston 		if (error && error != ENOENT)
1972f4f56ff4SMark Johnston 			return error;
1973f4f56ff4SMark Johnston 	}
1974f4f56ff4SMark Johnston 
1975f4f56ff4SMark Johnston 	if (sc->sc_fw_uof == NULL && sc->sc_fw_suof == NULL)
1976f4f56ff4SMark Johnston 		return ENOENT;
1977f4f56ff4SMark Johnston 
1978f4f56ff4SMark Johnston 	return 0;
1979f4f56ff4SMark Johnston }
1980f4f56ff4SMark Johnston 
1981f4f56ff4SMark Johnston static int
qat_aefw_mof_parse(struct qat_softc * sc)1982f4f56ff4SMark Johnston qat_aefw_mof_parse(struct qat_softc *sc)
1983f4f56ff4SMark Johnston {
1984f4f56ff4SMark Johnston 	const struct mof_file_hdr *mfh;
1985f4f56ff4SMark Johnston 	const struct mof_file_chunk_hdr *mfch;
1986f4f56ff4SMark Johnston 	size_t size;
1987f4f56ff4SMark Johnston 	u_int csum;
1988f4f56ff4SMark Johnston 	int error, i;
1989f4f56ff4SMark Johnston 
1990f4f56ff4SMark Johnston 	size = sc->sc_fw_mof_size;
1991f4f56ff4SMark Johnston 
1992f4f56ff4SMark Johnston 	if (size < sizeof(struct mof_file_hdr))
1993f4f56ff4SMark Johnston 		return EINVAL;
1994f4f56ff4SMark Johnston 	size -= sizeof(struct mof_file_hdr);
1995f4f56ff4SMark Johnston 
1996f4f56ff4SMark Johnston 	mfh = sc->sc_fw_mof;
1997f4f56ff4SMark Johnston 
1998f4f56ff4SMark Johnston 	if (mfh->mfh_fid != MOF_FID)
1999f4f56ff4SMark Johnston 		return EINVAL;
2000f4f56ff4SMark Johnston 
2001f4f56ff4SMark Johnston 	csum = qat_aefw_csum((char *)((uintptr_t)sc->sc_fw_mof +
2002f4f56ff4SMark Johnston 	    offsetof(struct mof_file_hdr, mfh_min_ver)),
2003f4f56ff4SMark Johnston 	    sc->sc_fw_mof_size -
2004f4f56ff4SMark Johnston 	    offsetof(struct mof_file_hdr, mfh_min_ver));
2005f4f56ff4SMark Johnston 	if (mfh->mfh_csum != csum)
2006f4f56ff4SMark Johnston 		return EINVAL;
2007f4f56ff4SMark Johnston 
2008f4f56ff4SMark Johnston 	if (mfh->mfh_min_ver != MOF_MIN_VER ||
2009f4f56ff4SMark Johnston 	    mfh->mfh_maj_ver != MOF_MAJ_VER)
2010f4f56ff4SMark Johnston 		return EINVAL;
2011f4f56ff4SMark Johnston 
2012f4f56ff4SMark Johnston 	if (mfh->mfh_max_chunks < mfh->mfh_num_chunks)
2013f4f56ff4SMark Johnston 		return EINVAL;
2014f4f56ff4SMark Johnston 
2015f4f56ff4SMark Johnston 	if (size < sizeof(struct mof_file_chunk_hdr) * mfh->mfh_num_chunks)
2016f4f56ff4SMark Johnston 		return EINVAL;
2017f4f56ff4SMark Johnston 	mfch = (const struct mof_file_chunk_hdr *)(mfh + 1);
2018f4f56ff4SMark Johnston 
2019f4f56ff4SMark Johnston 	for (i = 0; i < mfh->mfh_num_chunks; i++, mfch++) {
2020f4f56ff4SMark Johnston 		if (mfch->mfch_offset + mfch->mfch_size > sc->sc_fw_mof_size)
2021f4f56ff4SMark Johnston 			return EINVAL;
2022f4f56ff4SMark Johnston 
2023f4f56ff4SMark Johnston 		if (!strncmp(mfch->mfch_id, SYM_OBJS, MOF_OBJ_ID_LEN)) {
2024f4f56ff4SMark Johnston 			if (sc->sc_mof.qmf_sym != NULL)
2025f4f56ff4SMark Johnston 				return EINVAL;
2026f4f56ff4SMark Johnston 
2027f4f56ff4SMark Johnston 			sc->sc_mof.qmf_sym =
2028f4f56ff4SMark Johnston 			    (void *)((uintptr_t)sc->sc_fw_mof +
2029f4f56ff4SMark Johnston 			    (uintptr_t)mfch->mfch_offset + sizeof(u_int));
2030f4f56ff4SMark Johnston 			sc->sc_mof.qmf_sym_size =
2031f4f56ff4SMark Johnston 			    *(u_int *)((uintptr_t)sc->sc_fw_mof +
2032f4f56ff4SMark Johnston 			    (uintptr_t)mfch->mfch_offset);
2033f4f56ff4SMark Johnston 
2034f4f56ff4SMark Johnston 			if (sc->sc_mof.qmf_sym_size % sizeof(u_int) != 0)
2035f4f56ff4SMark Johnston 				return EINVAL;
2036f4f56ff4SMark Johnston 			if (mfch->mfch_size != sc->sc_mof.qmf_sym_size +
2037f4f56ff4SMark Johnston 			    sizeof(u_int) || mfch->mfch_size == 0)
2038f4f56ff4SMark Johnston 				return EINVAL;
2039f4f56ff4SMark Johnston 			if (*(char *)((uintptr_t)sc->sc_mof.qmf_sym +
2040f4f56ff4SMark Johnston 			    sc->sc_mof.qmf_sym_size - 1) != '\0')
2041f4f56ff4SMark Johnston 				return EINVAL;
2042f4f56ff4SMark Johnston 
2043f4f56ff4SMark Johnston 		} else if (!strncmp(mfch->mfch_id, UOF_OBJS, MOF_OBJ_ID_LEN)) {
2044f4f56ff4SMark Johnston 			if (sc->sc_mof.qmf_uof_objs != NULL)
2045f4f56ff4SMark Johnston 				return EINVAL;
2046f4f56ff4SMark Johnston 
2047f4f56ff4SMark Johnston 			sc->sc_mof.qmf_uof_objs =
2048f4f56ff4SMark Johnston 			    (void *)((uintptr_t)sc->sc_fw_mof +
2049f4f56ff4SMark Johnston 			    (uintptr_t)mfch->mfch_offset);
2050f4f56ff4SMark Johnston 			sc->sc_mof.qmf_uof_objs_size = mfch->mfch_size;
2051f4f56ff4SMark Johnston 
2052f4f56ff4SMark Johnston 		} else if (!strncmp(mfch->mfch_id, SUOF_OBJS, MOF_OBJ_ID_LEN)) {
2053f4f56ff4SMark Johnston 			if (sc->sc_mof.qmf_suof_objs != NULL)
2054f4f56ff4SMark Johnston 				return EINVAL;
2055f4f56ff4SMark Johnston 
2056f4f56ff4SMark Johnston 			sc->sc_mof.qmf_suof_objs =
2057f4f56ff4SMark Johnston 			    (void *)((uintptr_t)sc->sc_fw_mof +
2058f4f56ff4SMark Johnston 			    (uintptr_t)mfch->mfch_offset);
2059f4f56ff4SMark Johnston 			sc->sc_mof.qmf_suof_objs_size = mfch->mfch_size;
2060f4f56ff4SMark Johnston 		}
2061f4f56ff4SMark Johnston 	}
2062f4f56ff4SMark Johnston 
2063f4f56ff4SMark Johnston 	if (sc->sc_mof.qmf_sym == NULL ||
2064f4f56ff4SMark Johnston 	    (sc->sc_mof.qmf_uof_objs == NULL &&
2065f4f56ff4SMark Johnston 	    sc->sc_mof.qmf_suof_objs == NULL))
2066f4f56ff4SMark Johnston 		return EINVAL;
2067f4f56ff4SMark Johnston 
2068f4f56ff4SMark Johnston 	error = qat_aefw_mof_find_uof(sc);
2069f4f56ff4SMark Johnston 	if (error)
2070f4f56ff4SMark Johnston 		return error;
2071f4f56ff4SMark Johnston 	return 0;
2072f4f56ff4SMark Johnston }
2073f4f56ff4SMark Johnston 
2074f4f56ff4SMark Johnston static int
qat_aefw_uof_parse_image(struct qat_softc * sc,struct qat_uof_image * qui,struct uof_chunk_hdr * uch)2075f4f56ff4SMark Johnston qat_aefw_uof_parse_image(struct qat_softc *sc,
2076f4f56ff4SMark Johnston 	struct qat_uof_image *qui, struct uof_chunk_hdr *uch)
2077f4f56ff4SMark Johnston {
2078f4f56ff4SMark Johnston 	struct uof_image *image;
2079f4f56ff4SMark Johnston 	struct uof_code_page *page;
2080f4f56ff4SMark Johnston 	uintptr_t base = (uintptr_t)sc->sc_aefw_uof.qafu_obj_hdr;
2081f4f56ff4SMark Johnston 	size_t lim = uch->uch_offset + uch->uch_size, size;
2082f4f56ff4SMark Johnston 	int i, p;
2083f4f56ff4SMark Johnston 
2084f4f56ff4SMark Johnston 	size = uch->uch_size;
2085f4f56ff4SMark Johnston 	if (size < sizeof(struct uof_image))
2086f4f56ff4SMark Johnston 		return EINVAL;
2087f4f56ff4SMark Johnston 	size -= sizeof(struct uof_image);
2088f4f56ff4SMark Johnston 
2089f4f56ff4SMark Johnston 	qui->qui_image = image =
2090f4f56ff4SMark Johnston 	    (struct uof_image *)(base + uch->uch_offset);
2091f4f56ff4SMark Johnston 
2092f4f56ff4SMark Johnston #define ASSIGN_OBJ_TAB(np, typep, type, base, off, lim)			\
2093f4f56ff4SMark Johnston do {									\
2094f4f56ff4SMark Johnston 	u_int nent;							\
2095f4f56ff4SMark Johnston 	nent = ((struct uof_obj_table *)((base) + (off)))->uot_nentries;\
2096f4f56ff4SMark Johnston 	if ((lim) < off + sizeof(struct uof_obj_table) +		\
2097f4f56ff4SMark Johnston 	    sizeof(type) * nent)					\
2098f4f56ff4SMark Johnston 		return EINVAL;						\
2099f4f56ff4SMark Johnston 	*(np) = nent;							\
2100f4f56ff4SMark Johnston 	if (nent > 0)							\
2101f4f56ff4SMark Johnston 		*(typep) = (type)((struct uof_obj_table *)		\
2102f4f56ff4SMark Johnston 		    ((base) + (off)) + 1);				\
2103f4f56ff4SMark Johnston 	else								\
2104f4f56ff4SMark Johnston 		*(typep) = NULL;					\
2105f4f56ff4SMark Johnston } while (0)
2106f4f56ff4SMark Johnston 
2107f4f56ff4SMark Johnston 	ASSIGN_OBJ_TAB(&qui->qui_num_ae_reg, &qui->qui_ae_reg,
2108f4f56ff4SMark Johnston 	    struct uof_ae_reg *, base, image->ui_reg_tab, lim);
2109f4f56ff4SMark Johnston 	ASSIGN_OBJ_TAB(&qui->qui_num_init_reg_sym, &qui->qui_init_reg_sym,
2110f4f56ff4SMark Johnston 	    struct uof_init_reg_sym *, base, image->ui_init_reg_sym_tab, lim);
2111f4f56ff4SMark Johnston 	ASSIGN_OBJ_TAB(&qui->qui_num_sbreak, &qui->qui_sbreak,
2112f4f56ff4SMark Johnston 	    struct qui_sbreak *, base, image->ui_sbreak_tab, lim);
2113f4f56ff4SMark Johnston 
2114f4f56ff4SMark Johnston 	if (size < sizeof(struct uof_code_page) * image->ui_num_pages)
2115f4f56ff4SMark Johnston 		return EINVAL;
2116f4f56ff4SMark Johnston 	if (nitems(qui->qui_pages) < image->ui_num_pages)
2117f4f56ff4SMark Johnston 		return EINVAL;
2118f4f56ff4SMark Johnston 
2119f4f56ff4SMark Johnston 	page = (struct uof_code_page *)(image + 1);
2120f4f56ff4SMark Johnston 
2121f4f56ff4SMark Johnston 	for (p = 0; p < image->ui_num_pages; p++, page++) {
2122f4f56ff4SMark Johnston 		struct qat_uof_page *qup = &qui->qui_pages[p];
2123f4f56ff4SMark Johnston 		struct uof_code_area *uca;
2124f4f56ff4SMark Johnston 
2125f4f56ff4SMark Johnston 		qup->qup_page_num = page->ucp_page_num;
2126f4f56ff4SMark Johnston 		qup->qup_def_page = page->ucp_def_page;
2127f4f56ff4SMark Johnston 		qup->qup_page_region = page->ucp_page_region;
2128f4f56ff4SMark Johnston 		qup->qup_beg_vaddr = page->ucp_beg_vaddr;
2129f4f56ff4SMark Johnston 		qup->qup_beg_paddr = page->ucp_beg_paddr;
2130f4f56ff4SMark Johnston 
2131f4f56ff4SMark Johnston 		ASSIGN_OBJ_TAB(&qup->qup_num_uc_var, &qup->qup_uc_var,
2132f4f56ff4SMark Johnston 		    struct uof_uword_fixup *, base,
2133f4f56ff4SMark Johnston 		    page->ucp_uc_var_tab, lim);
2134f4f56ff4SMark Johnston 		ASSIGN_OBJ_TAB(&qup->qup_num_imp_var, &qup->qup_imp_var,
2135f4f56ff4SMark Johnston 		    struct uof_import_var *, base,
2136f4f56ff4SMark Johnston 		    page->ucp_imp_var_tab, lim);
2137f4f56ff4SMark Johnston 		ASSIGN_OBJ_TAB(&qup->qup_num_imp_expr, &qup->qup_imp_expr,
2138f4f56ff4SMark Johnston 		    struct uof_uword_fixup *, base,
2139f4f56ff4SMark Johnston 		    page->ucp_imp_expr_tab, lim);
2140f4f56ff4SMark Johnston 		ASSIGN_OBJ_TAB(&qup->qup_num_neigh_reg, &qup->qup_neigh_reg,
2141f4f56ff4SMark Johnston 		    struct uof_uword_fixup *, base,
2142f4f56ff4SMark Johnston 		    page->ucp_neigh_reg_tab, lim);
2143f4f56ff4SMark Johnston 
2144f4f56ff4SMark Johnston 		if (lim < page->ucp_code_area + sizeof(struct uof_code_area))
2145f4f56ff4SMark Johnston 			return EINVAL;
2146f4f56ff4SMark Johnston 
2147f4f56ff4SMark Johnston 		uca = (struct uof_code_area *)(base + page->ucp_code_area);
2148f4f56ff4SMark Johnston 		qup->qup_num_micro_words = uca->uca_num_micro_words;
2149f4f56ff4SMark Johnston 
2150f4f56ff4SMark Johnston 		ASSIGN_OBJ_TAB(&qup->qup_num_uw_blocks, &qup->qup_uw_blocks,
2151f4f56ff4SMark Johnston 		    struct qat_uof_uword_block *, base,
2152f4f56ff4SMark Johnston 		    uca->uca_uword_block_tab, lim);
2153f4f56ff4SMark Johnston 
2154f4f56ff4SMark Johnston 		for (i = 0; i < qup->qup_num_uw_blocks; i++) {
2155f4f56ff4SMark Johnston 			u_int uwordoff = ((struct uof_uword_block *)(
2156f4f56ff4SMark Johnston 			    &qup->qup_uw_blocks[i]))->uub_uword_offset;
2157f4f56ff4SMark Johnston 
2158f4f56ff4SMark Johnston 			if (lim < uwordoff)
2159f4f56ff4SMark Johnston 				return EINVAL;
2160f4f56ff4SMark Johnston 
2161f4f56ff4SMark Johnston 			qup->qup_uw_blocks[i].quub_micro_words =
2162f4f56ff4SMark Johnston 			    (base + uwordoff);
2163f4f56ff4SMark Johnston 		}
2164f4f56ff4SMark Johnston 	}
2165f4f56ff4SMark Johnston 
2166f4f56ff4SMark Johnston #undef ASSIGN_OBJ_TAB
2167f4f56ff4SMark Johnston 
2168f4f56ff4SMark Johnston 	return 0;
2169f4f56ff4SMark Johnston }
2170f4f56ff4SMark Johnston 
2171f4f56ff4SMark Johnston static int
qat_aefw_uof_parse_images(struct qat_softc * sc)2172f4f56ff4SMark Johnston qat_aefw_uof_parse_images(struct qat_softc *sc)
2173f4f56ff4SMark Johnston {
2174f4f56ff4SMark Johnston 	struct uof_chunk_hdr *uch = NULL;
2175f4f56ff4SMark Johnston 	int i, error;
2176f4f56ff4SMark Johnston 
2177f4f56ff4SMark Johnston 	for (i = 0; i < MAX_NUM_AE * MAX_AE_CTX; i++) {
2178f4f56ff4SMark Johnston 		uch = qat_aefw_uof_find_chunk(sc, UOF_IMAG, uch);
2179f4f56ff4SMark Johnston 		if (uch == NULL)
2180f4f56ff4SMark Johnston 			break;
2181f4f56ff4SMark Johnston 
2182f4f56ff4SMark Johnston 		if (i >= nitems(sc->sc_aefw_uof.qafu_imgs))
2183f4f56ff4SMark Johnston 			return ENOENT;
2184f4f56ff4SMark Johnston 
2185f4f56ff4SMark Johnston 		error = qat_aefw_uof_parse_image(sc, &sc->sc_aefw_uof.qafu_imgs[i], uch);
2186f4f56ff4SMark Johnston 		if (error)
2187f4f56ff4SMark Johnston 			return error;
2188f4f56ff4SMark Johnston 
2189f4f56ff4SMark Johnston 		sc->sc_aefw_uof.qafu_num_imgs++;
2190f4f56ff4SMark Johnston 	}
2191f4f56ff4SMark Johnston 
2192f4f56ff4SMark Johnston 	return 0;
2193f4f56ff4SMark Johnston }
2194f4f56ff4SMark Johnston 
2195f4f56ff4SMark Johnston static int
qat_aefw_uof_parse(struct qat_softc * sc)2196f4f56ff4SMark Johnston qat_aefw_uof_parse(struct qat_softc *sc)
2197f4f56ff4SMark Johnston {
2198f4f56ff4SMark Johnston 	struct uof_file_hdr *ufh;
2199f4f56ff4SMark Johnston 	struct uof_file_chunk_hdr *ufch;
2200f4f56ff4SMark Johnston 	struct uof_obj_hdr *uoh;
2201f4f56ff4SMark Johnston 	struct uof_chunk_hdr *uch;
2202f4f56ff4SMark Johnston 	void *uof = NULL;
2203f4f56ff4SMark Johnston 	size_t size, uof_size, hdr_size;
2204f4f56ff4SMark Johnston 	uintptr_t base;
2205f4f56ff4SMark Johnston 	u_int csum;
2206f4f56ff4SMark Johnston 	int i;
2207f4f56ff4SMark Johnston 
2208f4f56ff4SMark Johnston 	size = sc->sc_fw_uof_size;
2209f4f56ff4SMark Johnston 	if (size < MIN_UOF_SIZE)
2210f4f56ff4SMark Johnston 		return EINVAL;
2211f4f56ff4SMark Johnston 	size -= sizeof(struct uof_file_hdr);
2212f4f56ff4SMark Johnston 
2213f4f56ff4SMark Johnston 	ufh = sc->sc_fw_uof;
2214f4f56ff4SMark Johnston 
2215f4f56ff4SMark Johnston 	if (ufh->ufh_id != UOF_FID)
2216f4f56ff4SMark Johnston 		return EINVAL;
2217f4f56ff4SMark Johnston 	if (ufh->ufh_min_ver != UOF_MIN_VER || ufh->ufh_maj_ver != UOF_MAJ_VER)
2218f4f56ff4SMark Johnston 		return EINVAL;
2219f4f56ff4SMark Johnston 
2220f4f56ff4SMark Johnston 	if (ufh->ufh_max_chunks < ufh->ufh_num_chunks)
2221f4f56ff4SMark Johnston 		return EINVAL;
2222f4f56ff4SMark Johnston 	if (size < sizeof(struct uof_file_chunk_hdr) * ufh->ufh_num_chunks)
2223f4f56ff4SMark Johnston 		return EINVAL;
2224f4f56ff4SMark Johnston 	ufch = (struct uof_file_chunk_hdr *)(ufh + 1);
2225f4f56ff4SMark Johnston 
2226f4f56ff4SMark Johnston 	uof_size = 0;
2227f4f56ff4SMark Johnston 	for (i = 0; i < ufh->ufh_num_chunks; i++, ufch++) {
2228f4f56ff4SMark Johnston 		if (ufch->ufch_offset + ufch->ufch_size > sc->sc_fw_uof_size)
2229f4f56ff4SMark Johnston 			return EINVAL;
2230f4f56ff4SMark Johnston 
2231f4f56ff4SMark Johnston 		if (!strncmp(ufch->ufch_id, UOF_OBJS, UOF_OBJ_ID_LEN)) {
2232f4f56ff4SMark Johnston 			if (uof != NULL)
2233f4f56ff4SMark Johnston 				return EINVAL;
2234f4f56ff4SMark Johnston 
2235f4f56ff4SMark Johnston 			uof =
2236f4f56ff4SMark Johnston 			    (void *)((uintptr_t)sc->sc_fw_uof +
2237f4f56ff4SMark Johnston 			    ufch->ufch_offset);
2238f4f56ff4SMark Johnston 			uof_size = ufch->ufch_size;
2239f4f56ff4SMark Johnston 
2240f4f56ff4SMark Johnston 			csum = qat_aefw_csum(uof, uof_size);
2241f4f56ff4SMark Johnston 			if (csum != ufch->ufch_csum)
2242f4f56ff4SMark Johnston 				return EINVAL;
2243f4f56ff4SMark Johnston 		}
2244f4f56ff4SMark Johnston 	}
2245f4f56ff4SMark Johnston 
2246f4f56ff4SMark Johnston 	if (uof == NULL)
2247f4f56ff4SMark Johnston 		return ENOENT;
2248f4f56ff4SMark Johnston 
2249f4f56ff4SMark Johnston 	size = uof_size;
2250f4f56ff4SMark Johnston 	if (size < sizeof(struct uof_obj_hdr))
2251f4f56ff4SMark Johnston 		return EINVAL;
2252f4f56ff4SMark Johnston 	size -= sizeof(struct uof_obj_hdr);
2253f4f56ff4SMark Johnston 
2254f4f56ff4SMark Johnston 	uoh = uof;
2255f4f56ff4SMark Johnston 
2256f4f56ff4SMark Johnston 	if (size < sizeof(struct uof_chunk_hdr) * uoh->uoh_num_chunks)
2257f4f56ff4SMark Johnston 		return EINVAL;
2258f4f56ff4SMark Johnston 
2259f4f56ff4SMark Johnston 	/* Check if the UOF objects are compatible with the chip */
2260f4f56ff4SMark Johnston 	if ((uoh->uoh_cpu_type & sc->sc_hw.qhw_prod_type) == 0)
2261f4f56ff4SMark Johnston 		return ENOTSUP;
2262f4f56ff4SMark Johnston 
2263f4f56ff4SMark Johnston 	if (uoh->uoh_min_cpu_ver > sc->sc_rev ||
2264f4f56ff4SMark Johnston 	    uoh->uoh_max_cpu_ver < sc->sc_rev)
2265f4f56ff4SMark Johnston 		return ENOTSUP;
2266f4f56ff4SMark Johnston 
2267f4f56ff4SMark Johnston 	sc->sc_aefw_uof.qafu_size = uof_size;
2268f4f56ff4SMark Johnston 	sc->sc_aefw_uof.qafu_obj_hdr = uoh;
2269f4f56ff4SMark Johnston 
2270f4f56ff4SMark Johnston 	base = (uintptr_t)sc->sc_aefw_uof.qafu_obj_hdr;
2271f4f56ff4SMark Johnston 
2272f4f56ff4SMark Johnston 	/* map uof string-table */
2273f4f56ff4SMark Johnston 	uch = qat_aefw_uof_find_chunk(sc, UOF_STRT, NULL);
2274f4f56ff4SMark Johnston 	if (uch != NULL) {
2275f4f56ff4SMark Johnston 		hdr_size = offsetof(struct uof_str_tab, ust_strings);
2276f4f56ff4SMark Johnston 		sc->sc_aefw_uof.qafu_str_tab =
2277f4f56ff4SMark Johnston 		    (void *)(base + uch->uch_offset + hdr_size);
2278f4f56ff4SMark Johnston 		sc->sc_aefw_uof.qafu_str_tab_size = uch->uch_size - hdr_size;
2279f4f56ff4SMark Johnston 	}
2280f4f56ff4SMark Johnston 
2281f4f56ff4SMark Johnston 	/* get ustore mem inits table -- should be only one */
2282f4f56ff4SMark Johnston 	uch = qat_aefw_uof_find_chunk(sc, UOF_IMEM, NULL);
2283f4f56ff4SMark Johnston 	if (uch != NULL) {
2284f4f56ff4SMark Johnston 		if (uch->uch_size < sizeof(struct uof_obj_table))
2285f4f56ff4SMark Johnston 			return EINVAL;
2286f4f56ff4SMark Johnston 		sc->sc_aefw_uof.qafu_num_init_mem = ((struct uof_obj_table *)(base +
2287f4f56ff4SMark Johnston 		    uch->uch_offset))->uot_nentries;
2288f4f56ff4SMark Johnston 		if (sc->sc_aefw_uof.qafu_num_init_mem) {
2289f4f56ff4SMark Johnston 			sc->sc_aefw_uof.qafu_init_mem =
2290f4f56ff4SMark Johnston 			    (struct uof_init_mem *)(base + uch->uch_offset +
2291f4f56ff4SMark Johnston 			    sizeof(struct uof_obj_table));
2292f4f56ff4SMark Johnston 			sc->sc_aefw_uof.qafu_init_mem_size =
2293f4f56ff4SMark Johnston 			    uch->uch_size - sizeof(struct uof_obj_table);
2294f4f56ff4SMark Johnston 		}
2295f4f56ff4SMark Johnston 	}
2296f4f56ff4SMark Johnston 
2297f4f56ff4SMark Johnston 	uch = qat_aefw_uof_find_chunk(sc, UOF_MSEG, NULL);
2298f4f56ff4SMark Johnston 	if (uch != NULL) {
2299f4f56ff4SMark Johnston 		if (uch->uch_size < sizeof(struct uof_obj_table) +
2300f4f56ff4SMark Johnston 		    sizeof(struct uof_var_mem_seg))
2301f4f56ff4SMark Johnston 			return EINVAL;
2302f4f56ff4SMark Johnston 		sc->sc_aefw_uof.qafu_var_mem_seg =
2303f4f56ff4SMark Johnston 		    (struct uof_var_mem_seg *)(base + uch->uch_offset +
2304f4f56ff4SMark Johnston 		    sizeof(struct uof_obj_table));
2305f4f56ff4SMark Johnston 	}
2306f4f56ff4SMark Johnston 
2307f4f56ff4SMark Johnston 	return qat_aefw_uof_parse_images(sc);
2308f4f56ff4SMark Johnston }
2309f4f56ff4SMark Johnston 
2310f4f56ff4SMark Johnston static int
qat_aefw_suof_parse_image(struct qat_softc * sc,struct qat_suof_image * qsi,struct suof_chunk_hdr * sch)2311f4f56ff4SMark Johnston qat_aefw_suof_parse_image(struct qat_softc *sc, struct qat_suof_image *qsi,
2312f4f56ff4SMark Johnston     struct suof_chunk_hdr *sch)
2313f4f56ff4SMark Johnston {
2314f4f56ff4SMark Johnston 	struct qat_aefw_suof *qafs = &sc->sc_aefw_suof;
2315f4f56ff4SMark Johnston 	struct simg_ae_mode *ae_mode;
2316f4f56ff4SMark Johnston 	u_int maj_ver;
2317f4f56ff4SMark Johnston 
2318f4f56ff4SMark Johnston 	qsi->qsi_simg_buf = qafs->qafs_suof_buf + sch->sch_offset +
2319f4f56ff4SMark Johnston 	    sizeof(struct suof_obj_hdr);
2320f4f56ff4SMark Johnston 	qsi->qsi_simg_len =
2321f4f56ff4SMark Johnston 	    ((struct suof_obj_hdr *)
2322f4f56ff4SMark Johnston 	    (qafs->qafs_suof_buf + sch->sch_offset))->soh_img_length;
2323f4f56ff4SMark Johnston 
2324f4f56ff4SMark Johnston 	qsi->qsi_css_header = qsi->qsi_simg_buf;
2325f4f56ff4SMark Johnston 	qsi->qsi_css_key = qsi->qsi_css_header + sizeof(struct css_hdr);
2326f4f56ff4SMark Johnston 	qsi->qsi_css_signature = qsi->qsi_css_key +
2327f4f56ff4SMark Johnston 	    CSS_FWSK_MODULUS_LEN + CSS_FWSK_EXPONENT_LEN;
2328f4f56ff4SMark Johnston 	qsi->qsi_css_simg = qsi->qsi_css_signature + CSS_SIGNATURE_LEN;
2329f4f56ff4SMark Johnston 
2330f4f56ff4SMark Johnston 	ae_mode = (struct simg_ae_mode *)qsi->qsi_css_simg;
2331f4f56ff4SMark Johnston 	qsi->qsi_ae_mask = ae_mode->sam_ae_mask;
2332f4f56ff4SMark Johnston 	qsi->qsi_simg_name = (u_long)&ae_mode->sam_simg_name;
2333f4f56ff4SMark Johnston 	qsi->qsi_appmeta_data = (u_long)&ae_mode->sam_appmeta_data;
2334f4f56ff4SMark Johnston 	qsi->qsi_fw_type = ae_mode->sam_fw_type;
2335f4f56ff4SMark Johnston 
2336f4f56ff4SMark Johnston 	if (ae_mode->sam_dev_type != sc->sc_hw.qhw_prod_type)
2337f4f56ff4SMark Johnston 		return EINVAL;
2338f4f56ff4SMark Johnston 
2339f4f56ff4SMark Johnston 	maj_ver = (QAT_PID_MAJOR_REV | (sc->sc_rev & QAT_PID_MINOR_REV)) & 0xff;
2340f4f56ff4SMark Johnston 	if ((maj_ver > ae_mode->sam_devmax_ver) ||
2341f4f56ff4SMark Johnston 	    (maj_ver < ae_mode->sam_devmin_ver)) {
2342f4f56ff4SMark Johnston 		return EINVAL;
2343f4f56ff4SMark Johnston 	}
2344f4f56ff4SMark Johnston 
2345f4f56ff4SMark Johnston 	return 0;
2346f4f56ff4SMark Johnston }
2347f4f56ff4SMark Johnston 
2348f4f56ff4SMark Johnston static int
qat_aefw_suof_parse(struct qat_softc * sc)2349f4f56ff4SMark Johnston qat_aefw_suof_parse(struct qat_softc *sc)
2350f4f56ff4SMark Johnston {
2351f4f56ff4SMark Johnston 	struct suof_file_hdr *sfh;
2352f4f56ff4SMark Johnston 	struct suof_chunk_hdr *sch;
2353f4f56ff4SMark Johnston 	struct qat_aefw_suof *qafs = &sc->sc_aefw_suof;
2354f4f56ff4SMark Johnston 	struct qat_suof_image *qsi;
2355f4f56ff4SMark Johnston 	size_t size;
2356f4f56ff4SMark Johnston 	u_int csum;
2357f4f56ff4SMark Johnston 	int ae0_img = MAX_AE;
2358f4f56ff4SMark Johnston 	int i, error;
2359f4f56ff4SMark Johnston 
2360f4f56ff4SMark Johnston 	size = sc->sc_fw_suof_size;
2361f4f56ff4SMark Johnston 	if (size < sizeof(struct suof_file_hdr))
2362f4f56ff4SMark Johnston 		return EINVAL;
2363f4f56ff4SMark Johnston 
2364f4f56ff4SMark Johnston 	sfh = sc->sc_fw_suof;
2365f4f56ff4SMark Johnston 
2366f4f56ff4SMark Johnston 	if (sfh->sfh_file_id != SUOF_FID)
2367f4f56ff4SMark Johnston 		return EINVAL;
2368f4f56ff4SMark Johnston 	if (sfh->sfh_fw_type != 0)
2369f4f56ff4SMark Johnston 		return EINVAL;
2370f4f56ff4SMark Johnston 	if (sfh->sfh_num_chunks <= 1)
2371f4f56ff4SMark Johnston 		return EINVAL;
2372f4f56ff4SMark Johnston 	if (sfh->sfh_min_ver != SUOF_MIN_VER ||
2373f4f56ff4SMark Johnston 	    sfh->sfh_maj_ver != SUOF_MAJ_VER)
2374f4f56ff4SMark Johnston 		return EINVAL;
2375f4f56ff4SMark Johnston 
2376f4f56ff4SMark Johnston 	csum = qat_aefw_csum((char *)&sfh->sfh_min_ver,
2377f4f56ff4SMark Johnston 	    size - offsetof(struct suof_file_hdr, sfh_min_ver));
2378f4f56ff4SMark Johnston 	if (csum != sfh->sfh_check_sum)
2379f4f56ff4SMark Johnston 		return EINVAL;
2380f4f56ff4SMark Johnston 
2381f4f56ff4SMark Johnston 	size -= sizeof(struct suof_file_hdr);
2382f4f56ff4SMark Johnston 
2383f4f56ff4SMark Johnston 	qafs->qafs_file_id = SUOF_FID;
2384f4f56ff4SMark Johnston 	qafs->qafs_suof_buf = sc->sc_fw_suof;
2385f4f56ff4SMark Johnston 	qafs->qafs_suof_size = sc->sc_fw_suof_size;
2386f4f56ff4SMark Johnston 	qafs->qafs_check_sum = sfh->sfh_check_sum;
2387f4f56ff4SMark Johnston 	qafs->qafs_min_ver = sfh->sfh_min_ver;
2388f4f56ff4SMark Johnston 	qafs->qafs_maj_ver = sfh->sfh_maj_ver;
2389f4f56ff4SMark Johnston 	qafs->qafs_fw_type = sfh->sfh_fw_type;
2390f4f56ff4SMark Johnston 
2391f4f56ff4SMark Johnston 	if (size < sizeof(struct suof_chunk_hdr))
2392f4f56ff4SMark Johnston 		return EINVAL;
2393f4f56ff4SMark Johnston 	sch = (struct suof_chunk_hdr *)(sfh + 1);
2394f4f56ff4SMark Johnston 	size -= sizeof(struct suof_chunk_hdr);
2395f4f56ff4SMark Johnston 
2396f4f56ff4SMark Johnston 	if (size < sizeof(struct suof_str_tab))
2397f4f56ff4SMark Johnston 		return EINVAL;
2398f4f56ff4SMark Johnston 	size -= offsetof(struct suof_str_tab, sst_strings);
2399f4f56ff4SMark Johnston 
2400f4f56ff4SMark Johnston 	qafs->qafs_sym_size = ((struct suof_str_tab *)
2401f4f56ff4SMark Johnston 	    (qafs->qafs_suof_buf + sch->sch_offset))->sst_tab_length;
2402f4f56ff4SMark Johnston 	if (size < qafs->qafs_sym_size)
2403f4f56ff4SMark Johnston 		return EINVAL;
2404f4f56ff4SMark Johnston 	qafs->qafs_sym_str = qafs->qafs_suof_buf + sch->sch_offset +
2405f4f56ff4SMark Johnston 	    offsetof(struct suof_str_tab, sst_strings);
2406f4f56ff4SMark Johnston 
2407f4f56ff4SMark Johnston 	qafs->qafs_num_simgs = sfh->sfh_num_chunks - 1;
2408f4f56ff4SMark Johnston 	if (qafs->qafs_num_simgs == 0)
2409f4f56ff4SMark Johnston 		return EINVAL;
2410f4f56ff4SMark Johnston 
2411f4f56ff4SMark Johnston 	qsi = qat_alloc_mem(
2412f4f56ff4SMark Johnston 	    sizeof(struct qat_suof_image) * qafs->qafs_num_simgs);
2413f4f56ff4SMark Johnston 	qafs->qafs_simg = qsi;
2414f4f56ff4SMark Johnston 
2415f4f56ff4SMark Johnston 	for (i = 0; i < qafs->qafs_num_simgs; i++) {
2416f4f56ff4SMark Johnston 		error = qat_aefw_suof_parse_image(sc, &qsi[i], &sch[i + 1]);
2417f4f56ff4SMark Johnston 		if (error)
2418f4f56ff4SMark Johnston 			return error;
2419f4f56ff4SMark Johnston 		if ((qsi[i].qsi_ae_mask & 0x1) != 0)
2420f4f56ff4SMark Johnston 			ae0_img = i;
2421f4f56ff4SMark Johnston 	}
2422f4f56ff4SMark Johnston 
2423f4f56ff4SMark Johnston 	if (ae0_img != qafs->qafs_num_simgs - 1) {
2424f4f56ff4SMark Johnston 		struct qat_suof_image last_qsi;
2425f4f56ff4SMark Johnston 
2426f4f56ff4SMark Johnston 		memcpy(&last_qsi, &qsi[qafs->qafs_num_simgs - 1],
2427f4f56ff4SMark Johnston 		    sizeof(struct qat_suof_image));
2428f4f56ff4SMark Johnston 		memcpy(&qsi[qafs->qafs_num_simgs - 1], &qsi[ae0_img],
2429f4f56ff4SMark Johnston 		    sizeof(struct qat_suof_image));
2430f4f56ff4SMark Johnston 		memcpy(&qsi[ae0_img], &last_qsi,
2431f4f56ff4SMark Johnston 		    sizeof(struct qat_suof_image));
2432f4f56ff4SMark Johnston 	}
2433f4f56ff4SMark Johnston 
2434f4f56ff4SMark Johnston 	return 0;
2435f4f56ff4SMark Johnston }
2436f4f56ff4SMark Johnston 
2437f4f56ff4SMark Johnston static int
qat_aefw_alloc_auth_dmamem(struct qat_softc * sc,char * image,size_t size,struct qat_dmamem * dma)2438f4f56ff4SMark Johnston qat_aefw_alloc_auth_dmamem(struct qat_softc *sc, char *image, size_t size,
2439f4f56ff4SMark Johnston     struct qat_dmamem *dma)
2440f4f56ff4SMark Johnston {
2441f4f56ff4SMark Johnston 	struct css_hdr *css = (struct css_hdr *)image;
2442f4f56ff4SMark Johnston 	struct auth_chunk *auth_chunk;
2443f4f56ff4SMark Johnston 	struct fw_auth_desc *auth_desc;
2444f4f56ff4SMark Johnston 	size_t mapsize, simg_offset = sizeof(struct auth_chunk);
2445f4f56ff4SMark Johnston 	bus_size_t bus_addr;
2446f4f56ff4SMark Johnston 	uintptr_t virt_addr;
2447f4f56ff4SMark Johnston 	int error;
2448f4f56ff4SMark Johnston 
2449f4f56ff4SMark Johnston 	if (size > AE_IMG_OFFSET + CSS_MAX_IMAGE_LEN)
2450f4f56ff4SMark Johnston 		return EINVAL;
2451f4f56ff4SMark Johnston 
2452f4f56ff4SMark Johnston 	mapsize = (css->css_fw_type == CSS_AE_FIRMWARE) ?
2453f4f56ff4SMark Johnston 	    CSS_AE_SIMG_LEN + simg_offset :
2454f4f56ff4SMark Johnston 	    size + CSS_FWSK_PAD_LEN + simg_offset;
2455f4f56ff4SMark Johnston 	error = qat_alloc_dmamem(sc, dma, 1, mapsize, PAGE_SIZE);
2456f4f56ff4SMark Johnston 	if (error)
2457f4f56ff4SMark Johnston 		return error;
2458f4f56ff4SMark Johnston 
2459f4f56ff4SMark Johnston 	memset(dma->qdm_dma_vaddr, 0, mapsize);
2460f4f56ff4SMark Johnston 
2461f4f56ff4SMark Johnston 	auth_chunk = dma->qdm_dma_vaddr;
2462f4f56ff4SMark Johnston 	auth_chunk->ac_chunk_size = mapsize;
2463f4f56ff4SMark Johnston 	auth_chunk->ac_chunk_bus_addr = dma->qdm_dma_seg.ds_addr;
2464f4f56ff4SMark Johnston 
2465f4f56ff4SMark Johnston 	virt_addr = (uintptr_t)dma->qdm_dma_vaddr;
2466f4f56ff4SMark Johnston 	virt_addr += simg_offset;
2467f4f56ff4SMark Johnston 	bus_addr = auth_chunk->ac_chunk_bus_addr;
2468f4f56ff4SMark Johnston 	bus_addr += simg_offset;
2469f4f56ff4SMark Johnston 
2470f4f56ff4SMark Johnston 	auth_desc = &auth_chunk->ac_fw_auth_desc;
2471f4f56ff4SMark Johnston 	auth_desc->fad_css_hdr_high = (uint64_t)bus_addr >> 32;
2472f4f56ff4SMark Johnston 	auth_desc->fad_css_hdr_low = bus_addr;
2473f4f56ff4SMark Johnston 
2474f4f56ff4SMark Johnston 	memcpy((void *)virt_addr, image, sizeof(struct css_hdr));
2475f4f56ff4SMark Johnston 	/* pub key */
2476f4f56ff4SMark Johnston 	virt_addr += sizeof(struct css_hdr);
2477f4f56ff4SMark Johnston 	bus_addr += sizeof(struct css_hdr);
2478f4f56ff4SMark Johnston 	image += sizeof(struct css_hdr);
2479f4f56ff4SMark Johnston 
2480f4f56ff4SMark Johnston 	auth_desc->fad_fwsk_pub_high = (uint64_t)bus_addr >> 32;
2481f4f56ff4SMark Johnston 	auth_desc->fad_fwsk_pub_low = bus_addr;
2482f4f56ff4SMark Johnston 
2483f4f56ff4SMark Johnston 	memcpy((void *)virt_addr, image, CSS_FWSK_MODULUS_LEN);
2484f4f56ff4SMark Johnston 	memset((void *)(virt_addr + CSS_FWSK_MODULUS_LEN), 0, CSS_FWSK_PAD_LEN);
2485f4f56ff4SMark Johnston 	memcpy((void *)(virt_addr + CSS_FWSK_MODULUS_LEN + CSS_FWSK_PAD_LEN),
2486f4f56ff4SMark Johnston 	    image + CSS_FWSK_MODULUS_LEN, sizeof(uint32_t));
2487f4f56ff4SMark Johnston 
2488f4f56ff4SMark Johnston 	virt_addr += CSS_FWSK_PUB_LEN;
2489f4f56ff4SMark Johnston 	bus_addr += CSS_FWSK_PUB_LEN;
2490f4f56ff4SMark Johnston 	image += CSS_FWSK_MODULUS_LEN + CSS_FWSK_EXPONENT_LEN;
2491f4f56ff4SMark Johnston 
2492f4f56ff4SMark Johnston 	auth_desc->fad_signature_high = (uint64_t)bus_addr >> 32;
2493f4f56ff4SMark Johnston 	auth_desc->fad_signature_low = bus_addr;
2494f4f56ff4SMark Johnston 
2495f4f56ff4SMark Johnston 	memcpy((void *)virt_addr, image, CSS_SIGNATURE_LEN);
2496f4f56ff4SMark Johnston 
2497f4f56ff4SMark Johnston 	virt_addr += CSS_SIGNATURE_LEN;
2498f4f56ff4SMark Johnston 	bus_addr += CSS_SIGNATURE_LEN;
2499f4f56ff4SMark Johnston 	image += CSS_SIGNATURE_LEN;
2500f4f56ff4SMark Johnston 
2501f4f56ff4SMark Johnston 	auth_desc->fad_img_high = (uint64_t)bus_addr >> 32;
2502f4f56ff4SMark Johnston 	auth_desc->fad_img_low = bus_addr;
2503f4f56ff4SMark Johnston 	auth_desc->fad_img_len = size - AE_IMG_OFFSET;
2504f4f56ff4SMark Johnston 
2505f4f56ff4SMark Johnston 	memcpy((void *)virt_addr, image, auth_desc->fad_img_len);
2506f4f56ff4SMark Johnston 
2507f4f56ff4SMark Johnston 	if (css->css_fw_type == CSS_AE_FIRMWARE) {
2508f4f56ff4SMark Johnston 		auth_desc->fad_img_ae_mode_data_high = auth_desc->fad_img_high;
2509f4f56ff4SMark Johnston 		auth_desc->fad_img_ae_mode_data_low = auth_desc->fad_img_low;
2510f4f56ff4SMark Johnston 
2511f4f56ff4SMark Johnston 		bus_addr += sizeof(struct simg_ae_mode);
2512f4f56ff4SMark Johnston 
2513f4f56ff4SMark Johnston 		auth_desc->fad_img_ae_init_data_high = (uint64_t)bus_addr >> 32;
2514f4f56ff4SMark Johnston 		auth_desc->fad_img_ae_init_data_low = bus_addr;
2515f4f56ff4SMark Johnston 
2516f4f56ff4SMark Johnston 		bus_addr += SIMG_AE_INIT_SEQ_LEN;
2517f4f56ff4SMark Johnston 
2518f4f56ff4SMark Johnston 		auth_desc->fad_img_ae_insts_high = (uint64_t)bus_addr >> 32;
2519f4f56ff4SMark Johnston 		auth_desc->fad_img_ae_insts_low = bus_addr;
2520f4f56ff4SMark Johnston 	} else {
2521f4f56ff4SMark Johnston 		auth_desc->fad_img_ae_insts_high = auth_desc->fad_img_high;
2522f4f56ff4SMark Johnston 		auth_desc->fad_img_ae_insts_low = auth_desc->fad_img_low;
2523f4f56ff4SMark Johnston 	}
2524f4f56ff4SMark Johnston 
2525f4f56ff4SMark Johnston 	bus_dmamap_sync(dma->qdm_dma_tag, dma->qdm_dma_map,
2526f4f56ff4SMark Johnston 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2527f4f56ff4SMark Johnston 
2528f4f56ff4SMark Johnston 	return 0;
2529f4f56ff4SMark Johnston }
2530f4f56ff4SMark Johnston 
2531f4f56ff4SMark Johnston static int
qat_aefw_auth(struct qat_softc * sc,struct qat_dmamem * dma)2532f4f56ff4SMark Johnston qat_aefw_auth(struct qat_softc *sc, struct qat_dmamem *dma)
2533f4f56ff4SMark Johnston {
2534f4f56ff4SMark Johnston 	bus_addr_t addr;
2535f4f56ff4SMark Johnston 	uint32_t fcu, sts;
2536f4f56ff4SMark Johnston 	int retry = 0;
2537f4f56ff4SMark Johnston 
2538f4f56ff4SMark Johnston 	addr = dma->qdm_dma_seg.ds_addr;
2539f4f56ff4SMark Johnston 	qat_cap_global_write_4(sc, FCU_DRAM_ADDR_HI, (uint64_t)addr >> 32);
2540f4f56ff4SMark Johnston 	qat_cap_global_write_4(sc, FCU_DRAM_ADDR_LO, addr);
2541f4f56ff4SMark Johnston 	qat_cap_global_write_4(sc, FCU_CTRL, FCU_CTRL_CMD_AUTH);
2542f4f56ff4SMark Johnston 
2543f4f56ff4SMark Johnston 	do {
2544f4f56ff4SMark Johnston 		DELAY(FW_AUTH_WAIT_PERIOD * 1000);
2545f4f56ff4SMark Johnston 		fcu = qat_cap_global_read_4(sc, FCU_STATUS);
2546f4f56ff4SMark Johnston 		sts = __SHIFTOUT(fcu, FCU_STATUS_STS);
2547f4f56ff4SMark Johnston 		if (sts == FCU_STATUS_STS_VERI_FAIL)
2548f4f56ff4SMark Johnston 			goto fail;
2549f4f56ff4SMark Johnston 		if (fcu & FCU_STATUS_AUTHFWLD &&
2550f4f56ff4SMark Johnston 		    sts == FCU_STATUS_STS_VERI_DONE) {
2551f4f56ff4SMark Johnston 			return 0;
2552f4f56ff4SMark Johnston 		}
2553f4f56ff4SMark Johnston 	} while (retry++ < FW_AUTH_MAX_RETRY);
2554f4f56ff4SMark Johnston 
2555f4f56ff4SMark Johnston fail:
2556f4f56ff4SMark Johnston 	device_printf(sc->sc_dev,
2557f4f56ff4SMark Johnston 	   "firmware authentication error: status 0x%08x retry %d\n",
2558f4f56ff4SMark Johnston 	   fcu, retry);
2559f4f56ff4SMark Johnston 	return EINVAL;
2560f4f56ff4SMark Johnston }
2561f4f56ff4SMark Johnston 
2562f4f56ff4SMark Johnston static int
qat_aefw_suof_load(struct qat_softc * sc,struct qat_dmamem * dma)2563f4f56ff4SMark Johnston qat_aefw_suof_load(struct qat_softc *sc, struct qat_dmamem *dma)
2564f4f56ff4SMark Johnston {
2565f4f56ff4SMark Johnston 	struct simg_ae_mode *ae_mode;
2566f4f56ff4SMark Johnston 	uint32_t fcu, sts, loaded;
2567f4f56ff4SMark Johnston 	u_int mask;
2568f4f56ff4SMark Johnston 	u_char ae;
2569f4f56ff4SMark Johnston 	int retry = 0;
2570f4f56ff4SMark Johnston 
2571f4f56ff4SMark Johnston 	ae_mode = (struct simg_ae_mode *)((uintptr_t)dma->qdm_dma_vaddr +
2572f4f56ff4SMark Johnston 	    sizeof(struct auth_chunk) + sizeof(struct css_hdr) +
2573f4f56ff4SMark Johnston 	    CSS_FWSK_PUB_LEN + CSS_SIGNATURE_LEN);
2574f4f56ff4SMark Johnston 
2575f4f56ff4SMark Johnston 	for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
2576f4f56ff4SMark Johnston 		if (!(mask & 1))
2577f4f56ff4SMark Johnston 			continue;
2578f4f56ff4SMark Johnston 		if (!((ae_mode->sam_ae_mask >> ae) & 0x1))
2579f4f56ff4SMark Johnston 			continue;
2580f4f56ff4SMark Johnston 		if (qat_ae_is_active(sc, ae)) {
2581f4f56ff4SMark Johnston 			device_printf(sc->sc_dev, "AE %d is active\n", ae);
2582f4f56ff4SMark Johnston 			return EINVAL;
2583f4f56ff4SMark Johnston 		}
2584f4f56ff4SMark Johnston 		qat_cap_global_write_4(sc, FCU_CTRL,
2585f4f56ff4SMark Johnston 		    FCU_CTRL_CMD_LOAD | __SHIFTIN(ae, FCU_CTRL_AE));
2586f4f56ff4SMark Johnston 		do {
2587f4f56ff4SMark Johnston 			DELAY(FW_AUTH_WAIT_PERIOD * 1000);
2588f4f56ff4SMark Johnston 			fcu = qat_cap_global_read_4(sc, FCU_STATUS);
2589f4f56ff4SMark Johnston 			sts = __SHIFTOUT(fcu, FCU_STATUS_STS);
2590f4f56ff4SMark Johnston 			loaded = __SHIFTOUT(fcu, FCU_STATUS_LOADED_AE);
2591f4f56ff4SMark Johnston 			if (sts == FCU_STATUS_STS_LOAD_DONE &&
2592f4f56ff4SMark Johnston 			    (loaded & (1 << ae))) {
2593f4f56ff4SMark Johnston 				break;
2594f4f56ff4SMark Johnston 			}
2595f4f56ff4SMark Johnston 		} while (retry++ < FW_AUTH_MAX_RETRY);
2596f4f56ff4SMark Johnston 
2597f4f56ff4SMark Johnston 		if (retry > FW_AUTH_MAX_RETRY) {
2598f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
2599f4f56ff4SMark Johnston 			    "firmware load timeout: status %08x\n", fcu);
2600f4f56ff4SMark Johnston 			return EINVAL;
2601f4f56ff4SMark Johnston 		}
2602f4f56ff4SMark Johnston 	}
2603f4f56ff4SMark Johnston 
2604f4f56ff4SMark Johnston 	return 0;
2605f4f56ff4SMark Johnston }
2606f4f56ff4SMark Johnston 
2607f4f56ff4SMark Johnston static int
qat_aefw_suof_write(struct qat_softc * sc)2608f4f56ff4SMark Johnston qat_aefw_suof_write(struct qat_softc *sc)
2609f4f56ff4SMark Johnston {
2610f4f56ff4SMark Johnston 	struct qat_suof_image *qsi;
2611f4f56ff4SMark Johnston 	int i, error = 0;
2612f4f56ff4SMark Johnston 
2613f4f56ff4SMark Johnston 	for (i = 0; i < sc->sc_aefw_suof.qafs_num_simgs; i++) {
2614f4f56ff4SMark Johnston 		qsi = &sc->sc_aefw_suof.qafs_simg[i];
2615f4f56ff4SMark Johnston 		error = qat_aefw_alloc_auth_dmamem(sc, qsi->qsi_simg_buf,
2616f4f56ff4SMark Johnston 		    qsi->qsi_simg_len, &qsi->qsi_dma);
2617f4f56ff4SMark Johnston 		if (error)
2618f4f56ff4SMark Johnston 			return error;
2619f4f56ff4SMark Johnston 		error = qat_aefw_auth(sc, &qsi->qsi_dma);
2620f4f56ff4SMark Johnston 		if (error) {
2621f4f56ff4SMark Johnston 			qat_free_dmamem(sc, &qsi->qsi_dma);
2622f4f56ff4SMark Johnston 			return error;
2623f4f56ff4SMark Johnston 		}
2624f4f56ff4SMark Johnston 		error = qat_aefw_suof_load(sc, &qsi->qsi_dma);
2625f4f56ff4SMark Johnston 		if (error) {
2626f4f56ff4SMark Johnston 			qat_free_dmamem(sc, &qsi->qsi_dma);
2627f4f56ff4SMark Johnston 			return error;
2628f4f56ff4SMark Johnston 		}
2629f4f56ff4SMark Johnston 		qat_free_dmamem(sc, &qsi->qsi_dma);
2630f4f56ff4SMark Johnston 	}
2631f4f56ff4SMark Johnston 	qat_free_mem(sc->sc_aefw_suof.qafs_simg);
2632f4f56ff4SMark Johnston 
2633f4f56ff4SMark Johnston 	return 0;
2634f4f56ff4SMark Johnston }
2635f4f56ff4SMark Johnston 
2636f4f56ff4SMark Johnston static int
qat_aefw_uof_assign_image(struct qat_softc * sc,struct qat_ae * qae,struct qat_uof_image * qui)2637f4f56ff4SMark Johnston qat_aefw_uof_assign_image(struct qat_softc *sc, struct qat_ae *qae,
2638f4f56ff4SMark Johnston 	struct qat_uof_image *qui)
2639f4f56ff4SMark Johnston {
2640f4f56ff4SMark Johnston 	struct qat_ae_slice *slice;
2641f4f56ff4SMark Johnston 	int i, npages, nregions;
2642f4f56ff4SMark Johnston 
2643f4f56ff4SMark Johnston 	if (qae->qae_num_slices >= nitems(qae->qae_slices))
2644f4f56ff4SMark Johnston 		return ENOENT;
2645f4f56ff4SMark Johnston 
2646f4f56ff4SMark Johnston 	if (qui->qui_image->ui_ae_mode &
2647f4f56ff4SMark Johnston 	    (AE_MODE_RELOAD_CTX_SHARED | AE_MODE_SHARED_USTORE)) {
2648f4f56ff4SMark Johnston 		/* XXX */
2649f4f56ff4SMark Johnston 		device_printf(sc->sc_dev,
2650f4f56ff4SMark Johnston 		    "shared ae mode is not supported yet\n");
2651f4f56ff4SMark Johnston 		return ENOTSUP;
2652f4f56ff4SMark Johnston 	}
2653f4f56ff4SMark Johnston 
2654f4f56ff4SMark Johnston 	qae->qae_shareable_ustore = 0; /* XXX */
2655f4f56ff4SMark Johnston 	qae->qae_effect_ustore_size = USTORE_SIZE;
2656f4f56ff4SMark Johnston 
2657f4f56ff4SMark Johnston 	slice = &qae->qae_slices[qae->qae_num_slices];
2658f4f56ff4SMark Johnston 
2659f4f56ff4SMark Johnston 	slice->qas_image = qui;
2660f4f56ff4SMark Johnston 	slice->qas_assigned_ctx_mask = qui->qui_image->ui_ctx_assigned;
2661f4f56ff4SMark Johnston 
2662f4f56ff4SMark Johnston 	nregions = qui->qui_image->ui_num_page_regions;
2663f4f56ff4SMark Johnston 	npages = qui->qui_image->ui_num_pages;
2664f4f56ff4SMark Johnston 
2665f4f56ff4SMark Johnston 	if (nregions > nitems(slice->qas_regions))
2666f4f56ff4SMark Johnston 		return ENOENT;
2667f4f56ff4SMark Johnston 	if (npages > nitems(slice->qas_pages))
2668f4f56ff4SMark Johnston 		return ENOENT;
2669f4f56ff4SMark Johnston 
2670f4f56ff4SMark Johnston 	for (i = 0; i < nregions; i++) {
2671f4f56ff4SMark Johnston 		STAILQ_INIT(&slice->qas_regions[i].qar_waiting_pages);
2672f4f56ff4SMark Johnston 	}
2673f4f56ff4SMark Johnston 	for (i = 0; i < npages; i++) {
2674f4f56ff4SMark Johnston 		struct qat_ae_page *page = &slice->qas_pages[i];
2675f4f56ff4SMark Johnston 		int region;
2676f4f56ff4SMark Johnston 
2677f4f56ff4SMark Johnston 		page->qap_page = &qui->qui_pages[i];
2678f4f56ff4SMark Johnston 		region = page->qap_page->qup_page_region;
2679f4f56ff4SMark Johnston 		if (region >= nregions)
2680f4f56ff4SMark Johnston 			return EINVAL;
2681f4f56ff4SMark Johnston 
2682f4f56ff4SMark Johnston 		page->qap_region = &slice->qas_regions[region];
2683f4f56ff4SMark Johnston 	}
2684f4f56ff4SMark Johnston 
2685f4f56ff4SMark Johnston 	qae->qae_num_slices++;
2686f4f56ff4SMark Johnston 
2687f4f56ff4SMark Johnston 	return 0;
2688f4f56ff4SMark Johnston }
2689f4f56ff4SMark Johnston 
2690f4f56ff4SMark Johnston static int
qat_aefw_uof_init_ae(struct qat_softc * sc,u_char ae)2691f4f56ff4SMark Johnston qat_aefw_uof_init_ae(struct qat_softc *sc, u_char ae)
2692f4f56ff4SMark Johnston {
2693f4f56ff4SMark Johnston 	struct uof_image *image;
2694f4f56ff4SMark Johnston 	struct qat_ae *qae = &(QAT_AE(sc, ae));
2695f4f56ff4SMark Johnston 	int s;
2696f4f56ff4SMark Johnston 	u_char nn_mode;
2697f4f56ff4SMark Johnston 
2698f4f56ff4SMark Johnston 	for (s = 0; s < qae->qae_num_slices; s++) {
2699f4f56ff4SMark Johnston 		if (qae->qae_slices[s].qas_image == NULL)
2700f4f56ff4SMark Johnston 			continue;
2701f4f56ff4SMark Johnston 
2702f4f56ff4SMark Johnston 		image = qae->qae_slices[s].qas_image->qui_image;
2703f4f56ff4SMark Johnston 		qat_ae_write_ctx_mode(sc, ae,
2704f4f56ff4SMark Johnston 		    __SHIFTOUT(image->ui_ae_mode, AE_MODE_CTX_MODE));
2705f4f56ff4SMark Johnston 
2706f4f56ff4SMark Johnston 		nn_mode = __SHIFTOUT(image->ui_ae_mode, AE_MODE_NN_MODE);
2707f4f56ff4SMark Johnston 		if (nn_mode != AE_MODE_NN_MODE_DONTCARE)
2708f4f56ff4SMark Johnston 			qat_ae_write_nn_mode(sc, ae, nn_mode);
2709f4f56ff4SMark Johnston 
2710f4f56ff4SMark Johnston 		qat_ae_write_lm_mode(sc, ae, AEREG_LMEM0,
2711f4f56ff4SMark Johnston 		    __SHIFTOUT(image->ui_ae_mode, AE_MODE_LMEM0));
2712f4f56ff4SMark Johnston 		qat_ae_write_lm_mode(sc, ae, AEREG_LMEM1,
2713f4f56ff4SMark Johnston 		    __SHIFTOUT(image->ui_ae_mode, AE_MODE_LMEM1));
2714f4f56ff4SMark Johnston 
2715f4f56ff4SMark Johnston 		qat_ae_write_shared_cs_mode(sc, ae,
2716f4f56ff4SMark Johnston 		    __SHIFTOUT(image->ui_ae_mode, AE_MODE_SHARED_USTORE));
2717f4f56ff4SMark Johnston 		qat_ae_set_reload_ustore(sc, ae, image->ui_reloadable_size,
2718f4f56ff4SMark Johnston 		    __SHIFTOUT(image->ui_ae_mode, AE_MODE_RELOAD_CTX_SHARED),
2719f4f56ff4SMark Johnston 		    qae->qae_reloc_ustore_dram);
2720f4f56ff4SMark Johnston 	}
2721f4f56ff4SMark Johnston 
2722f4f56ff4SMark Johnston 	return 0;
2723f4f56ff4SMark Johnston }
2724f4f56ff4SMark Johnston 
2725f4f56ff4SMark Johnston static int
qat_aefw_uof_init(struct qat_softc * sc)2726f4f56ff4SMark Johnston qat_aefw_uof_init(struct qat_softc *sc)
2727f4f56ff4SMark Johnston {
2728f4f56ff4SMark Johnston 	int ae, i, error;
2729f4f56ff4SMark Johnston 	uint32_t mask;
2730f4f56ff4SMark Johnston 
2731f4f56ff4SMark Johnston 	for (ae = 0, mask = sc->sc_ae_mask; mask; ae++, mask >>= 1) {
2732f4f56ff4SMark Johnston 		struct qat_ae *qae;
2733f4f56ff4SMark Johnston 
2734f4f56ff4SMark Johnston 		if (!(mask & 1))
2735f4f56ff4SMark Johnston 			continue;
2736f4f56ff4SMark Johnston 
2737f4f56ff4SMark Johnston 		qae = &(QAT_AE(sc, ae));
2738f4f56ff4SMark Johnston 
2739f4f56ff4SMark Johnston 		for (i = 0; i < sc->sc_aefw_uof.qafu_num_imgs; i++) {
2740f4f56ff4SMark Johnston 			if ((sc->sc_aefw_uof.qafu_imgs[i].qui_image->ui_ae_assigned &
2741f4f56ff4SMark Johnston 			    (1 << ae)) == 0)
2742f4f56ff4SMark Johnston 				continue;
2743f4f56ff4SMark Johnston 
2744f4f56ff4SMark Johnston 			error = qat_aefw_uof_assign_image(sc, qae,
2745f4f56ff4SMark Johnston 			    &sc->sc_aefw_uof.qafu_imgs[i]);
2746f4f56ff4SMark Johnston 			if (error)
2747f4f56ff4SMark Johnston 				return error;
2748f4f56ff4SMark Johnston 		}
2749f4f56ff4SMark Johnston 
2750f4f56ff4SMark Johnston 		/* XXX UcLo_initNumUwordUsed */
2751f4f56ff4SMark Johnston 
2752f4f56ff4SMark Johnston 		qae->qae_reloc_ustore_dram = UINT_MAX; /* XXX */
2753f4f56ff4SMark Johnston 
2754f4f56ff4SMark Johnston 		error = qat_aefw_uof_init_ae(sc, ae);
2755f4f56ff4SMark Johnston 		if (error)
2756f4f56ff4SMark Johnston 			return error;
2757f4f56ff4SMark Johnston 	}
2758f4f56ff4SMark Johnston 
2759f4f56ff4SMark Johnston 	return 0;
2760f4f56ff4SMark Johnston }
2761f4f56ff4SMark Johnston 
2762f4f56ff4SMark Johnston int
qat_aefw_load(struct qat_softc * sc)2763f4f56ff4SMark Johnston qat_aefw_load(struct qat_softc *sc)
2764f4f56ff4SMark Johnston {
2765f4f56ff4SMark Johnston 	int error;
2766f4f56ff4SMark Johnston 
2767f4f56ff4SMark Johnston 	error = qat_aefw_load_mof(sc);
2768f4f56ff4SMark Johnston 	if (error)
2769f4f56ff4SMark Johnston 		return error;
2770f4f56ff4SMark Johnston 
2771f4f56ff4SMark Johnston 	error = qat_aefw_load_mmp(sc);
2772f4f56ff4SMark Johnston 	if (error)
2773f4f56ff4SMark Johnston 		return error;
2774f4f56ff4SMark Johnston 
2775f4f56ff4SMark Johnston 	error = qat_aefw_mof_parse(sc);
2776f4f56ff4SMark Johnston 	if (error) {
2777f4f56ff4SMark Johnston 		device_printf(sc->sc_dev, "couldn't parse mof: %d\n", error);
2778f4f56ff4SMark Johnston 		return error;
2779f4f56ff4SMark Johnston 	}
2780f4f56ff4SMark Johnston 
2781f4f56ff4SMark Johnston 	if (sc->sc_hw.qhw_fw_auth) {
2782f4f56ff4SMark Johnston 		error = qat_aefw_suof_parse(sc);
2783f4f56ff4SMark Johnston 		if (error) {
2784f4f56ff4SMark Johnston 			device_printf(sc->sc_dev, "couldn't parse suof: %d\n",
2785f4f56ff4SMark Johnston 			    error);
2786f4f56ff4SMark Johnston 			return error;
2787f4f56ff4SMark Johnston 		}
2788f4f56ff4SMark Johnston 
2789f4f56ff4SMark Johnston 		error = qat_aefw_suof_write(sc);
2790f4f56ff4SMark Johnston 		if (error) {
2791f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
2792f4f56ff4SMark Johnston 			    "could not write firmware: %d\n", error);
2793f4f56ff4SMark Johnston 			return error;
2794f4f56ff4SMark Johnston 		}
2795f4f56ff4SMark Johnston 
2796f4f56ff4SMark Johnston 	} else {
2797f4f56ff4SMark Johnston 		error = qat_aefw_uof_parse(sc);
2798f4f56ff4SMark Johnston 		if (error) {
2799f4f56ff4SMark Johnston 			device_printf(sc->sc_dev, "couldn't parse uof: %d\n",
2800f4f56ff4SMark Johnston 			    error);
2801f4f56ff4SMark Johnston 			return error;
2802f4f56ff4SMark Johnston 		}
2803f4f56ff4SMark Johnston 
2804f4f56ff4SMark Johnston 		error = qat_aefw_uof_init(sc);
2805f4f56ff4SMark Johnston 		if (error) {
2806f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
2807f4f56ff4SMark Johnston 			    "couldn't init for aefw: %d\n", error);
2808f4f56ff4SMark Johnston 			return error;
2809f4f56ff4SMark Johnston 		}
2810f4f56ff4SMark Johnston 
2811f4f56ff4SMark Johnston 		error = qat_aefw_uof_write(sc);
2812f4f56ff4SMark Johnston 		if (error) {
2813f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
2814f4f56ff4SMark Johnston 			    "Could not write firmware: %d\n", error);
2815f4f56ff4SMark Johnston 			return error;
2816f4f56ff4SMark Johnston 		}
2817f4f56ff4SMark Johnston 	}
2818f4f56ff4SMark Johnston 
2819f4f56ff4SMark Johnston 	return 0;
2820f4f56ff4SMark Johnston }
2821f4f56ff4SMark Johnston 
2822f4f56ff4SMark Johnston void
qat_aefw_unload(struct qat_softc * sc)2823f4f56ff4SMark Johnston qat_aefw_unload(struct qat_softc *sc)
2824f4f56ff4SMark Johnston {
2825f4f56ff4SMark Johnston 	qat_aefw_unload_mmp(sc);
2826f4f56ff4SMark Johnston 	qat_aefw_unload_mof(sc);
2827f4f56ff4SMark Johnston }
2828f4f56ff4SMark Johnston 
2829f4f56ff4SMark Johnston int
qat_aefw_start(struct qat_softc * sc,u_char ae,u_int ctx_mask)2830f4f56ff4SMark Johnston qat_aefw_start(struct qat_softc *sc, u_char ae, u_int ctx_mask)
2831f4f56ff4SMark Johnston {
2832f4f56ff4SMark Johnston 	uint32_t fcu;
2833f4f56ff4SMark Johnston 	int retry = 0;
2834f4f56ff4SMark Johnston 
2835f4f56ff4SMark Johnston 	if (sc->sc_hw.qhw_fw_auth) {
2836f4f56ff4SMark Johnston 		qat_cap_global_write_4(sc, FCU_CTRL, FCU_CTRL_CMD_START);
2837f4f56ff4SMark Johnston 		do {
2838f4f56ff4SMark Johnston 			DELAY(FW_AUTH_WAIT_PERIOD * 1000);
2839f4f56ff4SMark Johnston 			fcu = qat_cap_global_read_4(sc, FCU_STATUS);
2840f4f56ff4SMark Johnston 			if (fcu & FCU_STATUS_DONE)
2841f4f56ff4SMark Johnston 				return 0;
2842f4f56ff4SMark Johnston 		} while (retry++ < FW_AUTH_MAX_RETRY);
2843f4f56ff4SMark Johnston 
2844f4f56ff4SMark Johnston 		device_printf(sc->sc_dev,
2845f4f56ff4SMark Johnston 		    "firmware start timeout: status %08x\n", fcu);
2846f4f56ff4SMark Johnston 		return EINVAL;
2847f4f56ff4SMark Johnston 	} else {
2848f4f56ff4SMark Johnston 		qat_ae_ctx_indr_write(sc, ae, (~ctx_mask) & AE_ALL_CTX,
2849f4f56ff4SMark Johnston 		    CTX_WAKEUP_EVENTS_INDIRECT,
2850f4f56ff4SMark Johnston 		    CTX_WAKEUP_EVENTS_INDIRECT_SLEEP);
2851f4f56ff4SMark Johnston 		qat_ae_enable_ctx(sc, ae, ctx_mask);
2852f4f56ff4SMark Johnston 	}
2853f4f56ff4SMark Johnston 
2854f4f56ff4SMark Johnston 	return 0;
2855f4f56ff4SMark Johnston }
2856f4f56ff4SMark Johnston 
2857f4f56ff4SMark Johnston static int
qat_aefw_init_memory_one(struct qat_softc * sc,struct uof_init_mem * uim)2858f4f56ff4SMark Johnston qat_aefw_init_memory_one(struct qat_softc *sc, struct uof_init_mem *uim)
2859f4f56ff4SMark Johnston {
2860f4f56ff4SMark Johnston 	struct qat_aefw_uof *qafu = &sc->sc_aefw_uof;
2861f4f56ff4SMark Johnston 	struct qat_ae_batch_init_list *qabi_list;
2862f4f56ff4SMark Johnston 	struct uof_mem_val_attr *memattr;
2863f4f56ff4SMark Johnston 	size_t *curinit;
2864f4f56ff4SMark Johnston 	u_long ael;
2865f4f56ff4SMark Johnston 	int i;
2866f4f56ff4SMark Johnston 	const char *sym;
2867f4f56ff4SMark Johnston 	char *ep;
2868f4f56ff4SMark Johnston 
2869f4f56ff4SMark Johnston 	memattr = (struct uof_mem_val_attr *)(uim + 1);
2870f4f56ff4SMark Johnston 
2871f4f56ff4SMark Johnston 	switch (uim->uim_region) {
2872f4f56ff4SMark Johnston 	case LMEM_REGION:
2873f4f56ff4SMark Johnston 		if ((uim->uim_addr + uim->uim_num_bytes) > MAX_LMEM_REG * 4) {
2874f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
2875f4f56ff4SMark Johnston 			    "Invalid lmem addr or bytes\n");
2876f4f56ff4SMark Johnston 			return ENOBUFS;
2877f4f56ff4SMark Johnston 		}
2878f4f56ff4SMark Johnston 		if (uim->uim_scope != UOF_SCOPE_LOCAL)
2879f4f56ff4SMark Johnston 			return EINVAL;
2880f4f56ff4SMark Johnston 		sym = qat_aefw_uof_string(sc, uim->uim_sym_name);
2881f4f56ff4SMark Johnston 		ael = strtoul(sym, &ep, 10);
2882f4f56ff4SMark Johnston 		if (ep == sym || ael > MAX_AE)
2883f4f56ff4SMark Johnston 			return EINVAL;
2884f4f56ff4SMark Johnston 		if ((sc->sc_ae_mask & (1 << ael)) == 0)
2885f4f56ff4SMark Johnston 			return 0; /* ae is fused out */
2886f4f56ff4SMark Johnston 
2887f4f56ff4SMark Johnston 		curinit = &qafu->qafu_num_lm_init[ael];
2888f4f56ff4SMark Johnston 		qabi_list = &qafu->qafu_lm_init[ael];
2889f4f56ff4SMark Johnston 
2890f4f56ff4SMark Johnston 		for (i = 0; i < uim->uim_num_val_attr; i++, memattr++) {
2891f4f56ff4SMark Johnston 			struct qat_ae_batch_init *qabi;
2892f4f56ff4SMark Johnston 
2893f4f56ff4SMark Johnston 			qabi = qat_alloc_mem(sizeof(struct qat_ae_batch_init));
2894f4f56ff4SMark Johnston 			if (*curinit == 0)
2895f4f56ff4SMark Johnston 				STAILQ_INIT(qabi_list);
2896f4f56ff4SMark Johnston 			STAILQ_INSERT_TAIL(qabi_list, qabi, qabi_next);
2897f4f56ff4SMark Johnston 
2898f4f56ff4SMark Johnston 			qabi->qabi_ae = (u_int)ael;
2899f4f56ff4SMark Johnston 			qabi->qabi_addr =
2900f4f56ff4SMark Johnston 			    uim->uim_addr + memattr->umva_byte_offset;
2901f4f56ff4SMark Johnston 			qabi->qabi_value = &memattr->umva_value;
2902f4f56ff4SMark Johnston 			qabi->qabi_size = 4;
2903f4f56ff4SMark Johnston 			qafu->qafu_num_lm_init_inst[ael] +=
2904f4f56ff4SMark Johnston 			    qat_ae_get_inst_num(qabi->qabi_size);
2905f4f56ff4SMark Johnston 			(*curinit)++;
2906f4f56ff4SMark Johnston 			if (*curinit >= MAX_LMEM_REG) {
2907f4f56ff4SMark Johnston 				device_printf(sc->sc_dev,
2908f4f56ff4SMark Johnston 				    "Invalid lmem val attr\n");
2909f4f56ff4SMark Johnston 				return ENOBUFS;
2910f4f56ff4SMark Johnston 			}
2911f4f56ff4SMark Johnston 		}
2912f4f56ff4SMark Johnston 		break;
2913f4f56ff4SMark Johnston 	case SRAM_REGION:
2914f4f56ff4SMark Johnston 	case DRAM_REGION:
2915f4f56ff4SMark Johnston 	case DRAM1_REGION:
2916f4f56ff4SMark Johnston 	case SCRATCH_REGION:
2917f4f56ff4SMark Johnston 	case UMEM_REGION:
2918f4f56ff4SMark Johnston 		/* XXX */
2919f4f56ff4SMark Johnston 		/* fallthrough */
2920f4f56ff4SMark Johnston 	default:
2921f4f56ff4SMark Johnston 		device_printf(sc->sc_dev,
2922f4f56ff4SMark Johnston 		    "unsupported memory region to init: %d\n",
2923f4f56ff4SMark Johnston 		    uim->uim_region);
2924f4f56ff4SMark Johnston 		return ENOTSUP;
2925f4f56ff4SMark Johnston 	}
2926f4f56ff4SMark Johnston 
2927f4f56ff4SMark Johnston 	return 0;
2928f4f56ff4SMark Johnston }
2929f4f56ff4SMark Johnston 
2930f4f56ff4SMark Johnston static void
qat_aefw_free_lm_init(struct qat_softc * sc,u_char ae)2931f4f56ff4SMark Johnston qat_aefw_free_lm_init(struct qat_softc *sc, u_char ae)
2932f4f56ff4SMark Johnston {
2933f4f56ff4SMark Johnston 	struct qat_aefw_uof *qafu = &sc->sc_aefw_uof;
2934f4f56ff4SMark Johnston 	struct qat_ae_batch_init *qabi;
2935f4f56ff4SMark Johnston 
2936f4f56ff4SMark Johnston 	while ((qabi = STAILQ_FIRST(&qafu->qafu_lm_init[ae])) != NULL) {
2937f4f56ff4SMark Johnston 		STAILQ_REMOVE_HEAD(&qafu->qafu_lm_init[ae], qabi_next);
2938f4f56ff4SMark Johnston 		qat_free_mem(qabi);
2939f4f56ff4SMark Johnston 	}
2940f4f56ff4SMark Johnston 
2941f4f56ff4SMark Johnston 	qafu->qafu_num_lm_init[ae] = 0;
2942f4f56ff4SMark Johnston 	qafu->qafu_num_lm_init_inst[ae] = 0;
2943f4f56ff4SMark Johnston }
2944f4f56ff4SMark Johnston 
2945f4f56ff4SMark Johnston static int
qat_aefw_init_ustore(struct qat_softc * sc)2946f4f56ff4SMark Johnston qat_aefw_init_ustore(struct qat_softc *sc)
2947f4f56ff4SMark Johnston {
2948f4f56ff4SMark Johnston 	uint64_t *fill;
2949f4f56ff4SMark Johnston 	uint32_t dont_init;
2950f4f56ff4SMark Johnston 	int a, i, p;
2951f4f56ff4SMark Johnston 	int error = 0;
2952f4f56ff4SMark Johnston 	int usz, end, start;
2953f4f56ff4SMark Johnston 	u_char ae, nae;
2954f4f56ff4SMark Johnston 
2955f4f56ff4SMark Johnston 	fill = qat_alloc_mem(MAX_USTORE * sizeof(uint64_t));
2956f4f56ff4SMark Johnston 
2957f4f56ff4SMark Johnston 	for (a = 0; a < sc->sc_aefw_uof.qafu_num_imgs; a++) {
2958f4f56ff4SMark Johnston 		struct qat_uof_image *qui = &sc->sc_aefw_uof.qafu_imgs[a];
2959f4f56ff4SMark Johnston 		struct uof_image *ui = qui->qui_image;
2960f4f56ff4SMark Johnston 
2961f4f56ff4SMark Johnston 		for (i = 0; i < MAX_USTORE; i++)
2962f4f56ff4SMark Johnston 			memcpy(&fill[i], ui->ui_fill_pattern, sizeof(uint64_t));
2963f4f56ff4SMark Johnston 		/*
2964f4f56ff4SMark Johnston 		 * Compute do_not_init value as a value that will not be equal
2965f4f56ff4SMark Johnston 		 * to fill data when cast to an int
2966f4f56ff4SMark Johnston 		 */
2967f4f56ff4SMark Johnston 		dont_init = 0;
2968f4f56ff4SMark Johnston 		if (dont_init == (uint32_t)fill[0])
2969f4f56ff4SMark Johnston 			dont_init = 0xffffffff;
2970f4f56ff4SMark Johnston 
2971f4f56ff4SMark Johnston 		for (p = 0; p < ui->ui_num_pages; p++) {
2972f4f56ff4SMark Johnston 			struct qat_uof_page *qup = &qui->qui_pages[p];
2973f4f56ff4SMark Johnston 			if (!qup->qup_def_page)
2974f4f56ff4SMark Johnston 				continue;
2975f4f56ff4SMark Johnston 
2976f4f56ff4SMark Johnston 			for (i = qup->qup_beg_paddr;
2977f4f56ff4SMark Johnston 			    i < qup->qup_beg_paddr + qup->qup_num_micro_words;
2978f4f56ff4SMark Johnston 			    i++ ) {
2979f4f56ff4SMark Johnston 				fill[i] = (uint64_t)dont_init;
2980f4f56ff4SMark Johnston 			}
2981f4f56ff4SMark Johnston 		}
2982f4f56ff4SMark Johnston 
2983f4f56ff4SMark Johnston 		for (ae = 0; ae < sc->sc_ae_num; ae++) {
2984f4f56ff4SMark Johnston 			MPASS(ae < UOF_MAX_NUM_OF_AE);
2985f4f56ff4SMark Johnston 			if ((ui->ui_ae_assigned & (1 << ae)) == 0)
2986f4f56ff4SMark Johnston 				continue;
2987f4f56ff4SMark Johnston 
2988f4f56ff4SMark Johnston 			if (QAT_AE(sc, ae).qae_shareable_ustore && (ae & 1)) {
2989f4f56ff4SMark Johnston 				qat_ae_get_shared_ustore_ae(ae, &nae);
2990f4f56ff4SMark Johnston 				if (ui->ui_ae_assigned & (1 << ae))
2991f4f56ff4SMark Johnston 					continue;
2992f4f56ff4SMark Johnston 			}
2993f4f56ff4SMark Johnston 			usz = QAT_AE(sc, ae).qae_effect_ustore_size;
2994f4f56ff4SMark Johnston 
2995f4f56ff4SMark Johnston 			/* initialize the areas not going to be overwritten */
2996f4f56ff4SMark Johnston 			end = -1;
2997f4f56ff4SMark Johnston 			do {
2998f4f56ff4SMark Johnston 				/* find next uword that needs to be initialized */
2999f4f56ff4SMark Johnston 				for (start = end + 1; start < usz; start++) {
3000f4f56ff4SMark Johnston 					if ((uint32_t)fill[start] != dont_init)
3001f4f56ff4SMark Johnston 						break;
3002f4f56ff4SMark Johnston 				}
3003f4f56ff4SMark Johnston 				/* see if there are no more such uwords */
3004f4f56ff4SMark Johnston 				if (start >= usz)
3005f4f56ff4SMark Johnston 					break;
3006f4f56ff4SMark Johnston 				for (end = start + 1; end < usz; end++) {
3007f4f56ff4SMark Johnston 					if ((uint32_t)fill[end] == dont_init)
3008f4f56ff4SMark Johnston 						break;
3009f4f56ff4SMark Johnston 				}
3010f4f56ff4SMark Johnston 				if (QAT_AE(sc, ae).qae_shareable_ustore) {
3011f4f56ff4SMark Johnston 					error = ENOTSUP; /* XXX */
3012f4f56ff4SMark Johnston 					goto out;
3013f4f56ff4SMark Johnston 				} else {
3014f4f56ff4SMark Johnston 					error = qat_ae_ucode_write(sc, ae,
3015f4f56ff4SMark Johnston 					    start, end - start, &fill[start]);
3016f4f56ff4SMark Johnston 					if (error) {
3017f4f56ff4SMark Johnston 						goto out;
3018f4f56ff4SMark Johnston 					}
3019f4f56ff4SMark Johnston 				}
3020f4f56ff4SMark Johnston 
3021f4f56ff4SMark Johnston 			} while (end < usz);
3022f4f56ff4SMark Johnston 		}
3023f4f56ff4SMark Johnston 	}
3024f4f56ff4SMark Johnston 
3025f4f56ff4SMark Johnston out:
3026f4f56ff4SMark Johnston 	qat_free_mem(fill);
3027f4f56ff4SMark Johnston 	return error;
3028f4f56ff4SMark Johnston }
3029f4f56ff4SMark Johnston 
3030f4f56ff4SMark Johnston static int
qat_aefw_init_reg(struct qat_softc * sc,u_char ae,u_char ctx_mask,enum aereg_type regtype,u_short regaddr,u_int value)3031f4f56ff4SMark Johnston qat_aefw_init_reg(struct qat_softc *sc, u_char ae, u_char ctx_mask,
3032f4f56ff4SMark Johnston     enum aereg_type regtype, u_short regaddr, u_int value)
3033f4f56ff4SMark Johnston {
3034f4f56ff4SMark Johnston 	int error = 0;
3035f4f56ff4SMark Johnston 	u_char ctx;
3036f4f56ff4SMark Johnston 
3037f4f56ff4SMark Johnston 	switch (regtype) {
3038f4f56ff4SMark Johnston 	case AEREG_GPA_REL:
3039f4f56ff4SMark Johnston 	case AEREG_GPB_REL:
3040f4f56ff4SMark Johnston 	case AEREG_SR_REL:
3041f4f56ff4SMark Johnston 	case AEREG_SR_RD_REL:
3042f4f56ff4SMark Johnston 	case AEREG_SR_WR_REL:
3043f4f56ff4SMark Johnston 	case AEREG_DR_REL:
3044f4f56ff4SMark Johnston 	case AEREG_DR_RD_REL:
3045f4f56ff4SMark Johnston 	case AEREG_DR_WR_REL:
3046f4f56ff4SMark Johnston 	case AEREG_NEIGH_REL:
3047f4f56ff4SMark Johnston 		/* init for all valid ctx */
3048f4f56ff4SMark Johnston 		for (ctx = 0; ctx < MAX_AE_CTX; ctx++) {
3049f4f56ff4SMark Johnston 			if ((ctx_mask & (1 << ctx)) == 0)
3050f4f56ff4SMark Johnston 				continue;
3051f4f56ff4SMark Johnston 			error = qat_aereg_rel_data_write(sc, ae, ctx, regtype,
3052f4f56ff4SMark Johnston 			    regaddr, value);
3053f4f56ff4SMark Johnston 		}
3054f4f56ff4SMark Johnston 		break;
3055f4f56ff4SMark Johnston 	case AEREG_GPA_ABS:
3056f4f56ff4SMark Johnston 	case AEREG_GPB_ABS:
3057f4f56ff4SMark Johnston 	case AEREG_SR_ABS:
3058f4f56ff4SMark Johnston 	case AEREG_SR_RD_ABS:
3059f4f56ff4SMark Johnston 	case AEREG_SR_WR_ABS:
3060f4f56ff4SMark Johnston 	case AEREG_DR_ABS:
3061f4f56ff4SMark Johnston 	case AEREG_DR_RD_ABS:
3062f4f56ff4SMark Johnston 	case AEREG_DR_WR_ABS:
3063f4f56ff4SMark Johnston 		error = qat_aereg_abs_data_write(sc, ae, regtype,
3064f4f56ff4SMark Johnston 		    regaddr, value);
3065f4f56ff4SMark Johnston 		break;
3066f4f56ff4SMark Johnston 	default:
3067f4f56ff4SMark Johnston 		error = EINVAL;
3068f4f56ff4SMark Johnston 		break;
3069f4f56ff4SMark Johnston 	}
3070f4f56ff4SMark Johnston 
3071f4f56ff4SMark Johnston 	return error;
3072f4f56ff4SMark Johnston }
3073f4f56ff4SMark Johnston 
3074f4f56ff4SMark Johnston static int
qat_aefw_init_reg_sym_expr(struct qat_softc * sc,u_char ae,struct qat_uof_image * qui)3075f4f56ff4SMark Johnston qat_aefw_init_reg_sym_expr(struct qat_softc *sc, u_char ae,
3076f4f56ff4SMark Johnston     struct qat_uof_image *qui)
3077f4f56ff4SMark Johnston {
3078f4f56ff4SMark Johnston 	u_int i, expres;
3079f4f56ff4SMark Johnston 	u_char ctx_mask;
3080f4f56ff4SMark Johnston 
3081f4f56ff4SMark Johnston 	for (i = 0; i < qui->qui_num_init_reg_sym; i++) {
3082f4f56ff4SMark Johnston 		struct uof_init_reg_sym *uirs = &qui->qui_init_reg_sym[i];
3083f4f56ff4SMark Johnston 
3084f4f56ff4SMark Johnston 		if (uirs->uirs_value_type == EXPR_VAL) {
3085f4f56ff4SMark Johnston 			/* XXX */
3086f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
3087f4f56ff4SMark Johnston 			    "does not support initializing EXPR_VAL\n");
3088f4f56ff4SMark Johnston 			return ENOTSUP;
3089f4f56ff4SMark Johnston 		} else {
3090f4f56ff4SMark Johnston 			expres = uirs->uirs_value;
3091f4f56ff4SMark Johnston 		}
3092f4f56ff4SMark Johnston 
3093f4f56ff4SMark Johnston 		switch (uirs->uirs_init_type) {
3094f4f56ff4SMark Johnston 		case INIT_REG:
3095f4f56ff4SMark Johnston 			if (__SHIFTOUT(qui->qui_image->ui_ae_mode,
3096f4f56ff4SMark Johnston 			    AE_MODE_CTX_MODE) == MAX_AE_CTX) {
3097f4f56ff4SMark Johnston 				ctx_mask = 0xff; /* 8-ctx mode */
3098f4f56ff4SMark Johnston 			} else {
3099f4f56ff4SMark Johnston 				ctx_mask = 0x55; /* 4-ctx mode */
3100f4f56ff4SMark Johnston 			}
3101f4f56ff4SMark Johnston 			qat_aefw_init_reg(sc, ae, ctx_mask,
3102f4f56ff4SMark Johnston 			    (enum aereg_type)uirs->uirs_reg_type,
3103f4f56ff4SMark Johnston 			    (u_short)uirs->uirs_addr_offset, expres);
3104f4f56ff4SMark Johnston 			break;
3105f4f56ff4SMark Johnston 		case INIT_REG_CTX:
3106f4f56ff4SMark Johnston 			if (__SHIFTOUT(qui->qui_image->ui_ae_mode,
3107f4f56ff4SMark Johnston 			    AE_MODE_CTX_MODE) == MAX_AE_CTX) {
3108f4f56ff4SMark Johnston 				ctx_mask = 0xff; /* 8-ctx mode */
3109f4f56ff4SMark Johnston 			} else {
3110f4f56ff4SMark Johnston 				ctx_mask = 0x55; /* 4-ctx mode */
3111f4f56ff4SMark Johnston 			}
3112f4f56ff4SMark Johnston 			if (((1 << uirs->uirs_ctx) & ctx_mask) == 0)
3113f4f56ff4SMark Johnston 				return EINVAL;
3114f4f56ff4SMark Johnston 			qat_aefw_init_reg(sc, ae, 1 << uirs->uirs_ctx,
3115f4f56ff4SMark Johnston 			    (enum aereg_type)uirs->uirs_reg_type,
3116f4f56ff4SMark Johnston 			    (u_short)uirs->uirs_addr_offset, expres);
3117f4f56ff4SMark Johnston 			break;
3118f4f56ff4SMark Johnston 		case INIT_EXPR:
3119f4f56ff4SMark Johnston 		case INIT_EXPR_ENDIAN_SWAP:
3120f4f56ff4SMark Johnston 		default:
3121f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
3122f4f56ff4SMark Johnston 			    "does not support initializing init_type %d\n",
3123f4f56ff4SMark Johnston 			    uirs->uirs_init_type);
3124f4f56ff4SMark Johnston 			return ENOTSUP;
3125f4f56ff4SMark Johnston 		}
3126f4f56ff4SMark Johnston 	}
3127f4f56ff4SMark Johnston 
3128f4f56ff4SMark Johnston 	return 0;
3129f4f56ff4SMark Johnston }
3130f4f56ff4SMark Johnston 
3131f4f56ff4SMark Johnston static int
qat_aefw_init_memory(struct qat_softc * sc)3132f4f56ff4SMark Johnston qat_aefw_init_memory(struct qat_softc *sc)
3133f4f56ff4SMark Johnston {
3134f4f56ff4SMark Johnston 	struct qat_aefw_uof *qafu = &sc->sc_aefw_uof;
3135f4f56ff4SMark Johnston 	size_t uimsz, initmemsz = qafu->qafu_init_mem_size;
3136f4f56ff4SMark Johnston 	struct uof_init_mem *uim;
3137f4f56ff4SMark Johnston 	int error, i;
3138f4f56ff4SMark Johnston 	u_char ae;
3139f4f56ff4SMark Johnston 
3140f4f56ff4SMark Johnston 	uim = qafu->qafu_init_mem;
3141f4f56ff4SMark Johnston 	for (i = 0; i < qafu->qafu_num_init_mem; i++) {
3142f4f56ff4SMark Johnston 		uimsz = sizeof(struct uof_init_mem) +
3143f4f56ff4SMark Johnston 		    sizeof(struct uof_mem_val_attr) * uim->uim_num_val_attr;
3144f4f56ff4SMark Johnston 		if (uimsz > initmemsz) {
3145f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
3146f4f56ff4SMark Johnston 			    "invalid uof_init_mem or uof_mem_val_attr size\n");
3147f4f56ff4SMark Johnston 			return EINVAL;
3148f4f56ff4SMark Johnston 		}
3149f4f56ff4SMark Johnston 
3150f4f56ff4SMark Johnston 		if (uim->uim_num_bytes > 0) {
3151f4f56ff4SMark Johnston 			error = qat_aefw_init_memory_one(sc, uim);
3152f4f56ff4SMark Johnston 			if (error) {
3153f4f56ff4SMark Johnston 				device_printf(sc->sc_dev,
3154f4f56ff4SMark Johnston 				    "Could not init ae memory: %d\n", error);
3155f4f56ff4SMark Johnston 				return error;
3156f4f56ff4SMark Johnston 			}
3157f4f56ff4SMark Johnston 		}
3158f4f56ff4SMark Johnston 		uim = (struct uof_init_mem *)((uintptr_t)uim + uimsz);
3159f4f56ff4SMark Johnston 		initmemsz -= uimsz;
3160f4f56ff4SMark Johnston 	}
3161f4f56ff4SMark Johnston 
3162f4f56ff4SMark Johnston 	/* run Batch put LM API */
3163f4f56ff4SMark Johnston 	for (ae = 0; ae < MAX_AE; ae++) {
3164f4f56ff4SMark Johnston 		error = qat_ae_batch_put_lm(sc, ae, &qafu->qafu_lm_init[ae],
3165f4f56ff4SMark Johnston 		    qafu->qafu_num_lm_init_inst[ae]);
3166f4f56ff4SMark Johnston 		if (error)
3167f4f56ff4SMark Johnston 			device_printf(sc->sc_dev, "Could not put lm\n");
3168f4f56ff4SMark Johnston 
3169f4f56ff4SMark Johnston 		qat_aefw_free_lm_init(sc, ae);
3170f4f56ff4SMark Johnston 	}
3171f4f56ff4SMark Johnston 
3172f4f56ff4SMark Johnston 	error = qat_aefw_init_ustore(sc);
3173f4f56ff4SMark Johnston 
3174f4f56ff4SMark Johnston 	/* XXX run Batch put LM API */
3175f4f56ff4SMark Johnston 
3176f4f56ff4SMark Johnston 	return error;
3177f4f56ff4SMark Johnston }
3178f4f56ff4SMark Johnston 
3179f4f56ff4SMark Johnston static int
qat_aefw_init_globals(struct qat_softc * sc)3180f4f56ff4SMark Johnston qat_aefw_init_globals(struct qat_softc *sc)
3181f4f56ff4SMark Johnston {
3182f4f56ff4SMark Johnston 	struct qat_aefw_uof *qafu = &sc->sc_aefw_uof;
3183f4f56ff4SMark Johnston 	int error, i, p, s;
3184f4f56ff4SMark Johnston 	u_char ae;
3185f4f56ff4SMark Johnston 
3186f4f56ff4SMark Johnston 	/* initialize the memory segments */
3187f4f56ff4SMark Johnston 	if (qafu->qafu_num_init_mem > 0) {
3188f4f56ff4SMark Johnston 		error = qat_aefw_init_memory(sc);
3189f4f56ff4SMark Johnston 		if (error)
3190f4f56ff4SMark Johnston 			return error;
3191f4f56ff4SMark Johnston 	} else {
3192f4f56ff4SMark Johnston 		error = qat_aefw_init_ustore(sc);
3193f4f56ff4SMark Johnston 		if (error)
3194f4f56ff4SMark Johnston 			return error;
3195f4f56ff4SMark Johnston 	}
3196f4f56ff4SMark Johnston 
3197f4f56ff4SMark Johnston 	/* XXX bind import variables with ivd values */
3198f4f56ff4SMark Johnston 
3199f4f56ff4SMark Johnston 	/* XXX bind the uC global variables
3200f4f56ff4SMark Johnston 	 * local variables will done on-the-fly */
3201f4f56ff4SMark Johnston 	for (i = 0; i < sc->sc_aefw_uof.qafu_num_imgs; i++) {
3202f4f56ff4SMark Johnston 		for (p = 0; p < sc->sc_aefw_uof.qafu_imgs[i].qui_image->ui_num_pages; p++) {
3203f4f56ff4SMark Johnston 			struct qat_uof_page *qup =
3204f4f56ff4SMark Johnston 			    &sc->sc_aefw_uof.qafu_imgs[i].qui_pages[p];
3205f4f56ff4SMark Johnston 			if (qup->qup_num_uw_blocks &&
3206f4f56ff4SMark Johnston 			    (qup->qup_num_uc_var || qup->qup_num_imp_var)) {
3207f4f56ff4SMark Johnston 				device_printf(sc->sc_dev,
3208f4f56ff4SMark Johnston 				    "not support uC global variables\n");
3209f4f56ff4SMark Johnston 				return ENOTSUP;
3210f4f56ff4SMark Johnston 			}
3211f4f56ff4SMark Johnston 		}
3212f4f56ff4SMark Johnston 	}
3213f4f56ff4SMark Johnston 
3214f4f56ff4SMark Johnston 	for (ae = 0; ae < sc->sc_ae_num; ae++) {
3215f4f56ff4SMark Johnston 		struct qat_ae *qae = &(QAT_AE(sc, ae));
3216f4f56ff4SMark Johnston 
3217f4f56ff4SMark Johnston 		for (s = 0; s < qae->qae_num_slices; s++) {
3218f4f56ff4SMark Johnston 			struct qat_ae_slice *qas = &qae->qae_slices[s];
3219f4f56ff4SMark Johnston 
3220f4f56ff4SMark Johnston 			if (qas->qas_image == NULL)
3221f4f56ff4SMark Johnston 				continue;
3222f4f56ff4SMark Johnston 
3223f4f56ff4SMark Johnston 			error =
3224f4f56ff4SMark Johnston 			    qat_aefw_init_reg_sym_expr(sc, ae, qas->qas_image);
3225f4f56ff4SMark Johnston 			if (error)
3226f4f56ff4SMark Johnston 				return error;
3227f4f56ff4SMark Johnston 		}
3228f4f56ff4SMark Johnston 	}
3229f4f56ff4SMark Johnston 
3230f4f56ff4SMark Johnston 	return 0;
3231f4f56ff4SMark Johnston }
3232f4f56ff4SMark Johnston 
3233f4f56ff4SMark Johnston static uint64_t
qat_aefw_get_uof_inst(struct qat_softc * sc,struct qat_uof_page * qup,u_int addr)3234f4f56ff4SMark Johnston qat_aefw_get_uof_inst(struct qat_softc *sc, struct qat_uof_page *qup,
3235f4f56ff4SMark Johnston     u_int addr)
3236f4f56ff4SMark Johnston {
3237f4f56ff4SMark Johnston 	uint64_t uinst = 0;
3238f4f56ff4SMark Johnston 	u_int i;
3239f4f56ff4SMark Johnston 
3240f4f56ff4SMark Johnston 	/* find the block */
3241f4f56ff4SMark Johnston 	for (i = 0; i < qup->qup_num_uw_blocks; i++) {
3242f4f56ff4SMark Johnston 		struct qat_uof_uword_block *quub = &qup->qup_uw_blocks[i];
3243f4f56ff4SMark Johnston 
3244f4f56ff4SMark Johnston 		if ((addr >= quub->quub_start_addr) &&
3245f4f56ff4SMark Johnston 		    (addr <= (quub->quub_start_addr +
3246f4f56ff4SMark Johnston 		    (quub->quub_num_words - 1)))) {
3247f4f56ff4SMark Johnston 			/* unpack n bytes and assigned to the 64-bit uword value.
3248f4f56ff4SMark Johnston 			note: the microwords are stored as packed bytes.
3249f4f56ff4SMark Johnston 			*/
3250f4f56ff4SMark Johnston 			addr -= quub->quub_start_addr;
3251f4f56ff4SMark Johnston 			addr *= AEV2_PACKED_UWORD_BYTES;
3252f4f56ff4SMark Johnston 			memcpy(&uinst,
3253f4f56ff4SMark Johnston 			    (void *)((uintptr_t)quub->quub_micro_words + addr),
3254f4f56ff4SMark Johnston 			    AEV2_PACKED_UWORD_BYTES);
3255f4f56ff4SMark Johnston 			uinst = uinst & UWORD_MASK;
3256f4f56ff4SMark Johnston 
3257f4f56ff4SMark Johnston 			return uinst;
3258f4f56ff4SMark Johnston 		}
3259f4f56ff4SMark Johnston 	}
3260f4f56ff4SMark Johnston 
3261f4f56ff4SMark Johnston 	return INVLD_UWORD;
3262f4f56ff4SMark Johnston }
3263f4f56ff4SMark Johnston 
3264f4f56ff4SMark Johnston static int
qat_aefw_do_pagein(struct qat_softc * sc,u_char ae,struct qat_uof_page * qup)3265f4f56ff4SMark Johnston qat_aefw_do_pagein(struct qat_softc *sc, u_char ae, struct qat_uof_page *qup)
3266f4f56ff4SMark Johnston {
3267f4f56ff4SMark Johnston 	struct qat_ae *qae = &(QAT_AE(sc, ae));
3268f4f56ff4SMark Johnston 	uint64_t fill, *ucode_cpybuf;
3269f4f56ff4SMark Johnston 	u_int error, i, upaddr, ninst, cpylen;
3270f4f56ff4SMark Johnston 
3271f4f56ff4SMark Johnston 	if (qup->qup_num_uc_var || qup->qup_num_neigh_reg ||
3272f4f56ff4SMark Johnston 	    qup->qup_num_imp_var || qup->qup_num_imp_expr) {
3273f4f56ff4SMark Johnston 		device_printf(sc->sc_dev,
3274f4f56ff4SMark Johnston 		    "does not support fixup locals\n");
3275f4f56ff4SMark Johnston 		return ENOTSUP;
3276f4f56ff4SMark Johnston 	}
3277f4f56ff4SMark Johnston 
3278f4f56ff4SMark Johnston 	ucode_cpybuf = qat_alloc_mem(UWORD_CPYBUF_SIZE * sizeof(uint64_t));
3279f4f56ff4SMark Johnston 
3280f4f56ff4SMark Johnston 	/* XXX get fill-pattern from an image -- they are all the same */
3281f4f56ff4SMark Johnston 	memcpy(&fill, sc->sc_aefw_uof.qafu_imgs[0].qui_image->ui_fill_pattern,
3282f4f56ff4SMark Johnston 	    sizeof(uint64_t));
3283f4f56ff4SMark Johnston 
3284f4f56ff4SMark Johnston 	upaddr = qup->qup_beg_paddr;
3285f4f56ff4SMark Johnston 	ninst = qup->qup_num_micro_words;
3286f4f56ff4SMark Johnston 	while (ninst > 0) {
3287f4f56ff4SMark Johnston 		cpylen = min(ninst, UWORD_CPYBUF_SIZE);
3288f4f56ff4SMark Johnston 
3289f4f56ff4SMark Johnston 		/* load the buffer */
3290f4f56ff4SMark Johnston 		for (i = 0; i < cpylen; i++) {
3291f4f56ff4SMark Johnston 			/* keep below code structure in case there are
3292f4f56ff4SMark Johnston 			 * different handling for shared secnarios */
3293f4f56ff4SMark Johnston 			if (!qae->qae_shareable_ustore) {
3294f4f56ff4SMark Johnston 				/* qat_aefw_get_uof_inst() takes an address that
3295f4f56ff4SMark Johnston 				 * is relative to the start of the page.
3296f4f56ff4SMark Johnston 				 * So we don't need to add in the physical
3297f4f56ff4SMark Johnston 				 * offset of the page. */
3298f4f56ff4SMark Johnston 				if (qup->qup_page_region != 0) {
3299f4f56ff4SMark Johnston 					/* XXX */
3300f4f56ff4SMark Johnston 					device_printf(sc->sc_dev,
3301f4f56ff4SMark Johnston 					    "region != 0 is not supported\n");
3302f4f56ff4SMark Johnston 					qat_free_mem(ucode_cpybuf);
3303f4f56ff4SMark Johnston 					return ENOTSUP;
3304f4f56ff4SMark Johnston 				} else {
3305f4f56ff4SMark Johnston 					/* for mixing case, it should take
3306f4f56ff4SMark Johnston 					 * physical address */
3307f4f56ff4SMark Johnston 					ucode_cpybuf[i] = qat_aefw_get_uof_inst(
3308f4f56ff4SMark Johnston 					    sc, qup, upaddr + i);
3309f4f56ff4SMark Johnston 					if (ucode_cpybuf[i] == INVLD_UWORD) {
3310f4f56ff4SMark Johnston 					    /* fill hole in the uof */
3311f4f56ff4SMark Johnston 					    ucode_cpybuf[i] = fill;
3312f4f56ff4SMark Johnston 					}
3313f4f56ff4SMark Johnston 				}
3314f4f56ff4SMark Johnston 			} else {
3315f4f56ff4SMark Johnston 				/* XXX */
3316f4f56ff4SMark Johnston 				qat_free_mem(ucode_cpybuf);
3317f4f56ff4SMark Johnston 				return ENOTSUP;
3318f4f56ff4SMark Johnston 			}
3319f4f56ff4SMark Johnston 		}
3320f4f56ff4SMark Johnston 
3321f4f56ff4SMark Johnston 		/* copy the buffer to ustore */
3322f4f56ff4SMark Johnston 		if (!qae->qae_shareable_ustore) {
3323f4f56ff4SMark Johnston 			error = qat_ae_ucode_write(sc, ae, upaddr, cpylen,
3324f4f56ff4SMark Johnston 			    ucode_cpybuf);
3325f4f56ff4SMark Johnston 			if (error)
3326f4f56ff4SMark Johnston 				return error;
3327f4f56ff4SMark Johnston 		} else {
3328f4f56ff4SMark Johnston 			/* XXX */
3329f4f56ff4SMark Johnston 			qat_free_mem(ucode_cpybuf);
3330f4f56ff4SMark Johnston 			return ENOTSUP;
3331f4f56ff4SMark Johnston 		}
3332f4f56ff4SMark Johnston 		upaddr += cpylen;
3333f4f56ff4SMark Johnston 		ninst -= cpylen;
3334f4f56ff4SMark Johnston 	}
3335f4f56ff4SMark Johnston 
3336f4f56ff4SMark Johnston 	qat_free_mem(ucode_cpybuf);
3337f4f56ff4SMark Johnston 
3338f4f56ff4SMark Johnston 	return 0;
3339f4f56ff4SMark Johnston }
3340f4f56ff4SMark Johnston 
3341f4f56ff4SMark Johnston static int
qat_aefw_uof_write_one(struct qat_softc * sc,struct qat_uof_image * qui)3342f4f56ff4SMark Johnston qat_aefw_uof_write_one(struct qat_softc *sc, struct qat_uof_image *qui)
3343f4f56ff4SMark Johnston {
3344f4f56ff4SMark Johnston 	struct uof_image *ui = qui->qui_image;
3345f4f56ff4SMark Johnston 	struct qat_ae_page *qap;
3346f4f56ff4SMark Johnston 	u_int s, p, c;
3347f4f56ff4SMark Johnston 	int error;
3348f4f56ff4SMark Johnston 	u_char ae, ctx_mask;
3349f4f56ff4SMark Johnston 
3350f4f56ff4SMark Johnston 	if (__SHIFTOUT(ui->ui_ae_mode, AE_MODE_CTX_MODE) == MAX_AE_CTX)
3351f4f56ff4SMark Johnston 		ctx_mask = 0xff; /* 8-ctx mode */
3352f4f56ff4SMark Johnston 	else
3353f4f56ff4SMark Johnston 		ctx_mask = 0x55; /* 4-ctx mode */
3354f4f56ff4SMark Johnston 
3355f4f56ff4SMark Johnston 	/* load the default page and set assigned CTX PC
3356f4f56ff4SMark Johnston 	 * to the entrypoint address */
3357f4f56ff4SMark Johnston 	for (ae = 0; ae < sc->sc_ae_num; ae++) {
3358f4f56ff4SMark Johnston 		struct qat_ae *qae = &(QAT_AE(sc, ae));
3359f4f56ff4SMark Johnston 		struct qat_ae_slice *qas;
3360f4f56ff4SMark Johnston 		u_int metadata;
3361f4f56ff4SMark Johnston 
3362f4f56ff4SMark Johnston 		MPASS(ae < UOF_MAX_NUM_OF_AE);
3363f4f56ff4SMark Johnston 
3364f4f56ff4SMark Johnston 		if ((ui->ui_ae_assigned & (1 << ae)) == 0)
3365f4f56ff4SMark Johnston 			continue;
3366f4f56ff4SMark Johnston 
3367f4f56ff4SMark Johnston 		/* find the slice to which this image is assigned */
3368f4f56ff4SMark Johnston 		for (s = 0; s < qae->qae_num_slices; s++) {
3369f4f56ff4SMark Johnston 			qas = &qae->qae_slices[s];
3370f4f56ff4SMark Johnston 			if (ui->ui_ctx_assigned & qas->qas_assigned_ctx_mask)
3371f4f56ff4SMark Johnston 				break;
3372f4f56ff4SMark Johnston 		}
3373f4f56ff4SMark Johnston 		if (s >= qae->qae_num_slices)
3374f4f56ff4SMark Johnston 			continue;
3375f4f56ff4SMark Johnston 
3376f4f56ff4SMark Johnston 		qas = &qae->qae_slices[s];
3377f4f56ff4SMark Johnston 
3378f4f56ff4SMark Johnston 		for (p = 0; p < ui->ui_num_pages; p++) {
3379f4f56ff4SMark Johnston 			qap = &qas->qas_pages[p];
3380f4f56ff4SMark Johnston 
3381f4f56ff4SMark Johnston 			/* Only load pages loaded by default */
3382f4f56ff4SMark Johnston 			if (!qap->qap_page->qup_def_page)
3383f4f56ff4SMark Johnston 				continue;
3384f4f56ff4SMark Johnston 
3385f4f56ff4SMark Johnston 			error = qat_aefw_do_pagein(sc, ae, qap->qap_page);
3386f4f56ff4SMark Johnston 			if (error)
3387f4f56ff4SMark Johnston 				return error;
3388f4f56ff4SMark Johnston 		}
3389f4f56ff4SMark Johnston 
3390f4f56ff4SMark Johnston 		metadata = qas->qas_image->qui_image->ui_app_metadata;
3391f4f56ff4SMark Johnston 		if (metadata != 0xffffffff && bootverbose) {
3392f4f56ff4SMark Johnston 			device_printf(sc->sc_dev,
3393f4f56ff4SMark Johnston 			    "loaded firmware: %s\n",
3394f4f56ff4SMark Johnston 			    qat_aefw_uof_string(sc, metadata));
3395f4f56ff4SMark Johnston 		}
3396f4f56ff4SMark Johnston 
3397f4f56ff4SMark Johnston 		/* Assume starting page is page 0 */
3398f4f56ff4SMark Johnston 		qap = &qas->qas_pages[0];
3399f4f56ff4SMark Johnston 		for (c = 0; c < MAX_AE_CTX; c++) {
3400f4f56ff4SMark Johnston 			if (ctx_mask & (1 << c))
3401f4f56ff4SMark Johnston 				qas->qas_cur_pages[c] = qap;
3402f4f56ff4SMark Johnston 			else
3403f4f56ff4SMark Johnston 				qas->qas_cur_pages[c] = NULL;
3404f4f56ff4SMark Johnston 		}
3405f4f56ff4SMark Johnston 
3406f4f56ff4SMark Johnston 		/* set the live context */
3407f4f56ff4SMark Johnston 		qae->qae_live_ctx_mask = ui->ui_ctx_assigned;
3408f4f56ff4SMark Johnston 
3409f4f56ff4SMark Johnston 		/* set context PC to the image entrypoint address */
3410f4f56ff4SMark Johnston 		error = qat_ae_write_pc(sc, ae, ui->ui_ctx_assigned,
3411f4f56ff4SMark Johnston 		    ui->ui_entry_address);
3412f4f56ff4SMark Johnston 		if (error)
3413f4f56ff4SMark Johnston 			return error;
3414f4f56ff4SMark Johnston 	}
3415f4f56ff4SMark Johnston 
3416f4f56ff4SMark Johnston 	/* XXX store the checksum for convenience */
3417f4f56ff4SMark Johnston 
3418f4f56ff4SMark Johnston 	return 0;
3419f4f56ff4SMark Johnston }
3420f4f56ff4SMark Johnston 
3421f4f56ff4SMark Johnston static int
qat_aefw_uof_write(struct qat_softc * sc)3422f4f56ff4SMark Johnston qat_aefw_uof_write(struct qat_softc *sc)
3423f4f56ff4SMark Johnston {
3424f4f56ff4SMark Johnston 	int error = 0;
3425f4f56ff4SMark Johnston 	int i;
3426f4f56ff4SMark Johnston 
3427f4f56ff4SMark Johnston 	error = qat_aefw_init_globals(sc);
3428f4f56ff4SMark Johnston 	if (error) {
3429f4f56ff4SMark Johnston 		device_printf(sc->sc_dev,
3430f4f56ff4SMark Johnston 		    "Could not initialize globals\n");
3431f4f56ff4SMark Johnston 		return error;
3432f4f56ff4SMark Johnston 	}
3433f4f56ff4SMark Johnston 
3434f4f56ff4SMark Johnston 	for (i = 0; i < sc->sc_aefw_uof.qafu_num_imgs; i++) {
3435f4f56ff4SMark Johnston 		error = qat_aefw_uof_write_one(sc,
3436f4f56ff4SMark Johnston 		    &sc->sc_aefw_uof.qafu_imgs[i]);
3437f4f56ff4SMark Johnston 		if (error)
3438f4f56ff4SMark Johnston 			break;
3439f4f56ff4SMark Johnston 	}
3440f4f56ff4SMark Johnston 
3441f4f56ff4SMark Johnston 	/* XXX UcLo_computeFreeUstore */
3442f4f56ff4SMark Johnston 
3443f4f56ff4SMark Johnston 	return error;
3444f4f56ff4SMark Johnston }
3445