1/*	$NetBSD: qat_d15xx.c,v 1.1 2019/11/20 09:37:46 hikaru Exp $	*/
2
3/*
4 * Copyright (c) 2019 Internet Initiative Japan, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 *   Copyright(c) 2014 Intel Corporation.
31 *   Redistribution and use in source and binary forms, with or without
32 *   modification, are permitted provided that the following conditions
33 *   are met:
34 *
35 *     * Redistributions of source code must retain the above copyright
36 *       notice, this list of conditions and the following disclaimer.
37 *     * Redistributions in binary form must reproduce the above copyright
38 *       notice, this list of conditions and the following disclaimer in
39 *       the documentation and/or other materials provided with the
40 *       distribution.
41 *     * Neither the name of Intel Corporation nor the names of its
42 *       contributors may be used to endorse or promote products derived
43 *       from this software without specific prior written permission.
44 *
45 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
49 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
55 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 */
57
58#include <sys/cdefs.h>
59__KERNEL_RCSID(0, "$NetBSD: qat_d15xx.c,v 1.1 2019/11/20 09:37:46 hikaru Exp $");
60
61#include <sys/param.h>
62#include <sys/systm.h>
63
64#include <dev/pci/pcireg.h>
65#include <dev/pci/pcivar.h>
66
67#include "qatreg.h"
68#include "qat_hw17reg.h"
69#include "qat_d15xxreg.h"
70#include "qatvar.h"
71#include "qat_hw17var.h"
72
73static uint32_t
74qat_d15xx_get_accel_mask(struct qat_softc *sc)
75{
76	pcireg_t fusectl, strap;
77
78	fusectl = pci_conf_read(sc->sc_pc, sc->sc_pcitag, FUSECTL_REG);
79	strap = pci_conf_read(sc->sc_pc, sc->sc_pcitag, SOFTSTRAP_REG_D15XX);
80
81	return (((~(fusectl | strap)) >> ACCEL_REG_OFFSET_D15XX) &
82	    ACCEL_MASK_D15XX);
83}
84
85static uint32_t
86qat_d15xx_get_ae_mask(struct qat_softc *sc)
87{
88	pcireg_t fusectl, me_strap, me_disable, ssms_disabled;
89
90	fusectl = pci_conf_read(sc->sc_pc, sc->sc_pcitag, FUSECTL_REG);
91	me_strap = pci_conf_read(sc->sc_pc, sc->sc_pcitag, SOFTSTRAP_REG_D15XX);
92
93	/* If SSMs are disabled, then disable the corresponding MEs */
94	ssms_disabled = (~qat_d15xx_get_accel_mask(sc)) & ACCEL_MASK_D15XX;
95	me_disable = 0x3;
96	while (ssms_disabled) {
97		if (ssms_disabled & 1)
98			me_strap |= me_disable;
99		ssms_disabled >>= 1;
100		me_disable <<= 2;
101	}
102
103	return (~(fusectl | me_strap)) & AE_MASK_D15XX;
104}
105
106static enum qat_sku
107qat_d15xx_get_sku(struct qat_softc *sc)
108{
109	switch (sc->sc_ae_num) {
110	case 8:
111		return QAT_SKU_2;
112	case MAX_AE_D15XX:
113		return QAT_SKU_4;
114	}
115
116	return QAT_SKU_UNKNOWN;
117}
118
119static uint32_t
120qat_d15xx_get_accel_cap(struct qat_softc *sc)
121{
122	uint32_t cap;
123	pcireg_t legfuse, strap;
124
125	legfuse = pci_conf_read(sc->sc_pc, sc->sc_pcitag, LEGFUSE_REG);
126	strap = pci_conf_read(sc->sc_pc, sc->sc_pcitag, SOFTSTRAP_REG_D15XX);
127
128	cap = QAT_ACCEL_CAP_CRYPTO_SYMMETRIC +
129		QAT_ACCEL_CAP_CRYPTO_ASYMMETRIC +
130		QAT_ACCEL_CAP_CIPHER +
131		QAT_ACCEL_CAP_AUTHENTICATION +
132		QAT_ACCEL_CAP_COMPRESSION +
133		QAT_ACCEL_CAP_ZUC +
134		QAT_ACCEL_CAP_SHA3;
135
136	if (legfuse & LEGFUSE_ACCEL_MASK_CIPHER_SLICE) {
137		cap &= ~QAT_ACCEL_CAP_CRYPTO_SYMMETRIC;
138		cap &= ~QAT_ACCEL_CAP_CIPHER;
139	}
140	if (legfuse & LEGFUSE_ACCEL_MASK_AUTH_SLICE)
141		cap &= ~QAT_ACCEL_CAP_AUTHENTICATION;
142	if (legfuse & LEGFUSE_ACCEL_MASK_PKE_SLICE)
143		cap &= ~QAT_ACCEL_CAP_CRYPTO_ASYMMETRIC;
144	if (legfuse & LEGFUSE_ACCEL_MASK_COMPRESS_SLICE)
145		cap &= ~QAT_ACCEL_CAP_COMPRESSION;
146	if (legfuse & LEGFUSE_ACCEL_MASK_EIA3_SLICE)
147		cap &= ~QAT_ACCEL_CAP_ZUC;
148
149	if ((strap | legfuse) & SOFTSTRAP_SS_POWERGATE_PKE_D15XX)
150		cap &= ~QAT_ACCEL_CAP_CRYPTO_ASYMMETRIC;
151	if ((strap | legfuse) & SOFTSTRAP_SS_POWERGATE_CY_D15XX)
152		cap &= ~QAT_ACCEL_CAP_COMPRESSION;
153
154	return cap;
155}
156
157static const char *
158qat_d15xx_get_fw_uof_name(struct qat_softc *sc)
159{
160
161	return AE_FW_UOF_NAME_D15XX;
162}
163
164static void
165qat_d15xx_enable_intr(struct qat_softc *sc)
166{
167
168	/* Enable bundle and misc interrupts */
169	qat_misc_write_4(sc, SMIAPF0_D15XX, SMIA0_MASK_D15XX);
170	qat_misc_write_4(sc, SMIAPF1_D15XX, SMIA1_MASK_D15XX);
171}
172
173/* Worker thread to service arbiter mappings */
174static uint32_t thrd_to_arb_map[] = {
175	0x12222AAA, 0x11222AAA, 0x12222AAA, 0x11222AAA, 0x12222AAA,
176	0x11222AAA, 0x12222AAA, 0x11222AAA, 0x12222AAA, 0x11222AAA
177};
178
179static void
180qat_d15xx_get_arb_mapping(struct qat_softc *sc, const uint32_t **arb_map_config)
181{
182	int i;
183
184	for (i = 1; i < MAX_AE_D15XX; i++) {
185		if ((~sc->sc_ae_mask) & (1 << i))
186			thrd_to_arb_map[i] = 0;
187	}
188	*arb_map_config = thrd_to_arb_map;
189}
190
191static void
192qat_d15xx_enable_error_interrupts(struct qat_softc *sc)
193{
194	qat_misc_write_4(sc, ERRMSK0, ERRMSK0_CERR_D15XX); /* ME0-ME3 */
195	qat_misc_write_4(sc, ERRMSK1, ERRMSK1_CERR_D15XX); /* ME4-ME7 */
196	qat_misc_write_4(sc, ERRMSK4, ERRMSK4_CERR_D15XX); /* ME8-ME9 */
197	qat_misc_write_4(sc, ERRMSK5, ERRMSK5_CERR_D15XX); /* SSM2-SSM4 */
198
199	/* Reset everything except VFtoPF1_16. */
200	qat_misc_read_write_and_4(sc, ERRMSK3, VF2PF1_16_D15XX);
201	/* Disable Secure RAM correctable error interrupt */
202	qat_misc_read_write_or_4(sc, ERRMSK3, ERRMSK3_CERR_D15XX);
203
204	/* RI CPP bus interface error detection and reporting. */
205	qat_misc_write_4(sc, RICPPINTCTL_D15XX, RICPP_EN_D15XX);
206
207	/* TI CPP bus interface error detection and reporting. */
208	qat_misc_write_4(sc, TICPPINTCTL_D15XX, TICPP_EN_D15XX);
209
210	/* Enable CFC Error interrupts and logging. */
211	qat_misc_write_4(sc, CPP_CFC_ERR_CTRL_D15XX, CPP_CFC_UE_D15XX);
212
213	/* Enable SecureRAM to fix and log Correctable errors */
214	qat_misc_write_4(sc, SECRAMCERR_D15XX, SECRAM_CERR_D15XX);
215
216	/* Enable SecureRAM Uncorrectable error interrupts and logging */
217	qat_misc_write_4(sc, SECRAMUERR, SECRAM_UERR_D15XX);
218
219	/* Enable Push/Pull Misc Uncorrectable error interrupts and logging */
220	qat_misc_write_4(sc, CPPMEMTGTERR, TGT_UERR_D15XX);
221}
222
223static void
224qat_d15xx_disable_error_interrupts(struct qat_softc *sc)
225{
226	/* ME0-ME3 */
227	qat_misc_write_4(sc, ERRMSK0, ERRMSK0_UERR_D15XX | ERRMSK0_CERR_D15XX);
228	/* ME4-ME7 */
229	qat_misc_write_4(sc, ERRMSK1, ERRMSK1_UERR_D15XX | ERRMSK1_CERR_D15XX);
230	/* Secure RAM, CPP Push Pull, RI, TI, SSM0-SSM1, CFC */
231	qat_misc_write_4(sc, ERRMSK3, ERRMSK3_UERR_D15XX | ERRMSK3_CERR_D15XX);
232	/* ME8-ME9 */
233	qat_misc_write_4(sc, ERRMSK4, ERRMSK4_UERR_D15XX | ERRMSK4_CERR_D15XX);
234	/* SSM2-SSM4 */
235	qat_misc_write_4(sc, ERRMSK5, ERRMSK5_UERR_D15XX | ERRMSK5_CERR_D15XX);
236}
237
238static void
239qat_d15xx_enable_error_correction(struct qat_softc *sc)
240{
241	u_int i, mask;
242
243	/* Enable Accel Engine error detection & correction */
244	for (i = 0, mask = sc->sc_ae_mask; mask; i++, mask >>= 1) {
245		if (!(mask & 1))
246			continue;
247		qat_misc_read_write_or_4(sc, AE_CTX_ENABLES_D15XX(i),
248		    ENABLE_AE_ECC_ERR_D15XX);
249		qat_misc_read_write_or_4(sc, AE_MISC_CONTROL_D15XX(i),
250		    ENABLE_AE_ECC_PARITY_CORR_D15XX);
251	}
252
253	/* Enable shared memory error detection & correction */
254	for (i = 0, mask = sc->sc_accel_mask; mask; i++, mask >>= 1) {
255		if (!(mask & 1))
256			continue;
257
258		qat_misc_read_write_or_4(sc, UERRSSMSH(i), ERRSSMSH_EN_D15XX);
259		qat_misc_read_write_or_4(sc, CERRSSMSH(i), ERRSSMSH_EN_D15XX);
260		qat_misc_read_write_or_4(sc, PPERR(i), PPERR_EN_D15XX);
261	}
262
263	qat_d15xx_enable_error_interrupts(sc);
264}
265
266const struct qat_hw qat_hw_d15xx = {
267	.qhw_sram_bar_id = BAR_SRAM_ID_D15XX,
268	.qhw_misc_bar_id = BAR_PMISC_ID_D15XX,
269	.qhw_etr_bar_id = BAR_ETR_ID_D15XX,
270	.qhw_cap_global_offset = CAP_GLOBAL_OFFSET_D15XX,
271	.qhw_ae_offset = AE_OFFSET_D15XX,
272	.qhw_ae_local_offset = AE_LOCAL_OFFSET_D15XX,
273	.qhw_etr_bundle_size = ETR_BUNDLE_SIZE_D15XX,
274	.qhw_num_banks = ETR_MAX_BANKS_D15XX,
275	.qhw_num_rings_per_bank = ETR_MAX_RINGS_PER_BANK,
276	.qhw_num_accel = MAX_ACCEL_D15XX,
277	.qhw_num_engines = MAX_AE_D15XX,
278	.qhw_tx_rx_gap = ETR_TX_RX_GAP_D15XX,
279	.qhw_tx_rings_mask = ETR_TX_RINGS_MASK_D15XX,
280	.qhw_clock_per_sec = CLOCK_PER_SEC_D15XX,
281	.qhw_fw_auth = true,
282	.qhw_fw_req_size = FW_REQ_DEFAULT_SZ_HW17,
283	.qhw_fw_resp_size = FW_RESP_DEFAULT_SZ_HW17,
284	.qhw_ring_asym_tx = 0,
285	.qhw_ring_asym_rx = 8,
286	.qhw_ring_sym_tx = 2,
287	.qhw_ring_sym_rx = 10,
288	.qhw_mof_fwname = AE_FW_MOF_NAME_D15XX,
289	.qhw_mmp_fwname = AE_FW_MMP_NAME_D15XX,
290	.qhw_prod_type = AE_FW_PROD_TYPE_D15XX,
291	.qhw_get_accel_mask = qat_d15xx_get_accel_mask,
292	.qhw_get_ae_mask = qat_d15xx_get_ae_mask,
293	.qhw_get_sku = qat_d15xx_get_sku,
294	.qhw_get_accel_cap = qat_d15xx_get_accel_cap,
295	.qhw_get_fw_uof_name = qat_d15xx_get_fw_uof_name,
296	.qhw_enable_intr = qat_d15xx_enable_intr,
297	.qhw_init_admin_comms = qat_adm_mailbox_init,
298	.qhw_send_admin_init = qat_adm_mailbox_send_init,
299	.qhw_init_arb = qat_arb_init,
300	.qhw_get_arb_mapping = qat_d15xx_get_arb_mapping,
301	.qhw_enable_error_correction = qat_d15xx_enable_error_correction,
302	.qhw_disable_error_interrupts = qat_d15xx_disable_error_interrupts,
303	.qhw_set_ssm_wdtimer = qat_set_ssm_wdtimer,
304	.qhw_check_slice_hang = qat_check_slice_hang,
305	.qhw_crypto_setup_desc = qat_hw17_crypto_setup_desc,
306	.qhw_crypto_setup_req_params = qat_hw17_crypto_setup_req_params,
307	.qhw_crypto_opaque_offset = offsetof(struct fw_la_resp, opaque_data),
308};
309