1/*-
2 * Copyright (c) 2016 Chelsio Communications, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28#include "common.h"
29#include "t4_regs.h"
30#include "t4_regs_values.h"
31
32#undef msleep
33#define msleep(x) do { \
34	if (cold) \
35		DELAY((x) * 1000); \
36	else \
37		pause("t4hw", (x) * hz / 1000); \
38} while (0)
39
40/*
41 * Wait for the device to become ready (signified by our "who am I" register
42 * returning a value other than all 1's).  Return an error if it doesn't
43 * become ready ...
44 */
45int t4vf_wait_dev_ready(struct adapter *adapter)
46{
47	const u32 whoami = VF_PL_REG(A_PL_VF_WHOAMI);
48	const u32 notready1 = 0xffffffff;
49	const u32 notready2 = 0xeeeeeeee;
50	u32 val;
51
52	val = t4_read_reg(adapter, whoami);
53	if (val != notready1 && val != notready2)
54		return 0;
55	msleep(500);
56	val = t4_read_reg(adapter, whoami);
57	if (val != notready1 && val != notready2)
58		return 0;
59	else
60		return -EIO;
61}
62
63
64/**
65 *      t4vf_fw_reset - issue a reset to FW
66 *      @adapter: the adapter
67 *
68 *	Issues a reset command to FW.  For a Physical Function this would
69 *	result in the Firmware reseting all of its state.  For a Virtual
70 *	Function this just resets the state associated with the VF.
71 */
72int t4vf_fw_reset(struct adapter *adapter)
73{
74	struct fw_reset_cmd cmd;
75
76	memset(&cmd, 0, sizeof(cmd));
77	cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_RESET_CMD) |
78				      F_FW_CMD_WRITE);
79	cmd.retval_len16 = cpu_to_be32(V_FW_CMD_LEN16(FW_LEN16(cmd)));
80	return t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), NULL);
81}
82
83/**
84 *	t4vf_get_sge_params - retrieve adapter Scatter gather Engine parameters
85 *	@adapter: the adapter
86 *
87 *	Retrieves various core SGE parameters in the form of hardware SGE
88 *	register values.  The caller is responsible for decoding these as
89 *	needed.  The SGE parameters are stored in @adapter->params.sge.
90 */
91int t4vf_get_sge_params(struct adapter *adapter)
92{
93	struct sge_params *sp = &adapter->params.sge;
94	u32 params[7], vals[7];
95	u32 whoami;
96	unsigned int pf, s_hps;
97	int i, v;
98
99	params[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
100		     V_FW_PARAMS_PARAM_XYZ(A_SGE_CONTROL));
101	params[1] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
102		     V_FW_PARAMS_PARAM_XYZ(A_SGE_HOST_PAGE_SIZE));
103	params[2] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
104		     V_FW_PARAMS_PARAM_XYZ(A_SGE_TIMER_VALUE_0_AND_1));
105	params[3] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
106		     V_FW_PARAMS_PARAM_XYZ(A_SGE_TIMER_VALUE_2_AND_3));
107	params[4] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
108		     V_FW_PARAMS_PARAM_XYZ(A_SGE_TIMER_VALUE_4_AND_5));
109	params[5] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
110		     V_FW_PARAMS_PARAM_XYZ(A_SGE_CONM_CTRL));
111	params[6] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
112		     V_FW_PARAMS_PARAM_XYZ(A_SGE_INGRESS_RX_THRESHOLD));
113	v = t4vf_query_params(adapter, 7, params, vals);
114	if (v != FW_SUCCESS)
115		return v;
116
117	sp->sge_control = vals[0];
118	sp->counter_val[0] = G_THRESHOLD_0(vals[6]);
119	sp->counter_val[1] = G_THRESHOLD_1(vals[6]);
120	sp->counter_val[2] = G_THRESHOLD_2(vals[6]);
121	sp->counter_val[3] = G_THRESHOLD_3(vals[6]);
122	sp->timer_val[0] = core_ticks_to_us(adapter, G_TIMERVALUE0(vals[2]));
123	sp->timer_val[1] = core_ticks_to_us(adapter, G_TIMERVALUE1(vals[2]));
124	sp->timer_val[2] = core_ticks_to_us(adapter, G_TIMERVALUE2(vals[3]));
125	sp->timer_val[3] = core_ticks_to_us(adapter, G_TIMERVALUE3(vals[3]));
126	sp->timer_val[4] = core_ticks_to_us(adapter, G_TIMERVALUE4(vals[4]));
127	sp->timer_val[5] = core_ticks_to_us(adapter, G_TIMERVALUE5(vals[4]));
128
129	sp->fl_starve_threshold = G_EGRTHRESHOLD(vals[5]) * 2 + 1;
130	if (is_t4(adapter))
131		sp->fl_starve_threshold2 = sp->fl_starve_threshold;
132	else if (is_t5(adapter))
133		sp->fl_starve_threshold2 = G_EGRTHRESHOLDPACKING(vals[5]) * 2 + 1;
134	else
135		sp->fl_starve_threshold2 = G_T6_EGRTHRESHOLDPACKING(vals[5]) * 2 + 1;
136
137	/*
138	 * We need the Queues/Page and Host Page Size for our VF.
139	 * This is based on the PF from which we're instantiated.
140	 */
141	whoami = t4_read_reg(adapter, VF_PL_REG(A_PL_VF_WHOAMI));
142	pf = G_SOURCEPF(whoami);
143
144	s_hps = (S_HOSTPAGESIZEPF0 +
145	    (S_HOSTPAGESIZEPF1 - S_HOSTPAGESIZEPF0) * pf);
146	sp->page_shift = ((vals[1] >> s_hps) & M_HOSTPAGESIZEPF0) + 10;
147
148	for (i = 0; i < SGE_FLBUF_SIZES; i++) {
149		params[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
150		    V_FW_PARAMS_PARAM_XYZ(A_SGE_FL_BUFFER_SIZE0 + (4 * i)));
151		v = t4vf_query_params(adapter, 1, params, vals);
152		if (v != FW_SUCCESS)
153			return v;
154
155		sp->sge_fl_buffer_size[i] = vals[0];
156	}
157
158	/*
159	 * T4 uses a single control field to specify both the PCIe Padding and
160	 * Packing Boundary.  T5 introduced the ability to specify these
161	 * separately with the Padding Boundary in SGE_CONTROL and and Packing
162	 * Boundary in SGE_CONTROL2.  So for T5 and later we need to grab
163	 * SGE_CONTROL in order to determine how ingress packet data will be
164	 * laid out in Packed Buffer Mode.  Unfortunately, older versions of
165	 * the firmware won't let us retrieve SGE_CONTROL2 so if we get a
166	 * failure grabbing it we throw an error since we can't figure out the
167	 * right value.
168	 */
169	sp->spg_len = sp->sge_control & F_EGRSTATUSPAGESIZE ? 128 : 64;
170	sp->fl_pktshift = G_PKTSHIFT(sp->sge_control);
171	if (chip_id(adapter) <= CHELSIO_T5) {
172		sp->pad_boundary = 1 << (G_INGPADBOUNDARY(sp->sge_control) +
173		    X_INGPADBOUNDARY_SHIFT);
174	} else {
175		sp->pad_boundary = 1 << (G_INGPADBOUNDARY(sp->sge_control) +
176		    X_T6_INGPADBOUNDARY_SHIFT);
177	}
178	if (is_t4(adapter))
179		sp->pack_boundary = sp->pad_boundary;
180	else {
181		params[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
182			     V_FW_PARAMS_PARAM_XYZ(A_SGE_CONTROL2));
183		v = t4vf_query_params(adapter, 1, params, vals);
184		if (v != FW_SUCCESS) {
185			CH_ERR(adapter, "Unable to get SGE Control2; "
186			       "probably old firmware.\n");
187			return v;
188		}
189		if (G_INGPACKBOUNDARY(vals[0]) == 0)
190			sp->pack_boundary = 16;
191		else
192			sp->pack_boundary = 1 << (G_INGPACKBOUNDARY(vals[0]) +
193			    5);
194	}
195
196	/*
197	 * For T5 and later we want to use the new BAR2 Doorbells.
198	 * Unfortunately, older firmware didn't allow the this register to be
199	 * read.
200	 */
201	if (!is_t4(adapter)) {
202		unsigned int s_qpp;
203
204		params[0] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
205			     V_FW_PARAMS_PARAM_XYZ(A_SGE_EGRESS_QUEUES_PER_PAGE_VF));
206		params[1] = (V_FW_PARAMS_MNEM(FW_PARAMS_MNEM_REG) |
207			     V_FW_PARAMS_PARAM_XYZ(A_SGE_INGRESS_QUEUES_PER_PAGE_VF));
208		v = t4vf_query_params(adapter, 2, params, vals);
209		if (v != FW_SUCCESS) {
210			CH_WARN(adapter, "Unable to get VF SGE Queues/Page; "
211				"probably old firmware.\n");
212			return v;
213		}
214
215		s_qpp = (S_QUEUESPERPAGEPF0 +
216			 (S_QUEUESPERPAGEPF1 - S_QUEUESPERPAGEPF0) * pf);
217		sp->eq_s_qpp = ((vals[0] >> s_qpp) & M_QUEUESPERPAGEPF0);
218		sp->iq_s_qpp = ((vals[1] >> s_qpp) & M_QUEUESPERPAGEPF0);
219	}
220
221	return 0;
222}
223
224/**
225 *	t4vf_get_rss_glb_config - retrieve adapter RSS Global Configuration
226 *	@adapter: the adapter
227 *
228 *	Retrieves global RSS mode and parameters with which we have to live
229 *	and stores them in the @adapter's RSS parameters.
230 */
231int t4vf_get_rss_glb_config(struct adapter *adapter)
232{
233	struct rss_params *rss = &adapter->params.rss;
234	struct fw_rss_glb_config_cmd cmd, rpl;
235	int v;
236
237	/*
238	 * Execute an RSS Global Configuration read command to retrieve
239	 * our RSS configuration.
240	 */
241	memset(&cmd, 0, sizeof(cmd));
242	cmd.op_to_write = cpu_to_be32(V_FW_CMD_OP(FW_RSS_GLB_CONFIG_CMD) |
243				      F_FW_CMD_REQUEST |
244				      F_FW_CMD_READ);
245	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
246	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
247	if (v != FW_SUCCESS)
248		return v;
249
250	/*
251	 * Transate the big-endian RSS Global Configuration into our
252	 * cpu-endian format based on the RSS mode.  We also do first level
253	 * filtering at this point to weed out modes which don't support
254	 * VF Drivers ...
255	 */
256	rss->mode = G_FW_RSS_GLB_CONFIG_CMD_MODE(
257			be32_to_cpu(rpl.u.manual.mode_pkd));
258	switch (rss->mode) {
259	case FW_RSS_GLB_CONFIG_CMD_MODE_BASICVIRTUAL: {
260		u32 word = be32_to_cpu(
261				rpl.u.basicvirtual.synmapen_to_hashtoeplitz);
262
263		rss->u.basicvirtual.synmapen =
264			((word & F_FW_RSS_GLB_CONFIG_CMD_SYNMAPEN) != 0);
265		rss->u.basicvirtual.syn4tupenipv6 =
266			((word & F_FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV6) != 0);
267		rss->u.basicvirtual.syn2tupenipv6 =
268			((word & F_FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV6) != 0);
269		rss->u.basicvirtual.syn4tupenipv4 =
270			((word & F_FW_RSS_GLB_CONFIG_CMD_SYN4TUPENIPV4) != 0);
271		rss->u.basicvirtual.syn2tupenipv4 =
272			((word & F_FW_RSS_GLB_CONFIG_CMD_SYN2TUPENIPV4) != 0);
273
274		rss->u.basicvirtual.ofdmapen =
275			((word & F_FW_RSS_GLB_CONFIG_CMD_OFDMAPEN) != 0);
276
277		rss->u.basicvirtual.tnlmapen =
278			((word & F_FW_RSS_GLB_CONFIG_CMD_TNLMAPEN) != 0);
279		rss->u.basicvirtual.tnlalllookup =
280			((word  & F_FW_RSS_GLB_CONFIG_CMD_TNLALLLKP) != 0);
281
282		rss->u.basicvirtual.hashtoeplitz =
283			((word & F_FW_RSS_GLB_CONFIG_CMD_HASHTOEPLITZ) != 0);
284
285		/* we need at least Tunnel Map Enable to be set */
286		if (!rss->u.basicvirtual.tnlmapen)
287			return -EINVAL;
288		break;
289	}
290
291	default:
292		/* all unknown/unsupported RSS modes result in an error */
293		return -EINVAL;
294	}
295
296	return 0;
297}
298
299/**
300 *	t4vf_get_vfres - retrieve VF resource limits
301 *	@adapter: the adapter
302 *
303 *	Retrieves configured resource limits and capabilities for a virtual
304 *	function.  The results are stored in @adapter->vfres.
305 */
306int t4vf_get_vfres(struct adapter *adapter)
307{
308	struct vf_resources *vfres = &adapter->params.vfres;
309	struct fw_pfvf_cmd cmd, rpl;
310	int v;
311	u32 word;
312
313	/*
314	 * Execute PFVF Read command to get VF resource limits; bail out early
315	 * with error on command failure.
316	 */
317	memset(&cmd, 0, sizeof(cmd));
318	cmd.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_PFVF_CMD) |
319				    F_FW_CMD_REQUEST |
320				    F_FW_CMD_READ);
321	cmd.retval_len16 = cpu_to_be32(FW_LEN16(cmd));
322	v = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &rpl);
323	if (v != FW_SUCCESS)
324		return v;
325
326	/*
327	 * Extract VF resource limits and return success.
328	 */
329	word = be32_to_cpu(rpl.niqflint_niq);
330	vfres->niqflint = G_FW_PFVF_CMD_NIQFLINT(word);
331	vfres->niq = G_FW_PFVF_CMD_NIQ(word);
332
333	word = be32_to_cpu(rpl.type_to_neq);
334	vfres->neq = G_FW_PFVF_CMD_NEQ(word);
335	vfres->pmask = G_FW_PFVF_CMD_PMASK(word);
336
337	word = be32_to_cpu(rpl.tc_to_nexactf);
338	vfres->tc = G_FW_PFVF_CMD_TC(word);
339	vfres->nvi = G_FW_PFVF_CMD_NVI(word);
340	vfres->nexactf = G_FW_PFVF_CMD_NEXACTF(word);
341
342	word = be32_to_cpu(rpl.r_caps_to_nethctrl);
343	vfres->r_caps = G_FW_PFVF_CMD_R_CAPS(word);
344	vfres->wx_caps = G_FW_PFVF_CMD_WX_CAPS(word);
345	vfres->nethctrl = G_FW_PFVF_CMD_NETHCTRL(word);
346
347	return 0;
348}
349
350/**
351 */
352int t4vf_prep_adapter(struct adapter *adapter)
353{
354	int err;
355
356	/*
357	 * Wait for the device to become ready before proceeding ...
358	 */
359	err = t4vf_wait_dev_ready(adapter);
360	if (err)
361		return err;
362
363	adapter->params.chipid = pci_get_device(adapter->dev) >> 12;
364	if (adapter->params.chipid >= 0xa) {
365		adapter->params.chipid -= (0xa - 0x4);
366		adapter->params.fpga = 1;
367	}
368
369	/*
370	 * Default port and clock for debugging in case we can't reach
371	 * firmware.
372	 */
373	adapter->params.nports = 1;
374	adapter->params.vfres.pmask = 1;
375	adapter->params.vpd.cclk = 50000;
376
377	adapter->chip_params = t4_get_chip_params(chip_id(adapter));
378	if (adapter->chip_params == NULL)
379		return -EINVAL;
380
381	return 0;
382}
383
384/*
385 *	t4vf_get_vf_mac - Get the MAC address to be set to the VI of this VF.
386 *	@adapter: The adapter
387 *	@port: The port associated with vf
388 *	@naddr: the number of ACL MAC addresses returned in addr
389 *	@addr: Placeholder for MAC addresses
390 *
391 *	Find the MAC address to be set to the VF's VI. The requested MAC address
392 *	is from the host OS via callback in the PF driver.
393 */
394int t4vf_get_vf_mac(struct adapter *adapter, unsigned int port,
395		    unsigned int *naddr, u8 *addr)
396{
397	struct fw_acl_mac_cmd cmd;
398	int ret;
399
400	memset(&cmd, 0, sizeof(cmd));
401	cmd.op_to_vfn = cpu_to_be32(V_FW_CMD_OP(FW_ACL_MAC_CMD) |
402			      F_FW_CMD_REQUEST |
403			      F_FW_CMD_READ);
404	cmd.en_to_len16 = cpu_to_be32((unsigned int)FW_LEN16(cmd));
405	ret = t4vf_wr_mbox(adapter, &cmd, sizeof(cmd), &cmd);
406	if (ret)
407		return ret;
408
409	if (cmd.nmac < *naddr)
410		*naddr = cmd.nmac;
411
412	switch (port) {
413	case 3:
414		memcpy(addr, cmd.macaddr3, sizeof(cmd.macaddr3));
415		break;
416	case 2:
417		memcpy(addr, cmd.macaddr2, sizeof(cmd.macaddr2));
418		break;
419	case 1:
420		memcpy(addr, cmd.macaddr1, sizeof(cmd.macaddr1));
421		break;
422	case 0:
423		memcpy(addr, cmd.macaddr0, sizeof(cmd.macaddr0));
424		break;
425	}
426
427	return ret;
428}
429