blob: d7ab3e1ec88fc0310959899eb5076b17b68fe744 [file] [log] [blame]
Allen Hubbea1bd3ba2015-04-09 10:33:20 -04001/*
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 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
20 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 *
26 * * Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * * Redistributions in binary form must reproduce the above copy
29 * notice, this list of conditions and the following disclaimer in
30 * the documentation and/or other materials provided with the
31 * distribution.
32 * * Neither the name of Intel Corporation nor the names of its
33 * contributors may be used to endorse or promote products derived
34 * from this software without specific prior written permission.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 *
48 * PCIe NTB Linux driver
49 *
50 * Contact Information:
51 * Allen Hubbe <Allen.Hubbe@emc.com>
52 */
53
54#ifndef _NTB_H_
55#define _NTB_H_
56
57#include <linux/completion.h>
58#include <linux/device.h>
59
60struct ntb_client;
61struct ntb_dev;
62struct pci_dev;
63
64/**
65 * enum ntb_topo - NTB connection topology
66 * @NTB_TOPO_NONE: Topology is unknown or invalid.
67 * @NTB_TOPO_PRI: On primary side of local ntb.
68 * @NTB_TOPO_SEC: On secondary side of remote ntb.
69 * @NTB_TOPO_B2B_USD: On primary side of local ntb upstream of remote ntb.
70 * @NTB_TOPO_B2B_DSD: On primary side of local ntb downstream of remote ntb.
71 */
72enum ntb_topo {
73 NTB_TOPO_NONE = -1,
74 NTB_TOPO_PRI,
75 NTB_TOPO_SEC,
76 NTB_TOPO_B2B_USD,
77 NTB_TOPO_B2B_DSD,
78};
79
80static inline int ntb_topo_is_b2b(enum ntb_topo topo)
81{
82 switch ((int)topo) {
83 case NTB_TOPO_B2B_USD:
84 case NTB_TOPO_B2B_DSD:
85 return 1;
86 }
87 return 0;
88}
89
90static inline char *ntb_topo_string(enum ntb_topo topo)
91{
92 switch (topo) {
93 case NTB_TOPO_NONE: return "NTB_TOPO_NONE";
94 case NTB_TOPO_PRI: return "NTB_TOPO_PRI";
95 case NTB_TOPO_SEC: return "NTB_TOPO_SEC";
96 case NTB_TOPO_B2B_USD: return "NTB_TOPO_B2B_USD";
97 case NTB_TOPO_B2B_DSD: return "NTB_TOPO_B2B_DSD";
98 }
99 return "NTB_TOPO_INVALID";
100}
101
102/**
103 * enum ntb_speed - NTB link training speed
104 * @NTB_SPEED_AUTO: Request the max supported speed.
105 * @NTB_SPEED_NONE: Link is not trained to any speed.
106 * @NTB_SPEED_GEN1: Link is trained to gen1 speed.
107 * @NTB_SPEED_GEN2: Link is trained to gen2 speed.
108 * @NTB_SPEED_GEN3: Link is trained to gen3 speed.
109 */
110enum ntb_speed {
111 NTB_SPEED_AUTO = -1,
112 NTB_SPEED_NONE = 0,
113 NTB_SPEED_GEN1 = 1,
114 NTB_SPEED_GEN2 = 2,
115 NTB_SPEED_GEN3 = 3,
116};
117
118/**
119 * enum ntb_width - NTB link training width
120 * @NTB_WIDTH_AUTO: Request the max supported width.
121 * @NTB_WIDTH_NONE: Link is not trained to any width.
122 * @NTB_WIDTH_1: Link is trained to 1 lane width.
123 * @NTB_WIDTH_2: Link is trained to 2 lane width.
124 * @NTB_WIDTH_4: Link is trained to 4 lane width.
125 * @NTB_WIDTH_8: Link is trained to 8 lane width.
126 * @NTB_WIDTH_12: Link is trained to 12 lane width.
127 * @NTB_WIDTH_16: Link is trained to 16 lane width.
128 * @NTB_WIDTH_32: Link is trained to 32 lane width.
129 */
130enum ntb_width {
131 NTB_WIDTH_AUTO = -1,
132 NTB_WIDTH_NONE = 0,
133 NTB_WIDTH_1 = 1,
134 NTB_WIDTH_2 = 2,
135 NTB_WIDTH_4 = 4,
136 NTB_WIDTH_8 = 8,
137 NTB_WIDTH_12 = 12,
138 NTB_WIDTH_16 = 16,
139 NTB_WIDTH_32 = 32,
140};
141
142/**
143 * struct ntb_client_ops - ntb client operations
144 * @probe: Notify client of a new device.
145 * @remove: Notify client to remove a device.
146 */
147struct ntb_client_ops {
148 int (*probe)(struct ntb_client *client, struct ntb_dev *ntb);
149 void (*remove)(struct ntb_client *client, struct ntb_dev *ntb);
150};
151
152static inline int ntb_client_ops_is_valid(const struct ntb_client_ops *ops)
153{
154 /* commented callbacks are not required: */
155 return
156 ops->probe &&
157 ops->remove &&
158 1;
159}
160
161/**
162 * struct ntb_ctx_ops - ntb driver context operations
163 * @link_event: See ntb_link_event().
164 * @db_event: See ntb_db_event().
165 */
166struct ntb_ctx_ops {
167 void (*link_event)(void *ctx);
168 void (*db_event)(void *ctx, int db_vector);
169};
170
171static inline int ntb_ctx_ops_is_valid(const struct ntb_ctx_ops *ops)
172{
173 /* commented callbacks are not required: */
174 return
175 /* ops->link_event && */
176 /* ops->db_event && */
177 1;
178}
179
180/**
181 * struct ntb_ctx_ops - ntb device operations
Serge Semin60934b22016-12-14 02:49:13 +0300182 * @link_is_up: See ntb_link_is_up().
183 * @link_enable: See ntb_link_enable().
184 * @link_disable: See ntb_link_disable().
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400185 * @mw_count: See ntb_mw_count().
186 * @mw_get_range: See ntb_mw_get_range().
187 * @mw_set_trans: See ntb_mw_set_trans().
188 * @mw_clear_trans: See ntb_mw_clear_trans().
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400189 * @db_is_unsafe: See ntb_db_is_unsafe().
190 * @db_valid_mask: See ntb_db_valid_mask().
191 * @db_vector_count: See ntb_db_vector_count().
192 * @db_vector_mask: See ntb_db_vector_mask().
193 * @db_read: See ntb_db_read().
194 * @db_set: See ntb_db_set().
195 * @db_clear: See ntb_db_clear().
196 * @db_read_mask: See ntb_db_read_mask().
197 * @db_set_mask: See ntb_db_set_mask().
198 * @db_clear_mask: See ntb_db_clear_mask().
199 * @peer_db_addr: See ntb_peer_db_addr().
200 * @peer_db_read: See ntb_peer_db_read().
201 * @peer_db_set: See ntb_peer_db_set().
202 * @peer_db_clear: See ntb_peer_db_clear().
203 * @peer_db_read_mask: See ntb_peer_db_read_mask().
204 * @peer_db_set_mask: See ntb_peer_db_set_mask().
205 * @peer_db_clear_mask: See ntb_peer_db_clear_mask().
206 * @spad_is_unsafe: See ntb_spad_is_unsafe().
207 * @spad_count: See ntb_spad_count().
208 * @spad_read: See ntb_spad_read().
209 * @spad_write: See ntb_spad_write().
210 * @peer_spad_addr: See ntb_peer_spad_addr().
211 * @peer_spad_read: See ntb_peer_spad_read().
212 * @peer_spad_write: See ntb_peer_spad_write().
213 */
214struct ntb_dev_ops {
Serge Semin60934b22016-12-14 02:49:13 +0300215 int (*link_is_up)(struct ntb_dev *ntb,
216 enum ntb_speed *speed, enum ntb_width *width);
217 int (*link_enable)(struct ntb_dev *ntb,
218 enum ntb_speed max_speed, enum ntb_width max_width);
219 int (*link_disable)(struct ntb_dev *ntb);
220
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400221 int (*mw_count)(struct ntb_dev *ntb);
222 int (*mw_get_range)(struct ntb_dev *ntb, int idx,
223 phys_addr_t *base, resource_size_t *size,
224 resource_size_t *align, resource_size_t *align_size);
225 int (*mw_set_trans)(struct ntb_dev *ntb, int idx,
226 dma_addr_t addr, resource_size_t size);
227 int (*mw_clear_trans)(struct ntb_dev *ntb, int idx);
228
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400229 int (*db_is_unsafe)(struct ntb_dev *ntb);
230 u64 (*db_valid_mask)(struct ntb_dev *ntb);
231 int (*db_vector_count)(struct ntb_dev *ntb);
232 u64 (*db_vector_mask)(struct ntb_dev *ntb, int db_vector);
233
234 u64 (*db_read)(struct ntb_dev *ntb);
235 int (*db_set)(struct ntb_dev *ntb, u64 db_bits);
236 int (*db_clear)(struct ntb_dev *ntb, u64 db_bits);
237
238 u64 (*db_read_mask)(struct ntb_dev *ntb);
239 int (*db_set_mask)(struct ntb_dev *ntb, u64 db_bits);
240 int (*db_clear_mask)(struct ntb_dev *ntb, u64 db_bits);
241
242 int (*peer_db_addr)(struct ntb_dev *ntb,
243 phys_addr_t *db_addr, resource_size_t *db_size);
244 u64 (*peer_db_read)(struct ntb_dev *ntb);
245 int (*peer_db_set)(struct ntb_dev *ntb, u64 db_bits);
246 int (*peer_db_clear)(struct ntb_dev *ntb, u64 db_bits);
247
248 u64 (*peer_db_read_mask)(struct ntb_dev *ntb);
249 int (*peer_db_set_mask)(struct ntb_dev *ntb, u64 db_bits);
250 int (*peer_db_clear_mask)(struct ntb_dev *ntb, u64 db_bits);
251
252 int (*spad_is_unsafe)(struct ntb_dev *ntb);
253 int (*spad_count)(struct ntb_dev *ntb);
254
255 u32 (*spad_read)(struct ntb_dev *ntb, int idx);
256 int (*spad_write)(struct ntb_dev *ntb, int idx, u32 val);
257
258 int (*peer_spad_addr)(struct ntb_dev *ntb, int idx,
259 phys_addr_t *spad_addr);
260 u32 (*peer_spad_read)(struct ntb_dev *ntb, int idx);
261 int (*peer_spad_write)(struct ntb_dev *ntb, int idx, u32 val);
262};
263
264static inline int ntb_dev_ops_is_valid(const struct ntb_dev_ops *ops)
265{
266 /* commented callbacks are not required: */
267 return
Serge Semin60934b22016-12-14 02:49:13 +0300268 ops->link_is_up &&
269 ops->link_enable &&
270 ops->link_disable &&
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400271 ops->mw_count &&
272 ops->mw_get_range &&
273 ops->mw_set_trans &&
274 /* ops->mw_clear_trans && */
Serge Semin60934b22016-12-14 02:49:13 +0300275
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400276 /* ops->db_is_unsafe && */
277 ops->db_valid_mask &&
278
279 /* both set, or both unset */
280 (!ops->db_vector_count == !ops->db_vector_mask) &&
281
282 ops->db_read &&
283 /* ops->db_set && */
284 ops->db_clear &&
285 /* ops->db_read_mask && */
286 ops->db_set_mask &&
287 ops->db_clear_mask &&
Allen Hubbeafc54992016-03-21 04:53:13 -0400288 /* ops->peer_db_addr && */
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400289 /* ops->peer_db_read && */
290 ops->peer_db_set &&
291 /* ops->peer_db_clear && */
292 /* ops->peer_db_read_mask && */
293 /* ops->peer_db_set_mask && */
294 /* ops->peer_db_clear_mask && */
295 /* ops->spad_is_unsafe && */
296 ops->spad_count &&
297 ops->spad_read &&
298 ops->spad_write &&
Allen Hubbeafc54992016-03-21 04:53:13 -0400299 /* ops->peer_spad_addr && */
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400300 /* ops->peer_spad_read && */
301 ops->peer_spad_write &&
302 1;
303}
304
305/**
306 * struct ntb_client - client interested in ntb devices
307 * @drv: Linux driver object.
308 * @ops: See &ntb_client_ops.
309 */
310struct ntb_client {
311 struct device_driver drv;
312 const struct ntb_client_ops ops;
313};
314
315#define drv_ntb_client(__drv) container_of((__drv), struct ntb_client, drv)
316
317/**
318 * struct ntb_device - ntb device
319 * @dev: Linux device object.
320 * @pdev: Pci device entry of the ntb.
321 * @topo: Detected topology of the ntb.
322 * @ops: See &ntb_dev_ops.
323 * @ctx: See &ntb_ctx_ops.
324 * @ctx_ops: See &ntb_ctx_ops.
325 */
326struct ntb_dev {
327 struct device dev;
328 struct pci_dev *pdev;
329 enum ntb_topo topo;
330 const struct ntb_dev_ops *ops;
331 void *ctx;
332 const struct ntb_ctx_ops *ctx_ops;
333
334 /* private: */
335
336 /* synchronize setting, clearing, and calling ctx_ops */
337 spinlock_t ctx_lock;
338 /* block unregister until device is fully released */
339 struct completion released;
340};
341
342#define dev_ntb(__dev) container_of((__dev), struct ntb_dev, dev)
343
344/**
345 * ntb_register_client() - register a client for interest in ntb devices
346 * @client: Client context.
347 *
348 * The client will be added to the list of clients interested in ntb devices.
349 * The client will be notified of any ntb devices that are not already
350 * associated with a client, or if ntb devices are registered later.
351 *
352 * Return: Zero if the client is registered, otherwise an error number.
353 */
354#define ntb_register_client(client) \
355 __ntb_register_client((client), THIS_MODULE, KBUILD_MODNAME)
356
357int __ntb_register_client(struct ntb_client *client, struct module *mod,
358 const char *mod_name);
359
360/**
361 * ntb_unregister_client() - unregister a client for interest in ntb devices
362 * @client: Client context.
363 *
364 * The client will be removed from the list of clients interested in ntb
365 * devices. If any ntb devices are associated with the client, the client will
366 * be notified to remove those devices.
367 */
368void ntb_unregister_client(struct ntb_client *client);
369
370#define module_ntb_client(__ntb_client) \
371 module_driver(__ntb_client, ntb_register_client, \
372 ntb_unregister_client)
373
374/**
375 * ntb_register_device() - register a ntb device
376 * @ntb: NTB device context.
377 *
378 * The device will be added to the list of ntb devices. If any clients are
379 * interested in ntb devices, each client will be notified of the ntb device,
380 * until at most one client accepts the device.
381 *
382 * Return: Zero if the device is registered, otherwise an error number.
383 */
384int ntb_register_device(struct ntb_dev *ntb);
385
386/**
387 * ntb_register_device() - unregister a ntb device
388 * @ntb: NTB device context.
389 *
390 * The device will be removed from the list of ntb devices. If the ntb device
391 * is associated with a client, the client will be notified to remove the
392 * device.
393 */
394void ntb_unregister_device(struct ntb_dev *ntb);
395
396/**
397 * ntb_set_ctx() - associate a driver context with an ntb device
398 * @ntb: NTB device context.
399 * @ctx: Driver context.
400 * @ctx_ops: Driver context operations.
401 *
402 * Associate a driver context and operations with a ntb device. The context is
403 * provided by the client driver, and the driver may associate a different
404 * context with each ntb device.
405 *
406 * Return: Zero if the context is associated, otherwise an error number.
407 */
408int ntb_set_ctx(struct ntb_dev *ntb, void *ctx,
409 const struct ntb_ctx_ops *ctx_ops);
410
411/**
412 * ntb_clear_ctx() - disassociate any driver context from an ntb device
413 * @ntb: NTB device context.
414 *
415 * Clear any association that may exist between a driver context and the ntb
416 * device.
417 */
418void ntb_clear_ctx(struct ntb_dev *ntb);
419
420/**
421 * ntb_link_event() - notify driver context of a change in link status
422 * @ntb: NTB device context.
423 *
424 * Notify the driver context that the link status may have changed. The driver
425 * should call ntb_link_is_up() to get the current status.
426 */
427void ntb_link_event(struct ntb_dev *ntb);
428
429/**
430 * ntb_db_event() - notify driver context of a doorbell event
431 * @ntb: NTB device context.
432 * @vector: Interrupt vector number.
433 *
434 * Notify the driver context of a doorbell event. If hardware supports
435 * multiple interrupt vectors for doorbells, the vector number indicates which
436 * vector received the interrupt. The vector number is relative to the first
437 * vector used for doorbells, starting at zero, and must be less than
438 ** ntb_db_vector_count(). The driver may call ntb_db_read() to check which
439 * doorbell bits need service, and ntb_db_vector_mask() to determine which of
440 * those bits are associated with the vector number.
441 */
442void ntb_db_event(struct ntb_dev *ntb, int vector);
443
444/**
Serge Semin60934b22016-12-14 02:49:13 +0300445 * ntb_link_is_up() - get the current ntb link state
446 * @ntb: NTB device context.
447 * @speed: OUT - The link speed expressed as PCIe generation number.
448 * @width: OUT - The link width expressed as the number of PCIe lanes.
449 *
450 * Get the current state of the ntb link. It is recommended to query the link
451 * state once after every link event. It is safe to query the link state in
452 * the context of the link event callback.
453 *
454 * Return: One if the link is up, zero if the link is down, otherwise a
455 * negative value indicating the error number.
456 */
457static inline int ntb_link_is_up(struct ntb_dev *ntb,
458 enum ntb_speed *speed, enum ntb_width *width)
459{
460 return ntb->ops->link_is_up(ntb, speed, width);
461}
462
463/**
464 * ntb_link_enable() - enable the link on the secondary side of the ntb
465 * @ntb: NTB device context.
466 * @max_speed: The maximum link speed expressed as PCIe generation number.
467 * @max_width: The maximum link width expressed as the number of PCIe lanes.
468 *
469 * Enable the link on the secondary side of the ntb. This can only be done
470 * from the primary side of the ntb in primary or b2b topology. The ntb device
471 * should train the link to its maximum speed and width, or the requested speed
472 * and width, whichever is smaller, if supported.
473 *
474 * Return: Zero on success, otherwise an error number.
475 */
476static inline int ntb_link_enable(struct ntb_dev *ntb,
477 enum ntb_speed max_speed,
478 enum ntb_width max_width)
479{
480 return ntb->ops->link_enable(ntb, max_speed, max_width);
481}
482
483/**
484 * ntb_link_disable() - disable the link on the secondary side of the ntb
485 * @ntb: NTB device context.
486 *
487 * Disable the link on the secondary side of the ntb. This can only be
488 * done from the primary side of the ntb in primary or b2b topology. The ntb
489 * device should disable the link. Returning from this call must indicate that
490 * a barrier has passed, though with no more writes may pass in either
491 * direction across the link, except if this call returns an error number.
492 *
493 * Return: Zero on success, otherwise an error number.
494 */
495static inline int ntb_link_disable(struct ntb_dev *ntb)
496{
497 return ntb->ops->link_disable(ntb);
498}
499
500/**
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400501 * ntb_mw_count() - get the number of memory windows
502 * @ntb: NTB device context.
503 *
504 * Hardware and topology may support a different number of memory windows.
505 *
506 * Return: the number of memory windows.
507 */
508static inline int ntb_mw_count(struct ntb_dev *ntb)
509{
510 return ntb->ops->mw_count(ntb);
511}
512
513/**
514 * ntb_mw_get_range() - get the range of a memory window
515 * @ntb: NTB device context.
516 * @idx: Memory window number.
517 * @base: OUT - the base address for mapping the memory window
518 * @size: OUT - the size for mapping the memory window
519 * @align: OUT - the base alignment for translating the memory window
520 * @align_size: OUT - the size alignment for translating the memory window
521 *
522 * Get the range of a memory window. NULL may be given for any output
523 * parameter if the value is not needed. The base and size may be used for
524 * mapping the memory window, to access the peer memory. The alignment and
525 * size may be used for translating the memory window, for the peer to access
526 * memory on the local system.
527 *
528 * Return: Zero on success, otherwise an error number.
529 */
530static inline int ntb_mw_get_range(struct ntb_dev *ntb, int idx,
531 phys_addr_t *base, resource_size_t *size,
532 resource_size_t *align, resource_size_t *align_size)
533{
534 return ntb->ops->mw_get_range(ntb, idx, base, size,
535 align, align_size);
536}
537
538/**
539 * ntb_mw_set_trans() - set the translation of a memory window
540 * @ntb: NTB device context.
541 * @idx: Memory window number.
542 * @addr: The dma address local memory to expose to the peer.
543 * @size: The size of the local memory to expose to the peer.
544 *
545 * Set the translation of a memory window. The peer may access local memory
546 * through the window starting at the address, up to the size. The address
547 * must be aligned to the alignment specified by ntb_mw_get_range(). The size
548 * must be aligned to the size alignment specified by ntb_mw_get_range().
549 *
550 * Return: Zero on success, otherwise an error number.
551 */
552static inline int ntb_mw_set_trans(struct ntb_dev *ntb, int idx,
553 dma_addr_t addr, resource_size_t size)
554{
555 return ntb->ops->mw_set_trans(ntb, idx, addr, size);
556}
557
558/**
559 * ntb_mw_clear_trans() - clear the translation of a memory window
560 * @ntb: NTB device context.
561 * @idx: Memory window number.
562 *
563 * Clear the translation of a memory window. The peer may no longer access
564 * local memory through the window.
565 *
566 * Return: Zero on success, otherwise an error number.
567 */
568static inline int ntb_mw_clear_trans(struct ntb_dev *ntb, int idx)
569{
570 if (!ntb->ops->mw_clear_trans)
571 return ntb->ops->mw_set_trans(ntb, idx, 0, 0);
572
573 return ntb->ops->mw_clear_trans(ntb, idx);
574}
575
576/**
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400577 * ntb_db_is_unsafe() - check if it is safe to use hardware doorbell
578 * @ntb: NTB device context.
579 *
580 * It is possible for some ntb hardware to be affected by errata. Hardware
581 * drivers can advise clients to avoid using doorbells. Clients may ignore
582 * this advice, though caution is recommended.
583 *
584 * Return: Zero if it is safe to use doorbells, or One if it is not safe.
585 */
586static inline int ntb_db_is_unsafe(struct ntb_dev *ntb)
587{
588 if (!ntb->ops->db_is_unsafe)
589 return 0;
590
591 return ntb->ops->db_is_unsafe(ntb);
592}
593
594/**
595 * ntb_db_valid_mask() - get a mask of doorbell bits supported by the ntb
596 * @ntb: NTB device context.
597 *
598 * Hardware may support different number or arrangement of doorbell bits.
599 *
600 * Return: A mask of doorbell bits supported by the ntb.
601 */
602static inline u64 ntb_db_valid_mask(struct ntb_dev *ntb)
603{
604 return ntb->ops->db_valid_mask(ntb);
605}
606
607/**
608 * ntb_db_vector_count() - get the number of doorbell interrupt vectors
609 * @ntb: NTB device context.
610 *
611 * Hardware may support different number of interrupt vectors.
612 *
613 * Return: The number of doorbell interrupt vectors.
614 */
615static inline int ntb_db_vector_count(struct ntb_dev *ntb)
616{
617 if (!ntb->ops->db_vector_count)
618 return 1;
619
620 return ntb->ops->db_vector_count(ntb);
621}
622
623/**
624 * ntb_db_vector_mask() - get a mask of doorbell bits serviced by a vector
625 * @ntb: NTB device context.
626 * @vector: Doorbell vector number.
627 *
628 * Each interrupt vector may have a different number or arrangement of bits.
629 *
630 * Return: A mask of doorbell bits serviced by a vector.
631 */
632static inline u64 ntb_db_vector_mask(struct ntb_dev *ntb, int vector)
633{
634 if (!ntb->ops->db_vector_mask)
635 return ntb_db_valid_mask(ntb);
636
637 return ntb->ops->db_vector_mask(ntb, vector);
638}
639
640/**
641 * ntb_db_read() - read the local doorbell register
642 * @ntb: NTB device context.
643 *
644 * Read the local doorbell register, and return the bits that are set.
645 *
646 * Return: The bits currently set in the local doorbell register.
647 */
648static inline u64 ntb_db_read(struct ntb_dev *ntb)
649{
650 return ntb->ops->db_read(ntb);
651}
652
653/**
654 * ntb_db_set() - set bits in the local doorbell register
655 * @ntb: NTB device context.
656 * @db_bits: Doorbell bits to set.
657 *
658 * Set bits in the local doorbell register, which may generate a local doorbell
659 * interrupt. Bits that were already set must remain set.
660 *
661 * This is unusual, and hardware may not support it.
662 *
663 * Return: Zero on success, otherwise an error number.
664 */
665static inline int ntb_db_set(struct ntb_dev *ntb, u64 db_bits)
666{
667 if (!ntb->ops->db_set)
668 return -EINVAL;
669
670 return ntb->ops->db_set(ntb, db_bits);
671}
672
673/**
674 * ntb_db_clear() - clear bits in the local doorbell register
675 * @ntb: NTB device context.
676 * @db_bits: Doorbell bits to clear.
677 *
678 * Clear bits in the local doorbell register, arming the bits for the next
679 * doorbell.
680 *
681 * Return: Zero on success, otherwise an error number.
682 */
683static inline int ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
684{
685 return ntb->ops->db_clear(ntb, db_bits);
686}
687
688/**
689 * ntb_db_read_mask() - read the local doorbell mask
690 * @ntb: NTB device context.
691 *
692 * Read the local doorbell mask register, and return the bits that are set.
693 *
694 * This is unusual, though hardware is likely to support it.
695 *
696 * Return: The bits currently set in the local doorbell mask register.
697 */
698static inline u64 ntb_db_read_mask(struct ntb_dev *ntb)
699{
700 if (!ntb->ops->db_read_mask)
701 return 0;
702
703 return ntb->ops->db_read_mask(ntb);
704}
705
706/**
707 * ntb_db_set_mask() - set bits in the local doorbell mask
708 * @ntb: NTB device context.
709 * @db_bits: Doorbell mask bits to set.
710 *
711 * Set bits in the local doorbell mask register, preventing doorbell interrupts
712 * from being generated for those doorbell bits. Bits that were already set
713 * must remain set.
714 *
715 * Return: Zero on success, otherwise an error number.
716 */
717static inline int ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
718{
719 return ntb->ops->db_set_mask(ntb, db_bits);
720}
721
722/**
723 * ntb_db_clear_mask() - clear bits in the local doorbell mask
724 * @ntb: NTB device context.
725 * @db_bits: Doorbell bits to clear.
726 *
727 * Clear bits in the local doorbell mask register, allowing doorbell interrupts
728 * from being generated for those doorbell bits. If a doorbell bit is already
729 * set at the time the mask is cleared, and the corresponding mask bit is
730 * changed from set to clear, then the ntb driver must ensure that
731 * ntb_db_event() is called. If the hardware does not generate the interrupt
732 * on clearing the mask bit, then the driver must call ntb_db_event() anyway.
733 *
734 * Return: Zero on success, otherwise an error number.
735 */
736static inline int ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
737{
738 return ntb->ops->db_clear_mask(ntb, db_bits);
739}
740
741/**
742 * ntb_peer_db_addr() - address and size of the peer doorbell register
743 * @ntb: NTB device context.
744 * @db_addr: OUT - The address of the peer doorbell register.
745 * @db_size: OUT - The number of bytes to write the peer doorbell register.
746 *
747 * Return the address of the peer doorbell register. This may be used, for
748 * example, by drivers that offload memory copy operations to a dma engine.
749 * The drivers may wish to ring the peer doorbell at the completion of memory
750 * copy operations. For efficiency, and to simplify ordering of operations
751 * between the dma memory copies and the ringing doorbell, the driver may
752 * append one additional dma memory copy with the doorbell register as the
753 * destination, after the memory copy operations.
754 *
755 * Return: Zero on success, otherwise an error number.
756 */
757static inline int ntb_peer_db_addr(struct ntb_dev *ntb,
758 phys_addr_t *db_addr,
759 resource_size_t *db_size)
760{
Allen Hubbeafc54992016-03-21 04:53:13 -0400761 if (!ntb->ops->peer_db_addr)
762 return -EINVAL;
763
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400764 return ntb->ops->peer_db_addr(ntb, db_addr, db_size);
765}
766
767/**
768 * ntb_peer_db_read() - read the peer doorbell register
769 * @ntb: NTB device context.
770 *
771 * Read the peer doorbell register, and return the bits that are set.
772 *
773 * This is unusual, and hardware may not support it.
774 *
775 * Return: The bits currently set in the peer doorbell register.
776 */
777static inline u64 ntb_peer_db_read(struct ntb_dev *ntb)
778{
779 if (!ntb->ops->peer_db_read)
780 return 0;
781
782 return ntb->ops->peer_db_read(ntb);
783}
784
785/**
786 * ntb_peer_db_set() - set bits in the peer doorbell register
787 * @ntb: NTB device context.
788 * @db_bits: Doorbell bits to set.
789 *
790 * Set bits in the peer doorbell register, which may generate a peer doorbell
791 * interrupt. Bits that were already set must remain set.
792 *
793 * Return: Zero on success, otherwise an error number.
794 */
795static inline int ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
796{
797 return ntb->ops->peer_db_set(ntb, db_bits);
798}
799
800/**
Allen Hubbe86663c92015-07-15 12:43:21 -0400801 * ntb_peer_db_clear() - clear bits in the peer doorbell register
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400802 * @ntb: NTB device context.
803 * @db_bits: Doorbell bits to clear.
804 *
805 * Clear bits in the peer doorbell register, arming the bits for the next
806 * doorbell.
807 *
808 * This is unusual, and hardware may not support it.
809 *
810 * Return: Zero on success, otherwise an error number.
811 */
812static inline int ntb_peer_db_clear(struct ntb_dev *ntb, u64 db_bits)
813{
814 if (!ntb->ops->db_clear)
815 return -EINVAL;
816
817 return ntb->ops->peer_db_clear(ntb, db_bits);
818}
819
820/**
821 * ntb_peer_db_read_mask() - read the peer doorbell mask
822 * @ntb: NTB device context.
823 *
824 * Read the peer doorbell mask register, and return the bits that are set.
825 *
826 * This is unusual, and hardware may not support it.
827 *
828 * Return: The bits currently set in the peer doorbell mask register.
829 */
830static inline u64 ntb_peer_db_read_mask(struct ntb_dev *ntb)
831{
832 if (!ntb->ops->db_read_mask)
833 return 0;
834
835 return ntb->ops->peer_db_read_mask(ntb);
836}
837
838/**
839 * ntb_peer_db_set_mask() - set bits in the peer doorbell mask
840 * @ntb: NTB device context.
841 * @db_bits: Doorbell mask bits to set.
842 *
843 * Set bits in the peer doorbell mask register, preventing doorbell interrupts
844 * from being generated for those doorbell bits. Bits that were already set
845 * must remain set.
846 *
847 * This is unusual, and hardware may not support it.
848 *
849 * Return: Zero on success, otherwise an error number.
850 */
851static inline int ntb_peer_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
852{
853 if (!ntb->ops->db_set_mask)
854 return -EINVAL;
855
856 return ntb->ops->peer_db_set_mask(ntb, db_bits);
857}
858
859/**
860 * ntb_peer_db_clear_mask() - clear bits in the peer doorbell mask
861 * @ntb: NTB device context.
862 * @db_bits: Doorbell bits to clear.
863 *
864 * Clear bits in the peer doorbell mask register, allowing doorbell interrupts
865 * from being generated for those doorbell bits. If the hardware does not
866 * generate the interrupt on clearing the mask bit, then the driver should not
867 * implement this function!
868 *
869 * This is unusual, and hardware may not support it.
870 *
871 * Return: Zero on success, otherwise an error number.
872 */
873static inline int ntb_peer_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
874{
875 if (!ntb->ops->db_clear_mask)
876 return -EINVAL;
877
878 return ntb->ops->peer_db_clear_mask(ntb, db_bits);
879}
880
881/**
882 * ntb_spad_is_unsafe() - check if it is safe to use the hardware scratchpads
883 * @ntb: NTB device context.
884 *
885 * It is possible for some ntb hardware to be affected by errata. Hardware
886 * drivers can advise clients to avoid using scratchpads. Clients may ignore
887 * this advice, though caution is recommended.
888 *
889 * Return: Zero if it is safe to use scratchpads, or One if it is not safe.
890 */
891static inline int ntb_spad_is_unsafe(struct ntb_dev *ntb)
892{
893 if (!ntb->ops->spad_is_unsafe)
894 return 0;
895
896 return ntb->ops->spad_is_unsafe(ntb);
897}
898
899/**
Aaron Sierra74dcba32016-12-06 19:09:16 -0600900 * ntb_spad_count() - get the number of scratchpads
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400901 * @ntb: NTB device context.
902 *
903 * Hardware and topology may support a different number of scratchpads.
904 *
905 * Return: the number of scratchpads.
906 */
907static inline int ntb_spad_count(struct ntb_dev *ntb)
908{
909 return ntb->ops->spad_count(ntb);
910}
911
912/**
913 * ntb_spad_read() - read the local scratchpad register
914 * @ntb: NTB device context.
915 * @idx: Scratchpad index.
916 *
917 * Read the local scratchpad register, and return the value.
918 *
919 * Return: The value of the local scratchpad register.
920 */
921static inline u32 ntb_spad_read(struct ntb_dev *ntb, int idx)
922{
923 return ntb->ops->spad_read(ntb, idx);
924}
925
926/**
927 * ntb_spad_write() - write the local scratchpad register
928 * @ntb: NTB device context.
929 * @idx: Scratchpad index.
930 * @val: Scratchpad value.
931 *
932 * Write the value to the local scratchpad register.
933 *
934 * Return: Zero on success, otherwise an error number.
935 */
936static inline int ntb_spad_write(struct ntb_dev *ntb, int idx, u32 val)
937{
938 return ntb->ops->spad_write(ntb, idx, val);
939}
940
941/**
942 * ntb_peer_spad_addr() - address of the peer scratchpad register
943 * @ntb: NTB device context.
944 * @idx: Scratchpad index.
945 * @spad_addr: OUT - The address of the peer scratchpad register.
946 *
947 * Return the address of the peer doorbell register. This may be used, for
948 * example, by drivers that offload memory copy operations to a dma engine.
949 *
950 * Return: Zero on success, otherwise an error number.
951 */
952static inline int ntb_peer_spad_addr(struct ntb_dev *ntb, int idx,
953 phys_addr_t *spad_addr)
954{
Allen Hubbeafc54992016-03-21 04:53:13 -0400955 if (!ntb->ops->peer_spad_addr)
956 return -EINVAL;
957
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400958 return ntb->ops->peer_spad_addr(ntb, idx, spad_addr);
959}
960
961/**
962 * ntb_peer_spad_read() - read the peer scratchpad register
963 * @ntb: NTB device context.
964 * @idx: Scratchpad index.
965 *
966 * Read the peer scratchpad register, and return the value.
967 *
968 * Return: The value of the local scratchpad register.
969 */
970static inline u32 ntb_peer_spad_read(struct ntb_dev *ntb, int idx)
971{
Steven Wahl5c43c522016-12-08 17:02:28 +0000972 if (!ntb->ops->peer_spad_read)
973 return 0;
974
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400975 return ntb->ops->peer_spad_read(ntb, idx);
976}
977
978/**
979 * ntb_peer_spad_write() - write the peer scratchpad register
980 * @ntb: NTB device context.
981 * @idx: Scratchpad index.
982 * @val: Scratchpad value.
983 *
984 * Write the value to the peer scratchpad register.
985 *
986 * Return: Zero on success, otherwise an error number.
987 */
988static inline int ntb_peer_spad_write(struct ntb_dev *ntb, int idx, u32 val)
989{
990 return ntb->ops->peer_spad_write(ntb, idx, val);
991}
992
993#endif