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