GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / net / ethernet / netronome / nfp / nfpcore / nfp_cpp.h
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_cpp.h
36  * Interface for low-level NFP CPP access.
37  * Authors: Jason McMullan <jason.mcmullan@netronome.com>
38  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
39  */
40 #ifndef __NFP_CPP_H__
41 #define __NFP_CPP_H__
42
43 #include <linux/ctype.h>
44 #include <linux/types.h>
45 #include <linux/sizes.h>
46
47 #ifndef NFP_SUBSYS
48 #define NFP_SUBSYS "nfp"
49 #endif
50
51 #define nfp_err(cpp, fmt, args...) \
52         dev_err(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
53 #define nfp_warn(cpp, fmt, args...) \
54         dev_warn(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
55 #define nfp_info(cpp, fmt, args...) \
56         dev_info(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
57 #define nfp_dbg(cpp, fmt, args...) \
58         dev_dbg(nfp_cpp_device(cpp)->parent, NFP_SUBSYS ": " fmt, ## args)
59
60 #define PCI_64BIT_BAR_COUNT             3
61
62 #define NFP_CPP_NUM_TARGETS             16
63 /* Max size of area it should be safe to request */
64 #define NFP_CPP_SAFE_AREA_SIZE          SZ_2M
65
66 /* NFP_MUTEX_WAIT_* are timeouts in seconds when waiting for a mutex */
67 #define NFP_MUTEX_WAIT_FIRST_WARN       15
68 #define NFP_MUTEX_WAIT_NEXT_WARN        5
69 #define NFP_MUTEX_WAIT_ERROR            60
70
71 struct device;
72
73 struct nfp_cpp_area;
74 struct nfp_cpp;
75 struct resource;
76
77 /* Wildcard indicating a CPP read or write action
78  *
79  * The action used will be either read or write depending on whether a
80  * read or write instruction/call is performed on the NFP_CPP_ID.  It
81  * is recomended that the RW action is used even if all actions to be
82  * performed on a NFP_CPP_ID are known to be only reads or writes.
83  * Doing so will in many cases save NFP CPP internal software
84  * resources.
85  */
86 #define NFP_CPP_ACTION_RW               32
87
88 #define NFP_CPP_TARGET_ID_MASK          0x1f
89
90 /**
91  * NFP_CPP_ID() - pack target, token, and action into a CPP ID.
92  * @target:     NFP CPP target id
93  * @action:     NFP CPP action id
94  * @token:      NFP CPP token id
95  *
96  * Create a 32-bit CPP identifier representing the access to be made.
97  * These identifiers are used as parameters to other NFP CPP
98  * functions.  Some CPP devices may allow wildcard identifiers to be
99  * specified.
100  *
101  * Return:      NFP CPP ID
102  */
103 #define NFP_CPP_ID(target, action, token)                        \
104         ((((target) & 0x7f) << 24) | (((token)  & 0xff) << 16) | \
105          (((action) & 0xff) <<  8))
106
107 /**
108  * NFP_CPP_ISLAND_ID() - pack target, token, action, and island into a CPP ID.
109  * @target:     NFP CPP target id
110  * @action:     NFP CPP action id
111  * @token:      NFP CPP token id
112  * @island:     NFP CPP island id
113  *
114  * Create a 32-bit CPP identifier representing the access to be made.
115  * These identifiers are used as parameters to other NFP CPP
116  * functions.  Some CPP devices may allow wildcard identifiers to be
117  * specified.
118  *
119  * Return:      NFP CPP ID
120  */
121 #define NFP_CPP_ISLAND_ID(target, action, token, island)         \
122         ((((target) & 0x7f) << 24) | (((token)  & 0xff) << 16) | \
123          (((action) & 0xff) <<  8) | (((island) & 0xff) << 0))
124
125 /**
126  * NFP_CPP_ID_TARGET_of() - Return the NFP CPP target of a NFP CPP ID
127  * @id:         NFP CPP ID
128  *
129  * Return:      NFP CPP target
130  */
131 static inline u8 NFP_CPP_ID_TARGET_of(u32 id)
132 {
133         return (id >> 24) & NFP_CPP_TARGET_ID_MASK;
134 }
135
136 /**
137  * NFP_CPP_ID_TOKEN_of() - Return the NFP CPP token of a NFP CPP ID
138  * @id:         NFP CPP ID
139  * Return:      NFP CPP token
140  */
141 static inline u8 NFP_CPP_ID_TOKEN_of(u32 id)
142 {
143         return (id >> 16) & 0xff;
144 }
145
146 /**
147  * NFP_CPP_ID_ACTION_of() - Return the NFP CPP action of a NFP CPP ID
148  * @id:         NFP CPP ID
149  *
150  * Return:      NFP CPP action
151  */
152 static inline u8 NFP_CPP_ID_ACTION_of(u32 id)
153 {
154         return (id >> 8) & 0xff;
155 }
156
157 /**
158  * NFP_CPP_ID_ISLAND_of() - Return the NFP CPP island of a NFP CPP ID
159  * @id: NFP CPP ID
160  *
161  * Return:      NFP CPP island
162  */
163 static inline u8 NFP_CPP_ID_ISLAND_of(u32 id)
164 {
165         return (id >> 0) & 0xff;
166 }
167
168 /* NFP Interface types - logical interface for this CPP connection
169  * 4 bits are reserved for interface type.
170  */
171 #define NFP_CPP_INTERFACE_TYPE_INVALID      0x0
172 #define NFP_CPP_INTERFACE_TYPE_PCI          0x1
173 #define NFP_CPP_INTERFACE_TYPE_ARM          0x2
174 #define NFP_CPP_INTERFACE_TYPE_RPC          0x3
175 #define NFP_CPP_INTERFACE_TYPE_ILA          0x4
176
177 /**
178  * NFP_CPP_INTERFACE() - Construct a 16-bit NFP Interface ID
179  * @type:       NFP Interface Type
180  * @unit:       Unit identifier for the interface type
181  * @channel:    Channel identifier for the interface unit
182  *
183  * Interface IDs consists of 4 bits of interface type,
184  * 4 bits of unit identifier, and 8 bits of channel identifier.
185  *
186  * The NFP Interface ID is used in the implementation of
187  * NFP CPP API mutexes, which use the MU Atomic CompareAndWrite
188  * operation - hence the limit to 16 bits to be able to
189  * use the NFP Interface ID as a lock owner.
190  *
191  * Return:      Interface ID
192  */
193 #define NFP_CPP_INTERFACE(type, unit, channel)  \
194         ((((type) & 0xf) << 12) |               \
195          (((unit) & 0xf) <<  8) |               \
196          (((channel) & 0xff) << 0))
197
198 /**
199  * NFP_CPP_INTERFACE_TYPE_of() - Get the interface type
200  * @interface:  NFP Interface ID
201  * Return:      NFP Interface ID's type
202  */
203 #define NFP_CPP_INTERFACE_TYPE_of(interface)   (((interface) >> 12) & 0xf)
204
205 /**
206  * NFP_CPP_INTERFACE_UNIT_of() - Get the interface unit
207  * @interface:  NFP Interface ID
208  * Return:      NFP Interface ID's unit
209  */
210 #define NFP_CPP_INTERFACE_UNIT_of(interface)   (((interface) >>  8) & 0xf)
211
212 /**
213  * NFP_CPP_INTERFACE_CHANNEL_of() - Get the interface channel
214  * @interface:  NFP Interface ID
215  * Return:      NFP Interface ID's channel
216  */
217 #define NFP_CPP_INTERFACE_CHANNEL_of(interface)   (((interface) >>  0) & 0xff)
218
219 /* Implemented in nfp_cppcore.c */
220 void nfp_cpp_free(struct nfp_cpp *cpp);
221 u32 nfp_cpp_model(struct nfp_cpp *cpp);
222 u16 nfp_cpp_interface(struct nfp_cpp *cpp);
223 int nfp_cpp_serial(struct nfp_cpp *cpp, const u8 **serial);
224
225 struct nfp_cpp_area *nfp_cpp_area_alloc_with_name(struct nfp_cpp *cpp,
226                                                   u32 cpp_id,
227                                                   const char *name,
228                                                   unsigned long long address,
229                                                   unsigned long size);
230 struct nfp_cpp_area *nfp_cpp_area_alloc(struct nfp_cpp *cpp, u32 cpp_id,
231                                         unsigned long long address,
232                                         unsigned long size);
233 struct nfp_cpp_area *
234 nfp_cpp_area_alloc_acquire(struct nfp_cpp *cpp, const char *name, u32 cpp_id,
235                            unsigned long long address, unsigned long size);
236 void nfp_cpp_area_free(struct nfp_cpp_area *area);
237 int nfp_cpp_area_acquire(struct nfp_cpp_area *area);
238 int nfp_cpp_area_acquire_nonblocking(struct nfp_cpp_area *area);
239 void nfp_cpp_area_release(struct nfp_cpp_area *area);
240 void nfp_cpp_area_release_free(struct nfp_cpp_area *area);
241 int nfp_cpp_area_read(struct nfp_cpp_area *area, unsigned long offset,
242                       void *buffer, size_t length);
243 int nfp_cpp_area_write(struct nfp_cpp_area *area, unsigned long offset,
244                        const void *buffer, size_t length);
245 const char *nfp_cpp_area_name(struct nfp_cpp_area *cpp_area);
246 void *nfp_cpp_area_priv(struct nfp_cpp_area *cpp_area);
247 struct nfp_cpp *nfp_cpp_area_cpp(struct nfp_cpp_area *cpp_area);
248 struct resource *nfp_cpp_area_resource(struct nfp_cpp_area *area);
249 phys_addr_t nfp_cpp_area_phys(struct nfp_cpp_area *area);
250 void __iomem *nfp_cpp_area_iomem(struct nfp_cpp_area *area);
251
252 int nfp_cpp_area_readl(struct nfp_cpp_area *area, unsigned long offset,
253                        u32 *value);
254 int nfp_cpp_area_writel(struct nfp_cpp_area *area, unsigned long offset,
255                         u32 value);
256 int nfp_cpp_area_readq(struct nfp_cpp_area *area, unsigned long offset,
257                        u64 *value);
258 int nfp_cpp_area_writeq(struct nfp_cpp_area *area, unsigned long offset,
259                         u64 value);
260 int nfp_cpp_area_fill(struct nfp_cpp_area *area, unsigned long offset,
261                       u32 value, size_t length);
262
263 int nfp_xpb_readl(struct nfp_cpp *cpp, u32 xpb_tgt, u32 *value);
264 int nfp_xpb_writel(struct nfp_cpp *cpp, u32 xpb_tgt, u32 value);
265 int nfp_xpb_writelm(struct nfp_cpp *cpp, u32 xpb_tgt, u32 mask, u32 value);
266
267 /* Implemented in nfp_cpplib.c */
268 int nfp_cpp_read(struct nfp_cpp *cpp, u32 cpp_id,
269                  unsigned long long address, void *kernel_vaddr, size_t length);
270 int nfp_cpp_write(struct nfp_cpp *cpp, u32 cpp_id,
271                   unsigned long long address, const void *kernel_vaddr,
272                   size_t length);
273 int nfp_cpp_readl(struct nfp_cpp *cpp, u32 cpp_id,
274                   unsigned long long address, u32 *value);
275 int nfp_cpp_writel(struct nfp_cpp *cpp, u32 cpp_id,
276                    unsigned long long address, u32 value);
277 int nfp_cpp_readq(struct nfp_cpp *cpp, u32 cpp_id,
278                   unsigned long long address, u64 *value);
279 int nfp_cpp_writeq(struct nfp_cpp *cpp, u32 cpp_id,
280                    unsigned long long address, u64 value);
281
282 u8 __iomem *
283 nfp_cpp_map_area(struct nfp_cpp *cpp, const char *name, int domain, int target,
284                  u64 addr, unsigned long size, struct nfp_cpp_area **area);
285
286 struct nfp_cpp_mutex;
287
288 int nfp_cpp_mutex_init(struct nfp_cpp *cpp, int target,
289                        unsigned long long address, u32 key_id);
290 struct nfp_cpp_mutex *nfp_cpp_mutex_alloc(struct nfp_cpp *cpp, int target,
291                                           unsigned long long address,
292                                           u32 key_id);
293 void nfp_cpp_mutex_free(struct nfp_cpp_mutex *mutex);
294 int nfp_cpp_mutex_lock(struct nfp_cpp_mutex *mutex);
295 int nfp_cpp_mutex_unlock(struct nfp_cpp_mutex *mutex);
296 int nfp_cpp_mutex_trylock(struct nfp_cpp_mutex *mutex);
297
298 /**
299  * nfp_cppcore_pcie_unit() - Get PCI Unit of a CPP handle
300  * @cpp:        CPP handle
301  *
302  * Return: PCI unit for the NFP CPP handle
303  */
304 static inline u8 nfp_cppcore_pcie_unit(struct nfp_cpp *cpp)
305 {
306         return NFP_CPP_INTERFACE_UNIT_of(nfp_cpp_interface(cpp));
307 }
308
309 struct nfp_cpp_explicit;
310
311 struct nfp_cpp_explicit_command {
312         u32 cpp_id;
313         u16 data_ref;
314         u8  data_master;
315         u8  len;
316         u8  byte_mask;
317         u8  signal_master;
318         u8  signal_ref;
319         u8  posted;
320         u8  siga;
321         u8  sigb;
322         s8   siga_mode;
323         s8   sigb_mode;
324 };
325
326 #define NFP_SERIAL_LEN          6
327
328 /**
329  * struct nfp_cpp_operations - NFP CPP operations structure
330  * @area_priv_size:     Size of the nfp_cpp_area private data
331  * @owner:              Owner module
332  * @init:               Initialize the NFP CPP bus
333  * @free:               Free the bus
334  * @read_serial:        Read serial number to memory provided
335  * @get_interface:      Return CPP interface
336  * @area_init:          Initialize a new NFP CPP area (not serialized)
337  * @area_cleanup:       Clean up a NFP CPP area (not serialized)
338  * @area_acquire:       Acquire the NFP CPP area (serialized)
339  * @area_release:       Release area (serialized)
340  * @area_resource:      Get resource range of area (not serialized)
341  * @area_phys:          Get physical address of area (not serialized)
342  * @area_iomem:         Get iomem of area (not serialized)
343  * @area_read:          Perform a read from a NFP CPP area (serialized)
344  * @area_write:         Perform a write to a NFP CPP area (serialized)
345  * @explicit_priv_size: Size of an explicit's private area
346  * @explicit_acquire:   Acquire an explicit area
347  * @explicit_release:   Release an explicit area
348  * @explicit_put:       Write data to send
349  * @explicit_get:       Read data received
350  * @explicit_do:        Perform the transaction
351  */
352 struct nfp_cpp_operations {
353         size_t area_priv_size;
354         struct module *owner;
355
356         int (*init)(struct nfp_cpp *cpp);
357         void (*free)(struct nfp_cpp *cpp);
358
359         void (*read_serial)(struct device *dev, u8 *serial);
360         u16 (*get_interface)(struct device *dev);
361
362         int (*area_init)(struct nfp_cpp_area *area,
363                          u32 dest, unsigned long long address,
364                          unsigned long size);
365         void (*area_cleanup)(struct nfp_cpp_area *area);
366         int (*area_acquire)(struct nfp_cpp_area *area);
367         void (*area_release)(struct nfp_cpp_area *area);
368         struct resource *(*area_resource)(struct nfp_cpp_area *area);
369         phys_addr_t (*area_phys)(struct nfp_cpp_area *area);
370         void __iomem *(*area_iomem)(struct nfp_cpp_area *area);
371         int (*area_read)(struct nfp_cpp_area *area, void *kernel_vaddr,
372                          unsigned long offset, unsigned int length);
373         int (*area_write)(struct nfp_cpp_area *area, const void *kernel_vaddr,
374                           unsigned long offset, unsigned int length);
375
376         size_t explicit_priv_size;
377         int (*explicit_acquire)(struct nfp_cpp_explicit *expl);
378         void (*explicit_release)(struct nfp_cpp_explicit *expl);
379         int (*explicit_put)(struct nfp_cpp_explicit *expl,
380                             const void *buff, size_t len);
381         int (*explicit_get)(struct nfp_cpp_explicit *expl,
382                             void *buff, size_t len);
383         int (*explicit_do)(struct nfp_cpp_explicit *expl,
384                            const struct nfp_cpp_explicit_command *cmd,
385                            u64 address);
386 };
387
388 struct nfp_cpp *
389 nfp_cpp_from_operations(const struct nfp_cpp_operations *ops,
390                         struct device *parent, void *priv);
391 void *nfp_cpp_priv(struct nfp_cpp *priv);
392
393 int nfp_cpp_area_cache_add(struct nfp_cpp *cpp, size_t size);
394
395 /* The following section contains extensions to the
396  * NFP CPP API, to be used in a Linux kernel-space context.
397  */
398
399 /* Use this channel ID for multiple virtual channel interfaces
400  * (ie ARM and PCIe) when setting up the interface field.
401  */
402 #define NFP_CPP_INTERFACE_CHANNEL_PEROPENER     255
403 struct device *nfp_cpp_device(struct nfp_cpp *cpp);
404
405 /* Return code masks for nfp_cpp_explicit_do()
406  */
407 #define NFP_SIGNAL_MASK_A       BIT(0)  /* Signal A fired */
408 #define NFP_SIGNAL_MASK_B       BIT(1)  /* Signal B fired */
409
410 enum nfp_cpp_explicit_signal_mode {
411         NFP_SIGNAL_NONE = 0,
412         NFP_SIGNAL_PUSH = 1,
413         NFP_SIGNAL_PUSH_OPTIONAL = -1,
414         NFP_SIGNAL_PULL = 2,
415         NFP_SIGNAL_PULL_OPTIONAL = -2,
416 };
417
418 struct nfp_cpp_explicit *nfp_cpp_explicit_acquire(struct nfp_cpp *cpp);
419 int nfp_cpp_explicit_set_target(struct nfp_cpp_explicit *expl, u32 cpp_id,
420                                 u8 len, u8 mask);
421 int nfp_cpp_explicit_set_data(struct nfp_cpp_explicit *expl,
422                               u8 data_master, u16 data_ref);
423 int nfp_cpp_explicit_set_signal(struct nfp_cpp_explicit *expl,
424                                 u8 signal_master, u8 signal_ref);
425 int nfp_cpp_explicit_set_posted(struct nfp_cpp_explicit *expl, int posted,
426                                 u8 siga,
427                                 enum nfp_cpp_explicit_signal_mode siga_mode,
428                                 u8 sigb,
429                                 enum nfp_cpp_explicit_signal_mode sigb_mode);
430 int nfp_cpp_explicit_put(struct nfp_cpp_explicit *expl,
431                          const void *buff, size_t len);
432 int nfp_cpp_explicit_do(struct nfp_cpp_explicit *expl, u64 address);
433 int nfp_cpp_explicit_get(struct nfp_cpp_explicit *expl, void *buff, size_t len);
434 void nfp_cpp_explicit_release(struct nfp_cpp_explicit *expl);
435 struct nfp_cpp *nfp_cpp_explicit_cpp(struct nfp_cpp_explicit *expl);
436 void *nfp_cpp_explicit_priv(struct nfp_cpp_explicit *cpp_explicit);
437
438 /* Implemented in nfp_cpplib.c */
439
440 int nfp_cpp_model_autodetect(struct nfp_cpp *cpp, u32 *model);
441
442 int nfp_cpp_explicit_read(struct nfp_cpp *cpp, u32 cpp_id,
443                           u64 addr, void *buff, size_t len,
444                           int width_read);
445
446 int nfp_cpp_explicit_write(struct nfp_cpp *cpp, u32 cpp_id,
447                            u64 addr, const void *buff, size_t len,
448                            int width_write);
449
450 #endif /* !__NFP_CPP_H__ */