• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015-2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 /*
35  * nfp_nsp.c
36  * Author: Jakub Kicinski <jakub.kicinski@netronome.com>
37  *         Jason McMullan <jason.mcmullan@netronome.com>
38  */
39 
40 #include <linux/bitfield.h>
41 #include <linux/delay.h>
42 #include <linux/firmware.h>
43 #include <linux/kernel.h>
44 #include <linux/kthread.h>
45 #include <linux/sizes.h>
46 #include <linux/slab.h>
47 
48 #define NFP_SUBSYS "nfp_nsp"
49 
50 #include "nfp.h"
51 #include "nfp_cpp.h"
52 #include "nfp_nsp.h"
53 
54 /* Offsets relative to the CSR base */
55 #define NSP_STATUS		0x00
56 #define   NSP_STATUS_MAGIC	GENMASK_ULL(63, 48)
57 #define   NSP_STATUS_MAJOR	GENMASK_ULL(47, 44)
58 #define   NSP_STATUS_MINOR	GENMASK_ULL(43, 32)
59 #define   NSP_STATUS_CODE	GENMASK_ULL(31, 16)
60 #define   NSP_STATUS_RESULT	GENMASK_ULL(15, 8)
61 #define   NSP_STATUS_BUSY	BIT_ULL(0)
62 
63 #define NSP_COMMAND		0x08
64 #define   NSP_COMMAND_OPTION	GENMASK_ULL(63, 32)
65 #define   NSP_COMMAND_CODE	GENMASK_ULL(31, 16)
66 #define   NSP_COMMAND_START	BIT_ULL(0)
67 
68 /* CPP address to retrieve the data from */
69 #define NSP_BUFFER		0x10
70 #define   NSP_BUFFER_CPP	GENMASK_ULL(63, 40)
71 #define   NSP_BUFFER_ADDRESS	GENMASK_ULL(39, 0)
72 
73 #define NSP_DFLT_BUFFER		0x18
74 #define   NSP_DFLT_BUFFER_CPP	GENMASK_ULL(63, 40)
75 #define   NSP_DFLT_BUFFER_ADDRESS	GENMASK_ULL(39, 0)
76 
77 #define NSP_DFLT_BUFFER_CONFIG	0x20
78 #define   NSP_DFLT_BUFFER_SIZE_MB	GENMASK_ULL(7, 0)
79 
80 #define NSP_MAGIC		0xab10
81 #define NSP_MAJOR		0
82 #define NSP_MINOR		8
83 
84 #define NSP_CODE_MAJOR		GENMASK(15, 12)
85 #define NSP_CODE_MINOR		GENMASK(11, 0)
86 
87 enum nfp_nsp_cmd {
88 	SPCODE_NOOP		= 0, /* No operation */
89 	SPCODE_SOFT_RESET	= 1, /* Soft reset the NFP */
90 	SPCODE_FW_DEFAULT	= 2, /* Load default (UNDI) FW */
91 	SPCODE_PHY_INIT		= 3, /* Initialize the PHY */
92 	SPCODE_MAC_INIT		= 4, /* Initialize the MAC */
93 	SPCODE_PHY_RXADAPT	= 5, /* Re-run PHY RX Adaptation */
94 	SPCODE_FW_LOAD		= 6, /* Load fw from buffer, len in option */
95 	SPCODE_ETH_RESCAN	= 7, /* Rescan ETHs, write ETH_TABLE to buf */
96 	SPCODE_ETH_CONTROL	= 8, /* Update media config from buffer */
97 	SPCODE_NSP_SENSORS	= 12, /* Read NSP sensor(s) */
98 	SPCODE_NSP_IDENTIFY	= 13, /* Read NSP version */
99 };
100 
101 static const struct {
102 	int code;
103 	const char *msg;
104 } nsp_errors[] = {
105 	{ 6010, "could not map to phy for port" },
106 	{ 6011, "not an allowed rate/lanes for port" },
107 	{ 6012, "not an allowed rate/lanes for port" },
108 	{ 6013, "high/low error, change other port first" },
109 	{ 6014, "config not found in flash" },
110 };
111 
112 struct nfp_nsp {
113 	struct nfp_cpp *cpp;
114 	struct nfp_resource *res;
115 	struct {
116 		u16 major;
117 		u16 minor;
118 	} ver;
119 
120 	/* Eth table config state */
121 	bool modified;
122 	unsigned int idx;
123 	void *entries;
124 };
125 
nfp_nsp_cpp(struct nfp_nsp * state)126 struct nfp_cpp *nfp_nsp_cpp(struct nfp_nsp *state)
127 {
128 	return state->cpp;
129 }
130 
nfp_nsp_config_modified(struct nfp_nsp * state)131 bool nfp_nsp_config_modified(struct nfp_nsp *state)
132 {
133 	return state->modified;
134 }
135 
nfp_nsp_config_set_modified(struct nfp_nsp * state,bool modified)136 void nfp_nsp_config_set_modified(struct nfp_nsp *state, bool modified)
137 {
138 	state->modified = modified;
139 }
140 
nfp_nsp_config_entries(struct nfp_nsp * state)141 void *nfp_nsp_config_entries(struct nfp_nsp *state)
142 {
143 	return state->entries;
144 }
145 
nfp_nsp_config_idx(struct nfp_nsp * state)146 unsigned int nfp_nsp_config_idx(struct nfp_nsp *state)
147 {
148 	return state->idx;
149 }
150 
151 void
nfp_nsp_config_set_state(struct nfp_nsp * state,void * entries,unsigned int idx)152 nfp_nsp_config_set_state(struct nfp_nsp *state, void *entries, unsigned int idx)
153 {
154 	state->entries = entries;
155 	state->idx = idx;
156 }
157 
nfp_nsp_config_clear_state(struct nfp_nsp * state)158 void nfp_nsp_config_clear_state(struct nfp_nsp *state)
159 {
160 	state->entries = NULL;
161 	state->idx = 0;
162 }
163 
nfp_nsp_print_extended_error(struct nfp_nsp * state,u32 ret_val)164 static void nfp_nsp_print_extended_error(struct nfp_nsp *state, u32 ret_val)
165 {
166 	int i;
167 
168 	if (!ret_val)
169 		return;
170 
171 	for (i = 0; i < ARRAY_SIZE(nsp_errors); i++)
172 		if (ret_val == nsp_errors[i].code)
173 			nfp_err(state->cpp, "err msg: %s\n", nsp_errors[i].msg);
174 }
175 
nfp_nsp_check(struct nfp_nsp * state)176 static int nfp_nsp_check(struct nfp_nsp *state)
177 {
178 	struct nfp_cpp *cpp = state->cpp;
179 	u64 nsp_status, reg;
180 	u32 nsp_cpp;
181 	int err;
182 
183 	nsp_cpp = nfp_resource_cpp_id(state->res);
184 	nsp_status = nfp_resource_address(state->res) + NSP_STATUS;
185 
186 	err = nfp_cpp_readq(cpp, nsp_cpp, nsp_status, &reg);
187 	if (err < 0)
188 		return err;
189 
190 	if (FIELD_GET(NSP_STATUS_MAGIC, reg) != NSP_MAGIC) {
191 		nfp_err(cpp, "Cannot detect NFP Service Processor\n");
192 		return -ENODEV;
193 	}
194 
195 	state->ver.major = FIELD_GET(NSP_STATUS_MAJOR, reg);
196 	state->ver.minor = FIELD_GET(NSP_STATUS_MINOR, reg);
197 
198 	if (state->ver.major != NSP_MAJOR || state->ver.minor < NSP_MINOR) {
199 		nfp_err(cpp, "Unsupported ABI %hu.%hu\n",
200 			state->ver.major, state->ver.minor);
201 		return -EINVAL;
202 	}
203 
204 	if (reg & NSP_STATUS_BUSY) {
205 		nfp_err(cpp, "Service processor busy!\n");
206 		return -EBUSY;
207 	}
208 
209 	return 0;
210 }
211 
212 /**
213  * nfp_nsp_open() - Prepare for communication and lock the NSP resource.
214  * @cpp:	NFP CPP Handle
215  */
nfp_nsp_open(struct nfp_cpp * cpp)216 struct nfp_nsp *nfp_nsp_open(struct nfp_cpp *cpp)
217 {
218 	struct nfp_resource *res;
219 	struct nfp_nsp *state;
220 	int err;
221 
222 	res = nfp_resource_acquire(cpp, NFP_RESOURCE_NSP);
223 	if (IS_ERR(res))
224 		return (void *)res;
225 
226 	state = kzalloc(sizeof(*state), GFP_KERNEL);
227 	if (!state) {
228 		nfp_resource_release(res);
229 		return ERR_PTR(-ENOMEM);
230 	}
231 	state->cpp = cpp;
232 	state->res = res;
233 
234 	err = nfp_nsp_check(state);
235 	if (err) {
236 		nfp_nsp_close(state);
237 		return ERR_PTR(err);
238 	}
239 
240 	return state;
241 }
242 
243 /**
244  * nfp_nsp_close() - Clean up and unlock the NSP resource.
245  * @state:	NFP SP state
246  */
nfp_nsp_close(struct nfp_nsp * state)247 void nfp_nsp_close(struct nfp_nsp *state)
248 {
249 	nfp_resource_release(state->res);
250 	kfree(state);
251 }
252 
nfp_nsp_get_abi_ver_major(struct nfp_nsp * state)253 u16 nfp_nsp_get_abi_ver_major(struct nfp_nsp *state)
254 {
255 	return state->ver.major;
256 }
257 
nfp_nsp_get_abi_ver_minor(struct nfp_nsp * state)258 u16 nfp_nsp_get_abi_ver_minor(struct nfp_nsp *state)
259 {
260 	return state->ver.minor;
261 }
262 
263 static int
nfp_nsp_wait_reg(struct nfp_cpp * cpp,u64 * reg,u32 nsp_cpp,u64 addr,u64 mask,u64 val)264 nfp_nsp_wait_reg(struct nfp_cpp *cpp, u64 *reg,
265 		 u32 nsp_cpp, u64 addr, u64 mask, u64 val)
266 {
267 	const unsigned long wait_until = jiffies + 30 * HZ;
268 	int err;
269 
270 	for (;;) {
271 		const unsigned long start_time = jiffies;
272 
273 		err = nfp_cpp_readq(cpp, nsp_cpp, addr, reg);
274 		if (err < 0)
275 			return err;
276 
277 		if ((*reg & mask) == val)
278 			return 0;
279 
280 		msleep(25);
281 
282 		if (time_after(start_time, wait_until))
283 			return -ETIMEDOUT;
284 	}
285 }
286 
287 /**
288  * nfp_nsp_command() - Execute a command on the NFP Service Processor
289  * @state:	NFP SP state
290  * @code:	NFP SP Command Code
291  * @option:	NFP SP Command Argument
292  * @buff_cpp:	NFP SP Buffer CPP Address info
293  * @buff_addr:	NFP SP Buffer Host address
294  *
295  * Return: 0 for success with no result
296  *
297  *	 positive value for NSP completion with a result code
298  *
299  *	-EAGAIN if the NSP is not yet present
300  *	-ENODEV if the NSP is not a supported model
301  *	-EBUSY if the NSP is stuck
302  *	-EINTR if interrupted while waiting for completion
303  *	-ETIMEDOUT if the NSP took longer than 30 seconds to complete
304  */
nfp_nsp_command(struct nfp_nsp * state,u16 code,u32 option,u32 buff_cpp,u64 buff_addr)305 static int nfp_nsp_command(struct nfp_nsp *state, u16 code, u32 option,
306 			   u32 buff_cpp, u64 buff_addr)
307 {
308 	u64 reg, ret_val, nsp_base, nsp_buffer, nsp_status, nsp_command;
309 	struct nfp_cpp *cpp = state->cpp;
310 	u32 nsp_cpp;
311 	int err;
312 
313 	nsp_cpp = nfp_resource_cpp_id(state->res);
314 	nsp_base = nfp_resource_address(state->res);
315 	nsp_status = nsp_base + NSP_STATUS;
316 	nsp_command = nsp_base + NSP_COMMAND;
317 	nsp_buffer = nsp_base + NSP_BUFFER;
318 
319 	err = nfp_nsp_check(state);
320 	if (err)
321 		return err;
322 
323 	if (!FIELD_FIT(NSP_BUFFER_CPP, buff_cpp >> 8) ||
324 	    !FIELD_FIT(NSP_BUFFER_ADDRESS, buff_addr)) {
325 		nfp_err(cpp, "Host buffer out of reach %08x %016llx\n",
326 			buff_cpp, buff_addr);
327 		return -EINVAL;
328 	}
329 
330 	err = nfp_cpp_writeq(cpp, nsp_cpp, nsp_buffer,
331 			     FIELD_PREP(NSP_BUFFER_CPP, buff_cpp >> 8) |
332 			     FIELD_PREP(NSP_BUFFER_ADDRESS, buff_addr));
333 	if (err < 0)
334 		return err;
335 
336 	err = nfp_cpp_writeq(cpp, nsp_cpp, nsp_command,
337 			     FIELD_PREP(NSP_COMMAND_OPTION, option) |
338 			     FIELD_PREP(NSP_COMMAND_CODE, code) |
339 			     FIELD_PREP(NSP_COMMAND_START, 1));
340 	if (err < 0)
341 		return err;
342 
343 	/* Wait for NSP_COMMAND_START to go to 0 */
344 	err = nfp_nsp_wait_reg(cpp, &reg,
345 			       nsp_cpp, nsp_command, NSP_COMMAND_START, 0);
346 	if (err) {
347 		nfp_err(cpp, "Error %d waiting for code 0x%04x to start\n",
348 			err, code);
349 		return err;
350 	}
351 
352 	/* Wait for NSP_STATUS_BUSY to go to 0 */
353 	err = nfp_nsp_wait_reg(cpp, &reg,
354 			       nsp_cpp, nsp_status, NSP_STATUS_BUSY, 0);
355 	if (err) {
356 		nfp_err(cpp, "Error %d waiting for code 0x%04x to complete\n",
357 			err, code);
358 		return err;
359 	}
360 
361 	err = nfp_cpp_readq(cpp, nsp_cpp, nsp_command, &ret_val);
362 	if (err < 0)
363 		return err;
364 	ret_val = FIELD_GET(NSP_COMMAND_OPTION, ret_val);
365 
366 	err = FIELD_GET(NSP_STATUS_RESULT, reg);
367 	if (err) {
368 		nfp_warn(cpp, "Result (error) code set: %d (%d) command: %d\n",
369 			 -err, (int)ret_val, code);
370 		nfp_nsp_print_extended_error(state, ret_val);
371 		return -err;
372 	}
373 
374 	return ret_val;
375 }
376 
nfp_nsp_command_buf(struct nfp_nsp * nsp,u16 code,u32 option,const void * in_buf,unsigned int in_size,void * out_buf,unsigned int out_size)377 static int nfp_nsp_command_buf(struct nfp_nsp *nsp, u16 code, u32 option,
378 			       const void *in_buf, unsigned int in_size,
379 			       void *out_buf, unsigned int out_size)
380 {
381 	struct nfp_cpp *cpp = nsp->cpp;
382 	unsigned int max_size;
383 	u64 reg, cpp_buf;
384 	int ret, err;
385 	u32 cpp_id;
386 
387 	if (nsp->ver.minor < 13) {
388 		nfp_err(cpp, "NSP: Code 0x%04x with buffer not supported (ABI %hu.%hu)\n",
389 			code, nsp->ver.major, nsp->ver.minor);
390 		return -EOPNOTSUPP;
391 	}
392 
393 	err = nfp_cpp_readq(cpp, nfp_resource_cpp_id(nsp->res),
394 			    nfp_resource_address(nsp->res) +
395 			    NSP_DFLT_BUFFER_CONFIG,
396 			    &reg);
397 	if (err < 0)
398 		return err;
399 
400 	max_size = max(in_size, out_size);
401 	if (FIELD_GET(NSP_DFLT_BUFFER_SIZE_MB, reg) * SZ_1M < max_size) {
402 		nfp_err(cpp, "NSP: default buffer too small for command 0x%04x (%llu < %u)\n",
403 			code, FIELD_GET(NSP_DFLT_BUFFER_SIZE_MB, reg) * SZ_1M,
404 			max_size);
405 		return -EINVAL;
406 	}
407 
408 	err = nfp_cpp_readq(cpp, nfp_resource_cpp_id(nsp->res),
409 			    nfp_resource_address(nsp->res) +
410 			    NSP_DFLT_BUFFER,
411 			    &reg);
412 	if (err < 0)
413 		return err;
414 
415 	cpp_id = FIELD_GET(NSP_DFLT_BUFFER_CPP, reg) << 8;
416 	cpp_buf = FIELD_GET(NSP_DFLT_BUFFER_ADDRESS, reg);
417 
418 	if (in_buf && in_size) {
419 		err = nfp_cpp_write(cpp, cpp_id, cpp_buf, in_buf, in_size);
420 		if (err < 0)
421 			return err;
422 	}
423 	/* Zero out remaining part of the buffer */
424 	if (out_buf && out_size && out_size > in_size) {
425 		memset(out_buf, 0, out_size - in_size);
426 		err = nfp_cpp_write(cpp, cpp_id, cpp_buf + in_size,
427 				    out_buf, out_size - in_size);
428 		if (err < 0)
429 			return err;
430 	}
431 
432 	ret = nfp_nsp_command(nsp, code, option, cpp_id, cpp_buf);
433 	if (ret < 0)
434 		return ret;
435 
436 	if (out_buf && out_size) {
437 		err = nfp_cpp_read(cpp, cpp_id, cpp_buf, out_buf, out_size);
438 		if (err < 0)
439 			return err;
440 	}
441 
442 	return ret;
443 }
444 
nfp_nsp_wait(struct nfp_nsp * state)445 int nfp_nsp_wait(struct nfp_nsp *state)
446 {
447 	const unsigned long wait_until = jiffies + 30 * HZ;
448 	int err;
449 
450 	nfp_dbg(state->cpp, "Waiting for NSP to respond (30 sec max).\n");
451 
452 	for (;;) {
453 		const unsigned long start_time = jiffies;
454 
455 		err = nfp_nsp_command(state, SPCODE_NOOP, 0, 0, 0);
456 		if (err != -EAGAIN)
457 			break;
458 
459 		if (msleep_interruptible(25)) {
460 			err = -ERESTARTSYS;
461 			break;
462 		}
463 
464 		if (time_after(start_time, wait_until)) {
465 			err = -ETIMEDOUT;
466 			break;
467 		}
468 	}
469 	if (err)
470 		nfp_err(state->cpp, "NSP failed to respond %d\n", err);
471 
472 	return err;
473 }
474 
nfp_nsp_device_soft_reset(struct nfp_nsp * state)475 int nfp_nsp_device_soft_reset(struct nfp_nsp *state)
476 {
477 	return nfp_nsp_command(state, SPCODE_SOFT_RESET, 0, 0, 0);
478 }
479 
nfp_nsp_load_fw(struct nfp_nsp * state,const struct firmware * fw)480 int nfp_nsp_load_fw(struct nfp_nsp *state, const struct firmware *fw)
481 {
482 	return nfp_nsp_command_buf(state, SPCODE_FW_LOAD, fw->size, fw->data,
483 				   fw->size, NULL, 0);
484 }
485 
nfp_nsp_read_eth_table(struct nfp_nsp * state,void * buf,unsigned int size)486 int nfp_nsp_read_eth_table(struct nfp_nsp *state, void *buf, unsigned int size)
487 {
488 	return nfp_nsp_command_buf(state, SPCODE_ETH_RESCAN, size, NULL, 0,
489 				   buf, size);
490 }
491 
nfp_nsp_write_eth_table(struct nfp_nsp * state,const void * buf,unsigned int size)492 int nfp_nsp_write_eth_table(struct nfp_nsp *state,
493 			    const void *buf, unsigned int size)
494 {
495 	return nfp_nsp_command_buf(state, SPCODE_ETH_CONTROL, size, buf, size,
496 				   NULL, 0);
497 }
498 
nfp_nsp_read_identify(struct nfp_nsp * state,void * buf,unsigned int size)499 int nfp_nsp_read_identify(struct nfp_nsp *state, void *buf, unsigned int size)
500 {
501 	return nfp_nsp_command_buf(state, SPCODE_NSP_IDENTIFY, size, NULL, 0,
502 				   buf, size);
503 }
504 
nfp_nsp_read_sensors(struct nfp_nsp * state,unsigned int sensor_mask,void * buf,unsigned int size)505 int nfp_nsp_read_sensors(struct nfp_nsp *state, unsigned int sensor_mask,
506 			 void *buf, unsigned int size)
507 {
508 	return nfp_nsp_command_buf(state, SPCODE_NSP_SENSORS, sensor_mask,
509 				   NULL, 0, buf, size);
510 }
511