failover: allow name change on IFF_UP slave interfaces
[sfrench/cifs-2.6.git] / include / linux / ntb.h
1 /*
2  * This file is provided under a dual BSD/GPLv2 license.  When using or
3  *   redistributing this file, you may do so under either license.
4  *
5  *   GPL LICENSE SUMMARY
6  *
7  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
8  *   Copyright (C) 2016 T-Platforms. All Rights Reserved.
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of version 2 of the GNU General Public License as
12  *   published by the Free Software Foundation.
13  *
14  *   This program is distributed in the hope that it will be useful, but
15  *   WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *   General Public License for more details.
18  *
19  *   BSD LICENSE
20  *
21  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
22  *   Copyright (C) 2016 T-Platforms. All Rights Reserved.
23  *
24  *   Redistribution and use in source and binary forms, with or without
25  *   modification, are permitted provided that the following conditions
26  *   are met:
27  *
28  *     * Redistributions of source code must retain the above copyright
29  *       notice, this list of conditions and the following disclaimer.
30  *     * Redistributions in binary form must reproduce the above copy
31  *       notice, this list of conditions and the following disclaimer in
32  *       the documentation and/or other materials provided with the
33  *       distribution.
34  *     * Neither the name of Intel Corporation nor the names of its
35  *       contributors may be used to endorse or promote products derived
36  *       from this software without specific prior written permission.
37  *
38  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
41  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
42  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49  *
50  * PCIe NTB Linux driver
51  *
52  * Contact Information:
53  * Allen Hubbe <Allen.Hubbe@emc.com>
54  */
55
56 #ifndef _NTB_H_
57 #define _NTB_H_
58
59 #include <linux/completion.h>
60 #include <linux/device.h>
61
62 struct ntb_client;
63 struct ntb_dev;
64 struct pci_dev;
65
66 /**
67  * enum ntb_topo - NTB connection topology
68  * @NTB_TOPO_NONE:      Topology is unknown or invalid.
69  * @NTB_TOPO_PRI:       On primary side of local ntb.
70  * @NTB_TOPO_SEC:       On secondary side of remote ntb.
71  * @NTB_TOPO_B2B_USD:   On primary side of local ntb upstream of remote ntb.
72  * @NTB_TOPO_B2B_DSD:   On primary side of local ntb downstream of remote ntb.
73  * @NTB_TOPO_SWITCH:    Connected via a switch which supports ntb.
74  * @NTB_TOPO_CROSSLINK: Connected via two symmetric switchecs
75  */
76 enum ntb_topo {
77         NTB_TOPO_NONE = -1,
78         NTB_TOPO_PRI,
79         NTB_TOPO_SEC,
80         NTB_TOPO_B2B_USD,
81         NTB_TOPO_B2B_DSD,
82         NTB_TOPO_SWITCH,
83         NTB_TOPO_CROSSLINK,
84 };
85
86 static inline int ntb_topo_is_b2b(enum ntb_topo topo)
87 {
88         switch ((int)topo) {
89         case NTB_TOPO_B2B_USD:
90         case NTB_TOPO_B2B_DSD:
91                 return 1;
92         }
93         return 0;
94 }
95
96 static inline char *ntb_topo_string(enum ntb_topo topo)
97 {
98         switch (topo) {
99         case NTB_TOPO_NONE:             return "NTB_TOPO_NONE";
100         case NTB_TOPO_PRI:              return "NTB_TOPO_PRI";
101         case NTB_TOPO_SEC:              return "NTB_TOPO_SEC";
102         case NTB_TOPO_B2B_USD:          return "NTB_TOPO_B2B_USD";
103         case NTB_TOPO_B2B_DSD:          return "NTB_TOPO_B2B_DSD";
104         case NTB_TOPO_SWITCH:           return "NTB_TOPO_SWITCH";
105         case NTB_TOPO_CROSSLINK:        return "NTB_TOPO_CROSSLINK";
106         }
107         return "NTB_TOPO_INVALID";
108 }
109
110 /**
111  * enum ntb_speed - NTB link training speed
112  * @NTB_SPEED_AUTO:     Request the max supported speed.
113  * @NTB_SPEED_NONE:     Link is not trained to any speed.
114  * @NTB_SPEED_GEN1:     Link is trained to gen1 speed.
115  * @NTB_SPEED_GEN2:     Link is trained to gen2 speed.
116  * @NTB_SPEED_GEN3:     Link is trained to gen3 speed.
117  * @NTB_SPEED_GEN4:     Link is trained to gen4 speed.
118  */
119 enum ntb_speed {
120         NTB_SPEED_AUTO = -1,
121         NTB_SPEED_NONE = 0,
122         NTB_SPEED_GEN1 = 1,
123         NTB_SPEED_GEN2 = 2,
124         NTB_SPEED_GEN3 = 3,
125         NTB_SPEED_GEN4 = 4
126 };
127
128 /**
129  * enum ntb_width - NTB link training width
130  * @NTB_WIDTH_AUTO:     Request the max supported width.
131  * @NTB_WIDTH_NONE:     Link is not trained to any width.
132  * @NTB_WIDTH_1:        Link is trained to 1 lane width.
133  * @NTB_WIDTH_2:        Link is trained to 2 lane width.
134  * @NTB_WIDTH_4:        Link is trained to 4 lane width.
135  * @NTB_WIDTH_8:        Link is trained to 8 lane width.
136  * @NTB_WIDTH_12:       Link is trained to 12 lane width.
137  * @NTB_WIDTH_16:       Link is trained to 16 lane width.
138  * @NTB_WIDTH_32:       Link is trained to 32 lane width.
139  */
140 enum ntb_width {
141         NTB_WIDTH_AUTO = -1,
142         NTB_WIDTH_NONE = 0,
143         NTB_WIDTH_1 = 1,
144         NTB_WIDTH_2 = 2,
145         NTB_WIDTH_4 = 4,
146         NTB_WIDTH_8 = 8,
147         NTB_WIDTH_12 = 12,
148         NTB_WIDTH_16 = 16,
149         NTB_WIDTH_32 = 32,
150 };
151
152 /**
153  * enum ntb_default_port - NTB default port number
154  * @NTB_PORT_PRI_USD:   Default port of the NTB_TOPO_PRI/NTB_TOPO_B2B_USD
155  *                      topologies
156  * @NTB_PORT_SEC_DSD:   Default port of the NTB_TOPO_SEC/NTB_TOPO_B2B_DSD
157  *                      topologies
158  */
159 enum ntb_default_port {
160         NTB_PORT_PRI_USD,
161         NTB_PORT_SEC_DSD
162 };
163 #define NTB_DEF_PEER_CNT        (1)
164 #define NTB_DEF_PEER_IDX        (0)
165
166 /**
167  * struct ntb_client_ops - ntb client operations
168  * @probe:              Notify client of a new device.
169  * @remove:             Notify client to remove a device.
170  */
171 struct ntb_client_ops {
172         int (*probe)(struct ntb_client *client, struct ntb_dev *ntb);
173         void (*remove)(struct ntb_client *client, struct ntb_dev *ntb);
174 };
175
176 static inline int ntb_client_ops_is_valid(const struct ntb_client_ops *ops)
177 {
178         /* commented callbacks are not required: */
179         return
180                 ops->probe                      &&
181                 ops->remove                     &&
182                 1;
183 }
184
185 /**
186  * struct ntb_ctx_ops - ntb driver context operations
187  * @link_event:         See ntb_link_event().
188  * @db_event:           See ntb_db_event().
189  * @msg_event:          See ntb_msg_event().
190  */
191 struct ntb_ctx_ops {
192         void (*link_event)(void *ctx);
193         void (*db_event)(void *ctx, int db_vector);
194         void (*msg_event)(void *ctx);
195 };
196
197 static inline int ntb_ctx_ops_is_valid(const struct ntb_ctx_ops *ops)
198 {
199         /* commented callbacks are not required: */
200         return
201                 /* ops->link_event              && */
202                 /* ops->db_event                && */
203                 /* ops->msg_event               && */
204                 1;
205 }
206
207 /**
208  * struct ntb_ctx_ops - ntb device operations
209  * @port_number:        See ntb_port_number().
210  * @peer_port_count:    See ntb_peer_port_count().
211  * @peer_port_number:   See ntb_peer_port_number().
212  * @peer_port_idx:      See ntb_peer_port_idx().
213  * @link_is_up:         See ntb_link_is_up().
214  * @link_enable:        See ntb_link_enable().
215  * @link_disable:       See ntb_link_disable().
216  * @mw_count:           See ntb_mw_count().
217  * @mw_get_align:       See ntb_mw_get_align().
218  * @mw_set_trans:       See ntb_mw_set_trans().
219  * @mw_clear_trans:     See ntb_mw_clear_trans().
220  * @peer_mw_count:      See ntb_peer_mw_count().
221  * @peer_mw_get_addr:   See ntb_peer_mw_get_addr().
222  * @peer_mw_set_trans:  See ntb_peer_mw_set_trans().
223  * @peer_mw_clear_trans:See ntb_peer_mw_clear_trans().
224  * @db_is_unsafe:       See ntb_db_is_unsafe().
225  * @db_valid_mask:      See ntb_db_valid_mask().
226  * @db_vector_count:    See ntb_db_vector_count().
227  * @db_vector_mask:     See ntb_db_vector_mask().
228  * @db_read:            See ntb_db_read().
229  * @db_set:             See ntb_db_set().
230  * @db_clear:           See ntb_db_clear().
231  * @db_read_mask:       See ntb_db_read_mask().
232  * @db_set_mask:        See ntb_db_set_mask().
233  * @db_clear_mask:      See ntb_db_clear_mask().
234  * @peer_db_addr:       See ntb_peer_db_addr().
235  * @peer_db_read:       See ntb_peer_db_read().
236  * @peer_db_set:        See ntb_peer_db_set().
237  * @peer_db_clear:      See ntb_peer_db_clear().
238  * @peer_db_read_mask:  See ntb_peer_db_read_mask().
239  * @peer_db_set_mask:   See ntb_peer_db_set_mask().
240  * @peer_db_clear_mask: See ntb_peer_db_clear_mask().
241  * @spad_is_unsafe:     See ntb_spad_is_unsafe().
242  * @spad_count:         See ntb_spad_count().
243  * @spad_read:          See ntb_spad_read().
244  * @spad_write:         See ntb_spad_write().
245  * @peer_spad_addr:     See ntb_peer_spad_addr().
246  * @peer_spad_read:     See ntb_peer_spad_read().
247  * @peer_spad_write:    See ntb_peer_spad_write().
248  * @msg_count:          See ntb_msg_count().
249  * @msg_inbits:         See ntb_msg_inbits().
250  * @msg_outbits:        See ntb_msg_outbits().
251  * @msg_read_sts:       See ntb_msg_read_sts().
252  * @msg_clear_sts:      See ntb_msg_clear_sts().
253  * @msg_set_mask:       See ntb_msg_set_mask().
254  * @msg_clear_mask:     See ntb_msg_clear_mask().
255  * @msg_read:           See ntb_msg_read().
256  * @peer_msg_write:     See ntb_peer_msg_write().
257  */
258 struct ntb_dev_ops {
259         int (*port_number)(struct ntb_dev *ntb);
260         int (*peer_port_count)(struct ntb_dev *ntb);
261         int (*peer_port_number)(struct ntb_dev *ntb, int pidx);
262         int (*peer_port_idx)(struct ntb_dev *ntb, int port);
263
264         u64 (*link_is_up)(struct ntb_dev *ntb,
265                           enum ntb_speed *speed, enum ntb_width *width);
266         int (*link_enable)(struct ntb_dev *ntb,
267                            enum ntb_speed max_speed, enum ntb_width max_width);
268         int (*link_disable)(struct ntb_dev *ntb);
269
270         int (*mw_count)(struct ntb_dev *ntb, int pidx);
271         int (*mw_get_align)(struct ntb_dev *ntb, int pidx, int widx,
272                             resource_size_t *addr_align,
273                             resource_size_t *size_align,
274                             resource_size_t *size_max);
275         int (*mw_set_trans)(struct ntb_dev *ntb, int pidx, int widx,
276                             dma_addr_t addr, resource_size_t size);
277         int (*mw_clear_trans)(struct ntb_dev *ntb, int pidx, int widx);
278         int (*peer_mw_count)(struct ntb_dev *ntb);
279         int (*peer_mw_get_addr)(struct ntb_dev *ntb, int widx,
280                                 phys_addr_t *base, resource_size_t *size);
281         int (*peer_mw_set_trans)(struct ntb_dev *ntb, int pidx, int widx,
282                                  u64 addr, resource_size_t size);
283         int (*peer_mw_clear_trans)(struct ntb_dev *ntb, int pidx, int widx);
284
285         int (*db_is_unsafe)(struct ntb_dev *ntb);
286         u64 (*db_valid_mask)(struct ntb_dev *ntb);
287         int (*db_vector_count)(struct ntb_dev *ntb);
288         u64 (*db_vector_mask)(struct ntb_dev *ntb, int db_vector);
289
290         u64 (*db_read)(struct ntb_dev *ntb);
291         int (*db_set)(struct ntb_dev *ntb, u64 db_bits);
292         int (*db_clear)(struct ntb_dev *ntb, u64 db_bits);
293
294         u64 (*db_read_mask)(struct ntb_dev *ntb);
295         int (*db_set_mask)(struct ntb_dev *ntb, u64 db_bits);
296         int (*db_clear_mask)(struct ntb_dev *ntb, u64 db_bits);
297
298         int (*peer_db_addr)(struct ntb_dev *ntb,
299                             phys_addr_t *db_addr, resource_size_t *db_size,
300                                 u64 *db_data, int db_bit);
301         u64 (*peer_db_read)(struct ntb_dev *ntb);
302         int (*peer_db_set)(struct ntb_dev *ntb, u64 db_bits);
303         int (*peer_db_clear)(struct ntb_dev *ntb, u64 db_bits);
304
305         u64 (*peer_db_read_mask)(struct ntb_dev *ntb);
306         int (*peer_db_set_mask)(struct ntb_dev *ntb, u64 db_bits);
307         int (*peer_db_clear_mask)(struct ntb_dev *ntb, u64 db_bits);
308
309         int (*spad_is_unsafe)(struct ntb_dev *ntb);
310         int (*spad_count)(struct ntb_dev *ntb);
311
312         u32 (*spad_read)(struct ntb_dev *ntb, int sidx);
313         int (*spad_write)(struct ntb_dev *ntb, int sidx, u32 val);
314
315         int (*peer_spad_addr)(struct ntb_dev *ntb, int pidx, int sidx,
316                               phys_addr_t *spad_addr);
317         u32 (*peer_spad_read)(struct ntb_dev *ntb, int pidx, int sidx);
318         int (*peer_spad_write)(struct ntb_dev *ntb, int pidx, int sidx,
319                                u32 val);
320
321         int (*msg_count)(struct ntb_dev *ntb);
322         u64 (*msg_inbits)(struct ntb_dev *ntb);
323         u64 (*msg_outbits)(struct ntb_dev *ntb);
324         u64 (*msg_read_sts)(struct ntb_dev *ntb);
325         int (*msg_clear_sts)(struct ntb_dev *ntb, u64 sts_bits);
326         int (*msg_set_mask)(struct ntb_dev *ntb, u64 mask_bits);
327         int (*msg_clear_mask)(struct ntb_dev *ntb, u64 mask_bits);
328         u32 (*msg_read)(struct ntb_dev *ntb, int *pidx, int midx);
329         int (*peer_msg_write)(struct ntb_dev *ntb, int pidx, int midx, u32 msg);
330 };
331
332 static inline int ntb_dev_ops_is_valid(const struct ntb_dev_ops *ops)
333 {
334         /* commented callbacks are not required: */
335         return
336                 /* Port operations are required for multiport devices */
337                 !ops->peer_port_count == !ops->port_number      &&
338                 !ops->peer_port_number == !ops->port_number     &&
339                 !ops->peer_port_idx == !ops->port_number        &&
340
341                 /* Link operations are required */
342                 ops->link_is_up                         &&
343                 ops->link_enable                        &&
344                 ops->link_disable                       &&
345
346                 /* One or both MW interfaces should be developed */
347                 ops->mw_count                           &&
348                 ops->mw_get_align                       &&
349                 (ops->mw_set_trans                      ||
350                  ops->peer_mw_set_trans)                &&
351                 /* ops->mw_clear_trans                  && */
352                 ops->peer_mw_count                      &&
353                 ops->peer_mw_get_addr                   &&
354                 /* ops->peer_mw_clear_trans             && */
355
356                 /* Doorbell operations are mostly required */
357                 /* ops->db_is_unsafe                    && */
358                 ops->db_valid_mask                      &&
359                 /* both set, or both unset */
360                 (!ops->db_vector_count == !ops->db_vector_mask) &&
361                 ops->db_read                            &&
362                 /* ops->db_set                          && */
363                 ops->db_clear                           &&
364                 /* ops->db_read_mask                    && */
365                 ops->db_set_mask                        &&
366                 ops->db_clear_mask                      &&
367                 /* ops->peer_db_addr                    && */
368                 /* ops->peer_db_read                    && */
369                 ops->peer_db_set                        &&
370                 /* ops->peer_db_clear                   && */
371                 /* ops->peer_db_read_mask               && */
372                 /* ops->peer_db_set_mask                && */
373                 /* ops->peer_db_clear_mask              && */
374
375                 /* Scrachpads interface is optional */
376                 /* !ops->spad_is_unsafe == !ops->spad_count     && */
377                 !ops->spad_read == !ops->spad_count             &&
378                 !ops->spad_write == !ops->spad_count            &&
379                 /* !ops->peer_spad_addr == !ops->spad_count     && */
380                 /* !ops->peer_spad_read == !ops->spad_count     && */
381                 !ops->peer_spad_write == !ops->spad_count       &&
382
383                 /* Messaging interface is optional */
384                 !ops->msg_inbits == !ops->msg_count             &&
385                 !ops->msg_outbits == !ops->msg_count            &&
386                 !ops->msg_read_sts == !ops->msg_count           &&
387                 !ops->msg_clear_sts == !ops->msg_count          &&
388                 /* !ops->msg_set_mask == !ops->msg_count        && */
389                 /* !ops->msg_clear_mask == !ops->msg_count      && */
390                 !ops->msg_read == !ops->msg_count               &&
391                 !ops->peer_msg_write == !ops->msg_count         &&
392                 1;
393 }
394
395 /**
396  * struct ntb_client - client interested in ntb devices
397  * @drv:                Linux driver object.
398  * @ops:                See &ntb_client_ops.
399  */
400 struct ntb_client {
401         struct device_driver            drv;
402         const struct ntb_client_ops     ops;
403 };
404 #define drv_ntb_client(__drv) container_of((__drv), struct ntb_client, drv)
405
406 /**
407  * struct ntb_device - ntb device
408  * @dev:                Linux device object.
409  * @pdev:               PCI device entry of the ntb.
410  * @topo:               Detected topology of the ntb.
411  * @ops:                See &ntb_dev_ops.
412  * @ctx:                See &ntb_ctx_ops.
413  * @ctx_ops:            See &ntb_ctx_ops.
414  */
415 struct ntb_dev {
416         struct device                   dev;
417         struct pci_dev                  *pdev;
418         enum ntb_topo                   topo;
419         const struct ntb_dev_ops        *ops;
420         void                            *ctx;
421         const struct ntb_ctx_ops        *ctx_ops;
422
423         /* private: */
424
425         /* synchronize setting, clearing, and calling ctx_ops */
426         spinlock_t                      ctx_lock;
427         /* block unregister until device is fully released */
428         struct completion               released;
429 };
430 #define dev_ntb(__dev) container_of((__dev), struct ntb_dev, dev)
431
432 /**
433  * ntb_register_client() - register a client for interest in ntb devices
434  * @client:     Client context.
435  *
436  * The client will be added to the list of clients interested in ntb devices.
437  * The client will be notified of any ntb devices that are not already
438  * associated with a client, or if ntb devices are registered later.
439  *
440  * Return: Zero if the client is registered, otherwise an error number.
441  */
442 #define ntb_register_client(client) \
443         __ntb_register_client((client), THIS_MODULE, KBUILD_MODNAME)
444
445 int __ntb_register_client(struct ntb_client *client, struct module *mod,
446                           const char *mod_name);
447
448 /**
449  * ntb_unregister_client() - unregister a client for interest in ntb devices
450  * @client:     Client context.
451  *
452  * The client will be removed from the list of clients interested in ntb
453  * devices.  If any ntb devices are associated with the client, the client will
454  * be notified to remove those devices.
455  */
456 void ntb_unregister_client(struct ntb_client *client);
457
458 #define module_ntb_client(__ntb_client) \
459         module_driver(__ntb_client, ntb_register_client, \
460                         ntb_unregister_client)
461
462 /**
463  * ntb_register_device() - register a ntb device
464  * @ntb:        NTB device context.
465  *
466  * The device will be added to the list of ntb devices.  If any clients are
467  * interested in ntb devices, each client will be notified of the ntb device,
468  * until at most one client accepts the device.
469  *
470  * Return: Zero if the device is registered, otherwise an error number.
471  */
472 int ntb_register_device(struct ntb_dev *ntb);
473
474 /**
475  * ntb_register_device() - unregister a ntb device
476  * @ntb:        NTB device context.
477  *
478  * The device will be removed from the list of ntb devices.  If the ntb device
479  * is associated with a client, the client will be notified to remove the
480  * device.
481  */
482 void ntb_unregister_device(struct ntb_dev *ntb);
483
484 /**
485  * ntb_set_ctx() - associate a driver context with an ntb device
486  * @ntb:        NTB device context.
487  * @ctx:        Driver context.
488  * @ctx_ops:    Driver context operations.
489  *
490  * Associate a driver context and operations with a ntb device.  The context is
491  * provided by the client driver, and the driver may associate a different
492  * context with each ntb device.
493  *
494  * Return: Zero if the context is associated, otherwise an error number.
495  */
496 int ntb_set_ctx(struct ntb_dev *ntb, void *ctx,
497                 const struct ntb_ctx_ops *ctx_ops);
498
499 /**
500  * ntb_clear_ctx() - disassociate any driver context from an ntb device
501  * @ntb:        NTB device context.
502  *
503  * Clear any association that may exist between a driver context and the ntb
504  * device.
505  */
506 void ntb_clear_ctx(struct ntb_dev *ntb);
507
508 /**
509  * ntb_link_event() - notify driver context of a change in link status
510  * @ntb:        NTB device context.
511  *
512  * Notify the driver context that the link status may have changed.  The driver
513  * should call ntb_link_is_up() to get the current status.
514  */
515 void ntb_link_event(struct ntb_dev *ntb);
516
517 /**
518  * ntb_db_event() - notify driver context of a doorbell event
519  * @ntb:        NTB device context.
520  * @vector:     Interrupt vector number.
521  *
522  * Notify the driver context of a doorbell event.  If hardware supports
523  * multiple interrupt vectors for doorbells, the vector number indicates which
524  * vector received the interrupt.  The vector number is relative to the first
525  * vector used for doorbells, starting at zero, and must be less than
526  * ntb_db_vector_count().  The driver may call ntb_db_read() to check which
527  * doorbell bits need service, and ntb_db_vector_mask() to determine which of
528  * those bits are associated with the vector number.
529  */
530 void ntb_db_event(struct ntb_dev *ntb, int vector);
531
532 /**
533  * ntb_msg_event() - notify driver context of a message event
534  * @ntb:        NTB device context.
535  *
536  * Notify the driver context of a message event.  If hardware supports
537  * message registers, this event indicates, that a new message arrived in
538  * some incoming message register or last sent message couldn't be delivered.
539  * The events can be masked/unmasked by the methods ntb_msg_set_mask() and
540  * ntb_msg_clear_mask().
541  */
542 void ntb_msg_event(struct ntb_dev *ntb);
543
544 /**
545  * ntb_default_port_number() - get the default local port number
546  * @ntb:        NTB device context.
547  *
548  * If hardware driver doesn't specify port_number() callback method, the NTB
549  * is considered with just two ports. So this method returns default local
550  * port number in compliance with topology.
551  *
552  * NOTE Don't call this method directly. The ntb_port_number() function should
553  * be used instead.
554  *
555  * Return: the default local port number
556  */
557 int ntb_default_port_number(struct ntb_dev *ntb);
558
559 /**
560  * ntb_default_port_count() - get the default number of peer device ports
561  * @ntb:        NTB device context.
562  *
563  * By default hardware driver supports just one peer device.
564  *
565  * NOTE Don't call this method directly. The ntb_peer_port_count() function
566  * should be used instead.
567  *
568  * Return: the default number of peer ports
569  */
570 int ntb_default_peer_port_count(struct ntb_dev *ntb);
571
572 /**
573  * ntb_default_peer_port_number() - get the default peer port by given index
574  * @ntb:        NTB device context.
575  * @idx:        Peer port index (should not differ from zero).
576  *
577  * By default hardware driver supports just one peer device, so this method
578  * shall return the corresponding value from enum ntb_default_port.
579  *
580  * NOTE Don't call this method directly. The ntb_peer_port_number() function
581  * should be used instead.
582  *
583  * Return: the peer device port or negative value indicating an error
584  */
585 int ntb_default_peer_port_number(struct ntb_dev *ntb, int pidx);
586
587 /**
588  * ntb_default_peer_port_idx() - get the default peer device port index by
589  *                               given port number
590  * @ntb:        NTB device context.
591  * @port:       Peer port number (should be one of enum ntb_default_port).
592  *
593  * By default hardware driver supports just one peer device, so while
594  * specified port-argument indicates peer port from enum ntb_default_port,
595  * the return value shall be zero.
596  *
597  * NOTE Don't call this method directly. The ntb_peer_port_idx() function
598  * should be used instead.
599  *
600  * Return: the peer port index or negative value indicating an error
601  */
602 int ntb_default_peer_port_idx(struct ntb_dev *ntb, int port);
603
604 /**
605  * ntb_port_number() - get the local port number
606  * @ntb:        NTB device context.
607  *
608  * Hardware must support at least simple two-ports ntb connection
609  *
610  * Return: the local port number
611  */
612 static inline int ntb_port_number(struct ntb_dev *ntb)
613 {
614         if (!ntb->ops->port_number)
615                 return ntb_default_port_number(ntb);
616
617         return ntb->ops->port_number(ntb);
618 }
619
620 /**
621  * ntb_peer_port_count() - get the number of peer device ports
622  * @ntb:        NTB device context.
623  *
624  * Hardware may support an access to memory of several remote domains
625  * over multi-port NTB devices. This method returns the number of peers,
626  * local device can have shared memory with.
627  *
628  * Return: the number of peer ports
629  */
630 static inline int ntb_peer_port_count(struct ntb_dev *ntb)
631 {
632         if (!ntb->ops->peer_port_count)
633                 return ntb_default_peer_port_count(ntb);
634
635         return ntb->ops->peer_port_count(ntb);
636 }
637
638 /**
639  * ntb_peer_port_number() - get the peer port by given index
640  * @ntb:        NTB device context.
641  * @pidx:       Peer port index.
642  *
643  * Peer ports are continuously enumerated by NTB API logic, so this method
644  * lets to retrieve port real number by its index.
645  *
646  * Return: the peer device port or negative value indicating an error
647  */
648 static inline int ntb_peer_port_number(struct ntb_dev *ntb, int pidx)
649 {
650         if (!ntb->ops->peer_port_number)
651                 return ntb_default_peer_port_number(ntb, pidx);
652
653         return ntb->ops->peer_port_number(ntb, pidx);
654 }
655
656 /**
657  * ntb_peer_port_idx() - get the peer device port index by given port number
658  * @ntb:        NTB device context.
659  * @port:       Peer port number.
660  *
661  * Inverse operation of ntb_peer_port_number(), so one can get port index
662  * by specified port number.
663  *
664  * Return: the peer port index or negative value indicating an error
665  */
666 static inline int ntb_peer_port_idx(struct ntb_dev *ntb, int port)
667 {
668         if (!ntb->ops->peer_port_idx)
669                 return ntb_default_peer_port_idx(ntb, port);
670
671         return ntb->ops->peer_port_idx(ntb, port);
672 }
673
674 /**
675  * ntb_link_is_up() - get the current ntb link state
676  * @ntb:        NTB device context.
677  * @speed:      OUT - The link speed expressed as PCIe generation number.
678  * @width:      OUT - The link width expressed as the number of PCIe lanes.
679  *
680  * Get the current state of the ntb link.  It is recommended to query the link
681  * state once after every link event.  It is safe to query the link state in
682  * the context of the link event callback.
683  *
684  * Return: bitfield of indexed ports link state: bit is set/cleared if the
685  *         link is up/down respectively.
686  */
687 static inline u64 ntb_link_is_up(struct ntb_dev *ntb,
688                                  enum ntb_speed *speed, enum ntb_width *width)
689 {
690         return ntb->ops->link_is_up(ntb, speed, width);
691 }
692
693 /**
694  * ntb_link_enable() - enable the local port ntb connection
695  * @ntb:        NTB device context.
696  * @max_speed:  The maximum link speed expressed as PCIe generation number.
697  * @max_width:  The maximum link width expressed as the number of PCIe lanes.
698  *
699  * Enable the NTB/PCIe link on the local or remote (for bridge-to-bridge
700  * topology) side of the bridge. If it's supported the ntb device should train
701  * the link to its maximum speed and width, or the requested speed and width,
702  * whichever is smaller. Some hardware doesn't support PCIe link training, so
703  * the last two arguments will be ignored then.
704  *
705  * Return: Zero on success, otherwise an error number.
706  */
707 static inline int ntb_link_enable(struct ntb_dev *ntb,
708                                   enum ntb_speed max_speed,
709                                   enum ntb_width max_width)
710 {
711         return ntb->ops->link_enable(ntb, max_speed, max_width);
712 }
713
714 /**
715  * ntb_link_disable() - disable the local port ntb connection
716  * @ntb:        NTB device context.
717  *
718  * Disable the link on the local or remote (for b2b topology) of the ntb.
719  * The ntb device should disable the link.  Returning from this call must
720  * indicate that a barrier has passed, though with no more writes may pass in
721  * either direction across the link, except if this call returns an error
722  * number.
723  *
724  * Return: Zero on success, otherwise an error number.
725  */
726 static inline int ntb_link_disable(struct ntb_dev *ntb)
727 {
728         return ntb->ops->link_disable(ntb);
729 }
730
731 /**
732  * ntb_mw_count() - get the number of inbound memory windows, which could
733  *                  be created for a specified peer device
734  * @ntb:        NTB device context.
735  * @pidx:       Port index of peer device.
736  *
737  * Hardware and topology may support a different number of memory windows.
738  * Moreover different peer devices can support different number of memory
739  * windows. Simply speaking this method returns the number of possible inbound
740  * memory windows to share with specified peer device. Note: this may return
741  * zero if the link is not up yet.
742  *
743  * Return: the number of memory windows.
744  */
745 static inline int ntb_mw_count(struct ntb_dev *ntb, int pidx)
746 {
747         return ntb->ops->mw_count(ntb, pidx);
748 }
749
750 /**
751  * ntb_mw_get_align() - get the restriction parameters of inbound memory window
752  * @ntb:        NTB device context.
753  * @pidx:       Port index of peer device.
754  * @widx:       Memory window index.
755  * @addr_align: OUT - the base alignment for translating the memory window
756  * @size_align: OUT - the size alignment for translating the memory window
757  * @size_max:   OUT - the maximum size of the memory window
758  *
759  * Get the alignments of an inbound memory window with specified index.
760  * NULL may be given for any output parameter if the value is not needed.
761  * The alignment and size parameters may be used for allocation of proper
762  * shared memory. Note: this must only be called when the link is up.
763  *
764  * Return: Zero on success, otherwise a negative error number.
765  */
766 static inline int ntb_mw_get_align(struct ntb_dev *ntb, int pidx, int widx,
767                                    resource_size_t *addr_align,
768                                    resource_size_t *size_align,
769                                    resource_size_t *size_max)
770 {
771         if (!(ntb_link_is_up(ntb, NULL, NULL) & BIT_ULL(pidx)))
772                 return -ENOTCONN;
773
774         return ntb->ops->mw_get_align(ntb, pidx, widx, addr_align, size_align,
775                                       size_max);
776 }
777
778 /**
779  * ntb_mw_set_trans() - set the translation of an inbound memory window
780  * @ntb:        NTB device context.
781  * @pidx:       Port index of peer device.
782  * @widx:       Memory window index.
783  * @addr:       The dma address of local memory to expose to the peer.
784  * @size:       The size of the local memory to expose to the peer.
785  *
786  * Set the translation of a memory window.  The peer may access local memory
787  * through the window starting at the address, up to the size.  The address
788  * and size must be aligned in compliance with restrictions of
789  * ntb_mw_get_align(). The region size should not exceed the size_max parameter
790  * of that method.
791  *
792  * This method may not be implemented due to the hardware specific memory
793  * windows interface.
794  *
795  * Return: Zero on success, otherwise an error number.
796  */
797 static inline int ntb_mw_set_trans(struct ntb_dev *ntb, int pidx, int widx,
798                                    dma_addr_t addr, resource_size_t size)
799 {
800         if (!ntb->ops->mw_set_trans)
801                 return 0;
802
803         return ntb->ops->mw_set_trans(ntb, pidx, widx, addr, size);
804 }
805
806 /**
807  * ntb_mw_clear_trans() - clear the translation address of an inbound memory
808  *                        window
809  * @ntb:        NTB device context.
810  * @pidx:       Port index of peer device.
811  * @widx:       Memory window index.
812  *
813  * Clear the translation of an inbound memory window.  The peer may no longer
814  * access local memory through the window.
815  *
816  * Return: Zero on success, otherwise an error number.
817  */
818 static inline int ntb_mw_clear_trans(struct ntb_dev *ntb, int pidx, int widx)
819 {
820         if (!ntb->ops->mw_clear_trans)
821                 return ntb_mw_set_trans(ntb, pidx, widx, 0, 0);
822
823         return ntb->ops->mw_clear_trans(ntb, pidx, widx);
824 }
825
826 /**
827  * ntb_peer_mw_count() - get the number of outbound memory windows, which could
828  *                       be mapped to access a shared memory
829  * @ntb:        NTB device context.
830  *
831  * Hardware and topology may support a different number of memory windows.
832  * This method returns the number of outbound memory windows supported by
833  * local device.
834  *
835  * Return: the number of memory windows.
836  */
837 static inline int ntb_peer_mw_count(struct ntb_dev *ntb)
838 {
839         return ntb->ops->peer_mw_count(ntb);
840 }
841
842 /**
843  * ntb_peer_mw_get_addr() - get map address of an outbound memory window
844  * @ntb:        NTB device context.
845  * @widx:       Memory window index (within ntb_peer_mw_count() return value).
846  * @base:       OUT - the base address of mapping region.
847  * @size:       OUT - the size of mapping region.
848  *
849  * Get base and size of memory region to map.  NULL may be given for any output
850  * parameter if the value is not needed.  The base and size may be used for
851  * mapping the memory window, to access the peer memory.
852  *
853  * Return: Zero on success, otherwise a negative error number.
854  */
855 static inline int ntb_peer_mw_get_addr(struct ntb_dev *ntb, int widx,
856                                       phys_addr_t *base, resource_size_t *size)
857 {
858         return ntb->ops->peer_mw_get_addr(ntb, widx, base, size);
859 }
860
861 /**
862  * ntb_peer_mw_set_trans() - set a translation address of a memory window
863  *                           retrieved from a peer device
864  * @ntb:        NTB device context.
865  * @pidx:       Port index of peer device the translation address received from.
866  * @widx:       Memory window index.
867  * @addr:       The dma address of the shared memory to access.
868  * @size:       The size of the shared memory to access.
869  *
870  * Set the translation of an outbound memory window.  The local device may
871  * access shared memory allocated by a peer device sent the address.
872  *
873  * This method may not be implemented due to the hardware specific memory
874  * windows interface, so a translation address can be only set on the side,
875  * where shared memory (inbound memory windows) is allocated.
876  *
877  * Return: Zero on success, otherwise an error number.
878  */
879 static inline int ntb_peer_mw_set_trans(struct ntb_dev *ntb, int pidx, int widx,
880                                         u64 addr, resource_size_t size)
881 {
882         if (!ntb->ops->peer_mw_set_trans)
883                 return 0;
884
885         return ntb->ops->peer_mw_set_trans(ntb, pidx, widx, addr, size);
886 }
887
888 /**
889  * ntb_peer_mw_clear_trans() - clear the translation address of an outbound
890  *                             memory window
891  * @ntb:        NTB device context.
892  * @pidx:       Port index of peer device.
893  * @widx:       Memory window index.
894  *
895  * Clear the translation of a outbound memory window.  The local device may no
896  * longer access a shared memory through the window.
897  *
898  * This method may not be implemented due to the hardware specific memory
899  * windows interface.
900  *
901  * Return: Zero on success, otherwise an error number.
902  */
903 static inline int ntb_peer_mw_clear_trans(struct ntb_dev *ntb, int pidx,
904                                           int widx)
905 {
906         if (!ntb->ops->peer_mw_clear_trans)
907                 return ntb_peer_mw_set_trans(ntb, pidx, widx, 0, 0);
908
909         return ntb->ops->peer_mw_clear_trans(ntb, pidx, widx);
910 }
911
912 /**
913  * ntb_db_is_unsafe() - check if it is safe to use hardware doorbell
914  * @ntb:        NTB device context.
915  *
916  * It is possible for some ntb hardware to be affected by errata.  Hardware
917  * drivers can advise clients to avoid using doorbells.  Clients may ignore
918  * this advice, though caution is recommended.
919  *
920  * Return: Zero if it is safe to use doorbells, or One if it is not safe.
921  */
922 static inline int ntb_db_is_unsafe(struct ntb_dev *ntb)
923 {
924         if (!ntb->ops->db_is_unsafe)
925                 return 0;
926
927         return ntb->ops->db_is_unsafe(ntb);
928 }
929
930 /**
931  * ntb_db_valid_mask() - get a mask of doorbell bits supported by the ntb
932  * @ntb:        NTB device context.
933  *
934  * Hardware may support different number or arrangement of doorbell bits.
935  *
936  * Return: A mask of doorbell bits supported by the ntb.
937  */
938 static inline u64 ntb_db_valid_mask(struct ntb_dev *ntb)
939 {
940         return ntb->ops->db_valid_mask(ntb);
941 }
942
943 /**
944  * ntb_db_vector_count() - get the number of doorbell interrupt vectors
945  * @ntb:        NTB device context.
946  *
947  * Hardware may support different number of interrupt vectors.
948  *
949  * Return: The number of doorbell interrupt vectors.
950  */
951 static inline int ntb_db_vector_count(struct ntb_dev *ntb)
952 {
953         if (!ntb->ops->db_vector_count)
954                 return 1;
955
956         return ntb->ops->db_vector_count(ntb);
957 }
958
959 /**
960  * ntb_db_vector_mask() - get a mask of doorbell bits serviced by a vector
961  * @ntb:        NTB device context.
962  * @vector:     Doorbell vector number.
963  *
964  * Each interrupt vector may have a different number or arrangement of bits.
965  *
966  * Return: A mask of doorbell bits serviced by a vector.
967  */
968 static inline u64 ntb_db_vector_mask(struct ntb_dev *ntb, int vector)
969 {
970         if (!ntb->ops->db_vector_mask)
971                 return ntb_db_valid_mask(ntb);
972
973         return ntb->ops->db_vector_mask(ntb, vector);
974 }
975
976 /**
977  * ntb_db_read() - read the local doorbell register
978  * @ntb:        NTB device context.
979  *
980  * Read the local doorbell register, and return the bits that are set.
981  *
982  * Return: The bits currently set in the local doorbell register.
983  */
984 static inline u64 ntb_db_read(struct ntb_dev *ntb)
985 {
986         return ntb->ops->db_read(ntb);
987 }
988
989 /**
990  * ntb_db_set() - set bits in the local doorbell register
991  * @ntb:        NTB device context.
992  * @db_bits:    Doorbell bits to set.
993  *
994  * Set bits in the local doorbell register, which may generate a local doorbell
995  * interrupt.  Bits that were already set must remain set.
996  *
997  * This is unusual, and hardware may not support it.
998  *
999  * Return: Zero on success, otherwise an error number.
1000  */
1001 static inline int ntb_db_set(struct ntb_dev *ntb, u64 db_bits)
1002 {
1003         if (!ntb->ops->db_set)
1004                 return -EINVAL;
1005
1006         return ntb->ops->db_set(ntb, db_bits);
1007 }
1008
1009 /**
1010  * ntb_db_clear() - clear bits in the local doorbell register
1011  * @ntb:        NTB device context.
1012  * @db_bits:    Doorbell bits to clear.
1013  *
1014  * Clear bits in the local doorbell register, arming the bits for the next
1015  * doorbell.
1016  *
1017  * Return: Zero on success, otherwise an error number.
1018  */
1019 static inline int ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
1020 {
1021         return ntb->ops->db_clear(ntb, db_bits);
1022 }
1023
1024 /**
1025  * ntb_db_read_mask() - read the local doorbell mask
1026  * @ntb:        NTB device context.
1027  *
1028  * Read the local doorbell mask register, and return the bits that are set.
1029  *
1030  * This is unusual, though hardware is likely to support it.
1031  *
1032  * Return: The bits currently set in the local doorbell mask register.
1033  */
1034 static inline u64 ntb_db_read_mask(struct ntb_dev *ntb)
1035 {
1036         if (!ntb->ops->db_read_mask)
1037                 return 0;
1038
1039         return ntb->ops->db_read_mask(ntb);
1040 }
1041
1042 /**
1043  * ntb_db_set_mask() - set bits in the local doorbell mask
1044  * @ntb:        NTB device context.
1045  * @db_bits:    Doorbell mask bits to set.
1046  *
1047  * Set bits in the local doorbell mask register, preventing doorbell interrupts
1048  * from being generated for those doorbell bits.  Bits that were already set
1049  * must remain set.
1050  *
1051  * Return: Zero on success, otherwise an error number.
1052  */
1053 static inline int ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
1054 {
1055         return ntb->ops->db_set_mask(ntb, db_bits);
1056 }
1057
1058 /**
1059  * ntb_db_clear_mask() - clear bits in the local doorbell mask
1060  * @ntb:        NTB device context.
1061  * @db_bits:    Doorbell bits to clear.
1062  *
1063  * Clear bits in the local doorbell mask register, allowing doorbell interrupts
1064  * from being generated for those doorbell bits.  If a doorbell bit is already
1065  * set at the time the mask is cleared, and the corresponding mask bit is
1066  * changed from set to clear, then the ntb driver must ensure that
1067  * ntb_db_event() is called.  If the hardware does not generate the interrupt
1068  * on clearing the mask bit, then the driver must call ntb_db_event() anyway.
1069  *
1070  * Return: Zero on success, otherwise an error number.
1071  */
1072 static inline int ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
1073 {
1074         return ntb->ops->db_clear_mask(ntb, db_bits);
1075 }
1076
1077 /**
1078  * ntb_peer_db_addr() - address and size of the peer doorbell register
1079  * @ntb:        NTB device context.
1080  * @db_addr:    OUT - The address of the peer doorbell register.
1081  * @db_size:    OUT - The number of bytes to write the peer doorbell register.
1082  * @db_data:    OUT - The data of peer doorbell register
1083  * @db_bit:             door bell bit number
1084  *
1085  * Return the address of the peer doorbell register.  This may be used, for
1086  * example, by drivers that offload memory copy operations to a dma engine.
1087  * The drivers may wish to ring the peer doorbell at the completion of memory
1088  * copy operations.  For efficiency, and to simplify ordering of operations
1089  * between the dma memory copies and the ringing doorbell, the driver may
1090  * append one additional dma memory copy with the doorbell register as the
1091  * destination, after the memory copy operations.
1092  *
1093  * Return: Zero on success, otherwise an error number.
1094  */
1095 static inline int ntb_peer_db_addr(struct ntb_dev *ntb,
1096                                    phys_addr_t *db_addr,
1097                                    resource_size_t *db_size,
1098                                    u64 *db_data, int db_bit)
1099 {
1100         if (!ntb->ops->peer_db_addr)
1101                 return -EINVAL;
1102
1103         return ntb->ops->peer_db_addr(ntb, db_addr, db_size, db_data, db_bit);
1104 }
1105
1106 /**
1107  * ntb_peer_db_read() - read the peer doorbell register
1108  * @ntb:        NTB device context.
1109  *
1110  * Read the peer doorbell register, and return the bits that are set.
1111  *
1112  * This is unusual, and hardware may not support it.
1113  *
1114  * Return: The bits currently set in the peer doorbell register.
1115  */
1116 static inline u64 ntb_peer_db_read(struct ntb_dev *ntb)
1117 {
1118         if (!ntb->ops->peer_db_read)
1119                 return 0;
1120
1121         return ntb->ops->peer_db_read(ntb);
1122 }
1123
1124 /**
1125  * ntb_peer_db_set() - set bits in the peer doorbell register
1126  * @ntb:        NTB device context.
1127  * @db_bits:    Doorbell bits to set.
1128  *
1129  * Set bits in the peer doorbell register, which may generate a peer doorbell
1130  * interrupt.  Bits that were already set must remain set.
1131  *
1132  * Return: Zero on success, otherwise an error number.
1133  */
1134 static inline int ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
1135 {
1136         return ntb->ops->peer_db_set(ntb, db_bits);
1137 }
1138
1139 /**
1140  * ntb_peer_db_clear() - clear bits in the peer doorbell register
1141  * @ntb:        NTB device context.
1142  * @db_bits:    Doorbell bits to clear.
1143  *
1144  * Clear bits in the peer doorbell register, arming the bits for the next
1145  * doorbell.
1146  *
1147  * This is unusual, and hardware may not support it.
1148  *
1149  * Return: Zero on success, otherwise an error number.
1150  */
1151 static inline int ntb_peer_db_clear(struct ntb_dev *ntb, u64 db_bits)
1152 {
1153         if (!ntb->ops->db_clear)
1154                 return -EINVAL;
1155
1156         return ntb->ops->peer_db_clear(ntb, db_bits);
1157 }
1158
1159 /**
1160  * ntb_peer_db_read_mask() - read the peer doorbell mask
1161  * @ntb:        NTB device context.
1162  *
1163  * Read the peer doorbell mask register, and return the bits that are set.
1164  *
1165  * This is unusual, and hardware may not support it.
1166  *
1167  * Return: The bits currently set in the peer doorbell mask register.
1168  */
1169 static inline u64 ntb_peer_db_read_mask(struct ntb_dev *ntb)
1170 {
1171         if (!ntb->ops->db_read_mask)
1172                 return 0;
1173
1174         return ntb->ops->peer_db_read_mask(ntb);
1175 }
1176
1177 /**
1178  * ntb_peer_db_set_mask() - set bits in the peer doorbell mask
1179  * @ntb:        NTB device context.
1180  * @db_bits:    Doorbell mask bits to set.
1181  *
1182  * Set bits in the peer doorbell mask register, preventing doorbell interrupts
1183  * from being generated for those doorbell bits.  Bits that were already set
1184  * must remain set.
1185  *
1186  * This is unusual, and hardware may not support it.
1187  *
1188  * Return: Zero on success, otherwise an error number.
1189  */
1190 static inline int ntb_peer_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
1191 {
1192         if (!ntb->ops->db_set_mask)
1193                 return -EINVAL;
1194
1195         return ntb->ops->peer_db_set_mask(ntb, db_bits);
1196 }
1197
1198 /**
1199  * ntb_peer_db_clear_mask() - clear bits in the peer doorbell mask
1200  * @ntb:        NTB device context.
1201  * @db_bits:    Doorbell bits to clear.
1202  *
1203  * Clear bits in the peer doorbell mask register, allowing doorbell interrupts
1204  * from being generated for those doorbell bits.  If the hardware does not
1205  * generate the interrupt on clearing the mask bit, then the driver should not
1206  * implement this function!
1207  *
1208  * This is unusual, and hardware may not support it.
1209  *
1210  * Return: Zero on success, otherwise an error number.
1211  */
1212 static inline int ntb_peer_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
1213 {
1214         if (!ntb->ops->db_clear_mask)
1215                 return -EINVAL;
1216
1217         return ntb->ops->peer_db_clear_mask(ntb, db_bits);
1218 }
1219
1220 /**
1221  * ntb_spad_is_unsafe() - check if it is safe to use the hardware scratchpads
1222  * @ntb:        NTB device context.
1223  *
1224  * It is possible for some ntb hardware to be affected by errata.  Hardware
1225  * drivers can advise clients to avoid using scratchpads.  Clients may ignore
1226  * this advice, though caution is recommended.
1227  *
1228  * Return: Zero if it is safe to use scratchpads, or One if it is not safe.
1229  */
1230 static inline int ntb_spad_is_unsafe(struct ntb_dev *ntb)
1231 {
1232         if (!ntb->ops->spad_is_unsafe)
1233                 return 0;
1234
1235         return ntb->ops->spad_is_unsafe(ntb);
1236 }
1237
1238 /**
1239  * ntb_spad_count() - get the number of scratchpads
1240  * @ntb:        NTB device context.
1241  *
1242  * Hardware and topology may support a different number of scratchpads.
1243  * Although it must be the same for all ports per NTB device.
1244  *
1245  * Return: the number of scratchpads.
1246  */
1247 static inline int ntb_spad_count(struct ntb_dev *ntb)
1248 {
1249         if (!ntb->ops->spad_count)
1250                 return 0;
1251
1252         return ntb->ops->spad_count(ntb);
1253 }
1254
1255 /**
1256  * ntb_spad_read() - read the local scratchpad register
1257  * @ntb:        NTB device context.
1258  * @sidx:       Scratchpad index.
1259  *
1260  * Read the local scratchpad register, and return the value.
1261  *
1262  * Return: The value of the local scratchpad register.
1263  */
1264 static inline u32 ntb_spad_read(struct ntb_dev *ntb, int sidx)
1265 {
1266         if (!ntb->ops->spad_read)
1267                 return ~(u32)0;
1268
1269         return ntb->ops->spad_read(ntb, sidx);
1270 }
1271
1272 /**
1273  * ntb_spad_write() - write the local scratchpad register
1274  * @ntb:        NTB device context.
1275  * @sidx:       Scratchpad index.
1276  * @val:        Scratchpad value.
1277  *
1278  * Write the value to the local scratchpad register.
1279  *
1280  * Return: Zero on success, otherwise an error number.
1281  */
1282 static inline int ntb_spad_write(struct ntb_dev *ntb, int sidx, u32 val)
1283 {
1284         if (!ntb->ops->spad_write)
1285                 return -EINVAL;
1286
1287         return ntb->ops->spad_write(ntb, sidx, val);
1288 }
1289
1290 /**
1291  * ntb_peer_spad_addr() - address of the peer scratchpad register
1292  * @ntb:        NTB device context.
1293  * @pidx:       Port index of peer device.
1294  * @sidx:       Scratchpad index.
1295  * @spad_addr:  OUT - The address of the peer scratchpad register.
1296  *
1297  * Return the address of the peer doorbell register.  This may be used, for
1298  * example, by drivers that offload memory copy operations to a dma engine.
1299  *
1300  * Return: Zero on success, otherwise an error number.
1301  */
1302 static inline int ntb_peer_spad_addr(struct ntb_dev *ntb, int pidx, int sidx,
1303                                      phys_addr_t *spad_addr)
1304 {
1305         if (!ntb->ops->peer_spad_addr)
1306                 return -EINVAL;
1307
1308         return ntb->ops->peer_spad_addr(ntb, pidx, sidx, spad_addr);
1309 }
1310
1311 /**
1312  * ntb_peer_spad_read() - read the peer scratchpad register
1313  * @ntb:        NTB device context.
1314  * @pidx:       Port index of peer device.
1315  * @sidx:       Scratchpad index.
1316  *
1317  * Read the peer scratchpad register, and return the value.
1318  *
1319  * Return: The value of the local scratchpad register.
1320  */
1321 static inline u32 ntb_peer_spad_read(struct ntb_dev *ntb, int pidx, int sidx)
1322 {
1323         if (!ntb->ops->peer_spad_read)
1324                 return ~(u32)0;
1325
1326         return ntb->ops->peer_spad_read(ntb, pidx, sidx);
1327 }
1328
1329 /**
1330  * ntb_peer_spad_write() - write the peer scratchpad register
1331  * @ntb:        NTB device context.
1332  * @pidx:       Port index of peer device.
1333  * @sidx:       Scratchpad index.
1334  * @val:        Scratchpad value.
1335  *
1336  * Write the value to the peer scratchpad register.
1337  *
1338  * Return: Zero on success, otherwise an error number.
1339  */
1340 static inline int ntb_peer_spad_write(struct ntb_dev *ntb, int pidx, int sidx,
1341                                       u32 val)
1342 {
1343         if (!ntb->ops->peer_spad_write)
1344                 return -EINVAL;
1345
1346         return ntb->ops->peer_spad_write(ntb, pidx, sidx, val);
1347 }
1348
1349 /**
1350  * ntb_msg_count() - get the number of message registers
1351  * @ntb:        NTB device context.
1352  *
1353  * Hardware may support a different number of message registers.
1354  *
1355  * Return: the number of message registers.
1356  */
1357 static inline int ntb_msg_count(struct ntb_dev *ntb)
1358 {
1359         if (!ntb->ops->msg_count)
1360                 return 0;
1361
1362         return ntb->ops->msg_count(ntb);
1363 }
1364
1365 /**
1366  * ntb_msg_inbits() - get a bitfield of inbound message registers status
1367  * @ntb:        NTB device context.
1368  *
1369  * The method returns the bitfield of status and mask registers, which related
1370  * to inbound message registers.
1371  *
1372  * Return: bitfield of inbound message registers.
1373  */
1374 static inline u64 ntb_msg_inbits(struct ntb_dev *ntb)
1375 {
1376         if (!ntb->ops->msg_inbits)
1377                 return 0;
1378
1379         return ntb->ops->msg_inbits(ntb);
1380 }
1381
1382 /**
1383  * ntb_msg_outbits() - get a bitfield of outbound message registers status
1384  * @ntb:        NTB device context.
1385  *
1386  * The method returns the bitfield of status and mask registers, which related
1387  * to outbound message registers.
1388  *
1389  * Return: bitfield of outbound message registers.
1390  */
1391 static inline u64 ntb_msg_outbits(struct ntb_dev *ntb)
1392 {
1393         if (!ntb->ops->msg_outbits)
1394                 return 0;
1395
1396         return ntb->ops->msg_outbits(ntb);
1397 }
1398
1399 /**
1400  * ntb_msg_read_sts() - read the message registers status
1401  * @ntb:        NTB device context.
1402  *
1403  * Read the status of message register. Inbound and outbound message registers
1404  * related bits can be filtered by masks retrieved from ntb_msg_inbits() and
1405  * ntb_msg_outbits().
1406  *
1407  * Return: status bits of message registers
1408  */
1409 static inline u64 ntb_msg_read_sts(struct ntb_dev *ntb)
1410 {
1411         if (!ntb->ops->msg_read_sts)
1412                 return 0;
1413
1414         return ntb->ops->msg_read_sts(ntb);
1415 }
1416
1417 /**
1418  * ntb_msg_clear_sts() - clear status bits of message registers
1419  * @ntb:        NTB device context.
1420  * @sts_bits:   Status bits to clear.
1421  *
1422  * Clear bits in the status register.
1423  *
1424  * Return: Zero on success, otherwise a negative error number.
1425  */
1426 static inline int ntb_msg_clear_sts(struct ntb_dev *ntb, u64 sts_bits)
1427 {
1428         if (!ntb->ops->msg_clear_sts)
1429                 return -EINVAL;
1430
1431         return ntb->ops->msg_clear_sts(ntb, sts_bits);
1432 }
1433
1434 /**
1435  * ntb_msg_set_mask() - set mask of message register status bits
1436  * @ntb:        NTB device context.
1437  * @mask_bits:  Mask bits.
1438  *
1439  * Mask the message registers status bits from raising the message event.
1440  *
1441  * Return: Zero on success, otherwise a negative error number.
1442  */
1443 static inline int ntb_msg_set_mask(struct ntb_dev *ntb, u64 mask_bits)
1444 {
1445         if (!ntb->ops->msg_set_mask)
1446                 return -EINVAL;
1447
1448         return ntb->ops->msg_set_mask(ntb, mask_bits);
1449 }
1450
1451 /**
1452  * ntb_msg_clear_mask() - clear message registers mask
1453  * @ntb:        NTB device context.
1454  * @mask_bits:  Mask bits to clear.
1455  *
1456  * Clear bits in the message events mask register.
1457  *
1458  * Return: Zero on success, otherwise a negative error number.
1459  */
1460 static inline int ntb_msg_clear_mask(struct ntb_dev *ntb, u64 mask_bits)
1461 {
1462         if (!ntb->ops->msg_clear_mask)
1463                 return -EINVAL;
1464
1465         return ntb->ops->msg_clear_mask(ntb, mask_bits);
1466 }
1467
1468 /**
1469  * ntb_msg_read() - read inbound message register with specified index
1470  * @ntb:        NTB device context.
1471  * @pidx:       OUT - Port index of peer device a message retrieved from
1472  * @midx:       Message register index
1473  *
1474  * Read data from the specified message register. Source port index of a
1475  * message is retrieved as well.
1476  *
1477  * Return: The value of the inbound message register.
1478  */
1479 static inline u32 ntb_msg_read(struct ntb_dev *ntb, int *pidx, int midx)
1480 {
1481         if (!ntb->ops->msg_read)
1482                 return ~(u32)0;
1483
1484         return ntb->ops->msg_read(ntb, pidx, midx);
1485 }
1486
1487 /**
1488  * ntb_peer_msg_write() - write data to the specified peer message register
1489  * @ntb:        NTB device context.
1490  * @pidx:       Port index of peer device a message being sent to
1491  * @midx:       Message register index
1492  * @msg:        Data to send
1493  *
1494  * Send data to a specified peer device using the defined message register.
1495  * Message event can be raised if the midx registers isn't empty while
1496  * calling this method and the corresponding interrupt isn't masked.
1497  *
1498  * Return: Zero on success, otherwise a negative error number.
1499  */
1500 static inline int ntb_peer_msg_write(struct ntb_dev *ntb, int pidx, int midx,
1501                                      u32 msg)
1502 {
1503         if (!ntb->ops->peer_msg_write)
1504                 return -EINVAL;
1505
1506         return ntb->ops->peer_msg_write(ntb, pidx, midx, msg);
1507 }
1508
1509 #endif