2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
36 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
37 * Jason McMullan <jason.mcmullan@netronome.com>
38 * Espen Skoglund <espen.skoglund@netronome.com>
40 #include <linux/kernel.h>
41 #include <linux/slab.h>
47 #define NFP_MIP_SIGNATURE cpu_to_le32(0x0050494d) /* "MIP\0" */
48 #define NFP_MIP_VERSION cpu_to_le32(1)
49 #define NFP_MIP_MAX_OFFSET (256 * 1024)
71 /* Read memory and check if it could be a valid MIP */
73 nfp_mip_try_read(struct nfp_cpp *cpp, u32 cpp_id, u64 addr, struct nfp_mip *mip)
77 ret = nfp_cpp_read(cpp, cpp_id, addr, mip, sizeof(*mip));
78 if (ret != sizeof(*mip)) {
79 nfp_err(cpp, "Failed to read MIP data (%d, %zu)\n",
83 if (mip->signature != NFP_MIP_SIGNATURE) {
84 nfp_warn(cpp, "Incorrect MIP signature (0x%08x)\n",
85 le32_to_cpu(mip->signature));
88 if (mip->mip_version != NFP_MIP_VERSION) {
89 nfp_warn(cpp, "Unsupported MIP version (%d)\n",
90 le32_to_cpu(mip->mip_version));
97 /* Try to locate MIP using the resource table */
98 static int nfp_mip_read_resource(struct nfp_cpp *cpp, struct nfp_mip *mip)
100 struct nfp_nffw_info *nffw_info;
105 nffw_info = nfp_nffw_info_open(cpp);
106 if (IS_ERR(nffw_info))
107 return PTR_ERR(nffw_info);
109 err = nfp_nffw_info_mip_first(nffw_info, &cpp_id, &addr);
111 goto exit_close_nffw;
113 err = nfp_mip_try_read(cpp, cpp_id, addr, mip);
115 nfp_nffw_info_close(nffw_info);
120 * nfp_mip_open() - Get device MIP structure
121 * @cpp: NFP CPP Handle
123 * Copy MIP structure from NFP device and return it. The returned
124 * structure is handled internally by the library and should be
125 * freed by calling nfp_mip_close().
127 * Return: pointer to mip, NULL on failure.
129 const struct nfp_mip *nfp_mip_open(struct nfp_cpp *cpp)
134 mip = kmalloc(sizeof(*mip), GFP_KERNEL);
138 err = nfp_mip_read_resource(cpp, mip);
144 mip->name[sizeof(mip->name) - 1] = 0;
149 void nfp_mip_close(const struct nfp_mip *mip)
154 const char *nfp_mip_name(const struct nfp_mip *mip)
160 * nfp_mip_symtab() - Get the address and size of the MIP symbol table
162 * @addr: Location for NFP DDR address of MIP symbol table
163 * @size: Location for size of MIP symbol table
165 void nfp_mip_symtab(const struct nfp_mip *mip, u32 *addr, u32 *size)
167 *addr = le32_to_cpu(mip->symtab_addr);
168 *size = le32_to_cpu(mip->symtab_size);
172 * nfp_mip_strtab() - Get the address and size of the MIP symbol name table
174 * @addr: Location for NFP DDR address of MIP symbol name table
175 * @size: Location for size of MIP symbol name table
177 void nfp_mip_strtab(const struct nfp_mip *mip, u32 *addr, u32 *size)
179 *addr = le32_to_cpu(mip->strtab_addr);
180 *size = le32_to_cpu(mip->strtab_size);