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.
7 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
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.
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.
20 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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
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.
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.
48 * PCIe NTB Linux driver
50 * Contact Information:
51 * Allen Hubbe <Allen.Hubbe@emc.com>
57 #include <linux/completion.h>
58 #include <linux/device.h>
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.
80 static inline int ntb_topo_is_b2b(enum ntb_topo topo)
83 case NTB_TOPO_B2B_USD:
84 case NTB_TOPO_B2B_DSD:
90 static inline char *ntb_topo_string(enum ntb_topo 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";
99 return "NTB_TOPO_INVALID";
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.
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.
143 * struct ntb_client_ops - ntb client operations
144 * @probe: Notify client of a new device.
145 * @remove: Notify client to remove a device.
147 struct 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);
152 static inline int ntb_client_ops_is_valid(const struct ntb_client_ops *ops)
154 /* commented callbacks are not required: */
162 * struct ntb_ctx_ops - ntb driver context operations
163 * @link_event: See ntb_link_event().
164 * @db_event: See ntb_db_event().
167 void (*link_event)(void *ctx);
168 void (*db_event)(void *ctx, int db_vector);
171 static inline int ntb_ctx_ops_is_valid(const struct ntb_ctx_ops *ops)
173 /* commented callbacks are not required: */
175 /* ops->link_event && */
176 /* ops->db_event && */
181 * struct ntb_ctx_ops - ntb device operations
182 * @mw_count: See ntb_mw_count().
183 * @mw_get_range: See ntb_mw_get_range().
184 * @mw_set_trans: See ntb_mw_set_trans().
185 * @mw_clear_trans: See ntb_mw_clear_trans().
186 * @link_is_up: See ntb_link_is_up().
187 * @link_enable: See ntb_link_enable().
188 * @link_disable: See ntb_link_disable().
189 * @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().
215 int (*mw_count)(struct ntb_dev *ntb);
216 int (*mw_get_range)(struct ntb_dev *ntb, int idx,
217 phys_addr_t *base, resource_size_t *size,
218 resource_size_t *align, resource_size_t *align_size);
219 int (*mw_set_trans)(struct ntb_dev *ntb, int idx,
220 dma_addr_t addr, resource_size_t size);
221 int (*mw_clear_trans)(struct ntb_dev *ntb, int idx);
223 int (*link_is_up)(struct ntb_dev *ntb,
224 enum ntb_speed *speed, enum ntb_width *width);
225 int (*link_enable)(struct ntb_dev *ntb,
226 enum ntb_speed max_speed, enum ntb_width max_width);
227 int (*link_disable)(struct ntb_dev *ntb);
229 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);
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);
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);
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);
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);
252 int (*spad_is_unsafe)(struct ntb_dev *ntb);
253 int (*spad_count)(struct ntb_dev *ntb);
255 u32 (*spad_read)(struct ntb_dev *ntb, int idx);
256 int (*spad_write)(struct ntb_dev *ntb, int idx, u32 val);
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);
264 static inline int ntb_dev_ops_is_valid(const struct ntb_dev_ops *ops)
266 /* commented callbacks are not required: */
271 /* ops->mw_clear_trans && */
275 /* ops->db_is_unsafe && */
276 ops->db_valid_mask &&
278 /* both set, or both unset */
279 (!ops->db_vector_count == !ops->db_vector_mask) &&
284 /* ops->db_read_mask && */
286 ops->db_clear_mask &&
288 /* ops->peer_db_read && */
290 /* ops->peer_db_clear && */
291 /* ops->peer_db_read_mask && */
292 /* ops->peer_db_set_mask && */
293 /* ops->peer_db_clear_mask && */
294 /* ops->spad_is_unsafe && */
298 ops->peer_spad_addr &&
299 /* ops->peer_spad_read && */
300 ops->peer_spad_write &&
305 * struct ntb_client - client interested in ntb devices
306 * @drv: Linux driver object.
307 * @ops: See &ntb_client_ops.
310 struct device_driver drv;
311 const struct ntb_client_ops ops;
314 #define drv_ntb_client(__drv) container_of((__drv), struct ntb_client, drv)
317 * struct ntb_device - ntb device
318 * @dev: Linux device object.
319 * @pdev: Pci device entry of the ntb.
320 * @topo: Detected topology of the ntb.
321 * @ops: See &ntb_dev_ops.
322 * @ctx: See &ntb_ctx_ops.
323 * @ctx_ops: See &ntb_ctx_ops.
327 struct pci_dev *pdev;
329 const struct ntb_dev_ops *ops;
331 const struct ntb_ctx_ops *ctx_ops;
335 /* synchronize setting, clearing, and calling ctx_ops */
337 /* block unregister until device is fully released */
338 struct completion released;
341 #define dev_ntb(__dev) container_of((__dev), struct ntb_dev, dev)
344 * ntb_register_client() - register a client for interest in ntb devices
345 * @client: Client context.
347 * The client will be added to the list of clients interested in ntb devices.
348 * The client will be notified of any ntb devices that are not already
349 * associated with a client, or if ntb devices are registered later.
351 * Return: Zero if the client is registered, otherwise an error number.
353 #define ntb_register_client(client) \
354 __ntb_register_client((client), THIS_MODULE, KBUILD_MODNAME)
356 int __ntb_register_client(struct ntb_client *client, struct module *mod,
357 const char *mod_name);
360 * ntb_unregister_client() - unregister a client for interest in ntb devices
361 * @client: Client context.
363 * The client will be removed from the list of clients interested in ntb
364 * devices. If any ntb devices are associated with the client, the client will
365 * be notified to remove those devices.
367 void ntb_unregister_client(struct ntb_client *client);
369 #define module_ntb_client(__ntb_client) \
370 module_driver(__ntb_client, ntb_register_client, \
371 ntb_unregister_client)
374 * ntb_register_device() - register a ntb device
375 * @ntb: NTB device context.
377 * The device will be added to the list of ntb devices. If any clients are
378 * interested in ntb devices, each client will be notified of the ntb device,
379 * until at most one client accepts the device.
381 * Return: Zero if the device is registered, otherwise an error number.
383 int ntb_register_device(struct ntb_dev *ntb);
386 * ntb_register_device() - unregister a ntb device
387 * @ntb: NTB device context.
389 * The device will be removed from the list of ntb devices. If the ntb device
390 * is associated with a client, the client will be notified to remove the
393 void ntb_unregister_device(struct ntb_dev *ntb);
396 * ntb_set_ctx() - associate a driver context with an ntb device
397 * @ntb: NTB device context.
398 * @ctx: Driver context.
399 * @ctx_ops: Driver context operations.
401 * Associate a driver context and operations with a ntb device. The context is
402 * provided by the client driver, and the driver may associate a different
403 * context with each ntb device.
405 * Return: Zero if the context is associated, otherwise an error number.
407 int ntb_set_ctx(struct ntb_dev *ntb, void *ctx,
408 const struct ntb_ctx_ops *ctx_ops);
411 * ntb_clear_ctx() - disassociate any driver context from an ntb device
412 * @ntb: NTB device context.
414 * Clear any association that may exist between a driver context and the ntb
417 void ntb_clear_ctx(struct ntb_dev *ntb);
420 * ntb_link_event() - notify driver context of a change in link status
421 * @ntb: NTB device context.
423 * Notify the driver context that the link status may have changed. The driver
424 * should call ntb_link_is_up() to get the current status.
426 void ntb_link_event(struct ntb_dev *ntb);
429 * ntb_db_event() - notify driver context of a doorbell event
430 * @ntb: NTB device context.
431 * @vector: Interrupt vector number.
433 * Notify the driver context of a doorbell event. If hardware supports
434 * multiple interrupt vectors for doorbells, the vector number indicates which
435 * vector received the interrupt. The vector number is relative to the first
436 * vector used for doorbells, starting at zero, and must be less than
437 ** ntb_db_vector_count(). The driver may call ntb_db_read() to check which
438 * doorbell bits need service, and ntb_db_vector_mask() to determine which of
439 * those bits are associated with the vector number.
441 void ntb_db_event(struct ntb_dev *ntb, int vector);
444 * ntb_mw_count() - get the number of memory windows
445 * @ntb: NTB device context.
447 * Hardware and topology may support a different number of memory windows.
449 * Return: the number of memory windows.
451 static inline int ntb_mw_count(struct ntb_dev *ntb)
453 return ntb->ops->mw_count(ntb);
457 * ntb_mw_get_range() - get the range of a memory window
458 * @ntb: NTB device context.
459 * @idx: Memory window number.
460 * @base: OUT - the base address for mapping the memory window
461 * @size: OUT - the size for mapping the memory window
462 * @align: OUT - the base alignment for translating the memory window
463 * @align_size: OUT - the size alignment for translating the memory window
465 * Get the range of a memory window. NULL may be given for any output
466 * parameter if the value is not needed. The base and size may be used for
467 * mapping the memory window, to access the peer memory. The alignment and
468 * size may be used for translating the memory window, for the peer to access
469 * memory on the local system.
471 * Return: Zero on success, otherwise an error number.
473 static inline int ntb_mw_get_range(struct ntb_dev *ntb, int idx,
474 phys_addr_t *base, resource_size_t *size,
475 resource_size_t *align, resource_size_t *align_size)
477 return ntb->ops->mw_get_range(ntb, idx, base, size,
482 * ntb_mw_set_trans() - set the translation of a memory window
483 * @ntb: NTB device context.
484 * @idx: Memory window number.
485 * @addr: The dma address local memory to expose to the peer.
486 * @size: The size of the local memory to expose to the peer.
488 * Set the translation of a memory window. The peer may access local memory
489 * through the window starting at the address, up to the size. The address
490 * must be aligned to the alignment specified by ntb_mw_get_range(). The size
491 * must be aligned to the size alignment specified by ntb_mw_get_range().
493 * Return: Zero on success, otherwise an error number.
495 static inline int ntb_mw_set_trans(struct ntb_dev *ntb, int idx,
496 dma_addr_t addr, resource_size_t size)
498 return ntb->ops->mw_set_trans(ntb, idx, addr, size);
502 * ntb_mw_clear_trans() - clear the translation of a memory window
503 * @ntb: NTB device context.
504 * @idx: Memory window number.
506 * Clear the translation of a memory window. The peer may no longer access
507 * local memory through the window.
509 * Return: Zero on success, otherwise an error number.
511 static inline int ntb_mw_clear_trans(struct ntb_dev *ntb, int idx)
513 if (!ntb->ops->mw_clear_trans)
514 return ntb->ops->mw_set_trans(ntb, idx, 0, 0);
516 return ntb->ops->mw_clear_trans(ntb, idx);
520 * ntb_link_is_up() - get the current ntb link state
521 * @ntb: NTB device context.
522 * @speed: OUT - The link speed expressed as PCIe generation number.
523 * @width: OUT - The link width expressed as the number of PCIe lanes.
525 * Set the translation of a memory window. The peer may access local memory
526 * through the window starting at the address, up to the size. The address
527 * must be aligned to the alignment specified by ntb_mw_get_range(). The size
528 * must be aligned to the size alignment specified by ntb_mw_get_range().
530 * Return: One if the link is up, zero if the link is down, otherwise a
531 * negative value indicating the error number.
533 static inline int ntb_link_is_up(struct ntb_dev *ntb,
534 enum ntb_speed *speed, enum ntb_width *width)
536 return ntb->ops->link_is_up(ntb, speed, width);
540 * ntb_link_enable() - enable the link on the secondary side of the ntb
541 * @ntb: NTB device context.
542 * @max_speed: The maximum link speed expressed as PCIe generation number.
543 * @max_width: The maximum link width expressed as the number of PCIe lanes.
545 * Enable the link on the secondary side of the ntb. This can only be done
546 * from the primary side of the ntb in primary or b2b topology. The ntb device
547 * should train the link to its maximum speed and width, or the requested speed
548 * and width, whichever is smaller, if supported.
550 * Return: Zero on success, otherwise an error number.
552 static inline int ntb_link_enable(struct ntb_dev *ntb,
553 enum ntb_speed max_speed,
554 enum ntb_width max_width)
556 return ntb->ops->link_enable(ntb, max_speed, max_width);
560 * ntb_link_disable() - disable the link on the secondary side of the ntb
561 * @ntb: NTB device context.
563 * Disable the link on the secondary side of the ntb. This can only be
564 * done from the primary side of the ntb in primary or b2b topology. The ntb
565 * device should disable the link. Returning from this call must indicate that
566 * a barrier has passed, though with no more writes may pass in either
567 * direction across the link, except if this call returns an error number.
569 * Return: Zero on success, otherwise an error number.
571 static inline int ntb_link_disable(struct ntb_dev *ntb)
573 return ntb->ops->link_disable(ntb);
577 * ntb_db_is_unsafe() - check if it is safe to use hardware doorbell
578 * @ntb: NTB device context.
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.
584 * Return: Zero if it is safe to use doorbells, or One if it is not safe.
586 static inline int ntb_db_is_unsafe(struct ntb_dev *ntb)
588 if (!ntb->ops->db_is_unsafe)
591 return ntb->ops->db_is_unsafe(ntb);
595 * ntb_db_valid_mask() - get a mask of doorbell bits supported by the ntb
596 * @ntb: NTB device context.
598 * Hardware may support different number or arrangement of doorbell bits.
600 * Return: A mask of doorbell bits supported by the ntb.
602 static inline u64 ntb_db_valid_mask(struct ntb_dev *ntb)
604 return ntb->ops->db_valid_mask(ntb);
608 * ntb_db_vector_count() - get the number of doorbell interrupt vectors
609 * @ntb: NTB device context.
611 * Hardware may support different number of interrupt vectors.
613 * Return: The number of doorbell interrupt vectors.
615 static inline int ntb_db_vector_count(struct ntb_dev *ntb)
617 if (!ntb->ops->db_vector_count)
620 return ntb->ops->db_vector_count(ntb);
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.
628 * Each interrupt vector may have a different number or arrangement of bits.
630 * Return: A mask of doorbell bits serviced by a vector.
632 static inline u64 ntb_db_vector_mask(struct ntb_dev *ntb, int vector)
634 if (!ntb->ops->db_vector_mask)
635 return ntb_db_valid_mask(ntb);
637 return ntb->ops->db_vector_mask(ntb, vector);
641 * ntb_db_read() - read the local doorbell register
642 * @ntb: NTB device context.
644 * Read the local doorbell register, and return the bits that are set.
646 * Return: The bits currently set in the local doorbell register.
648 static inline u64 ntb_db_read(struct ntb_dev *ntb)
650 return ntb->ops->db_read(ntb);
654 * ntb_db_set() - set bits in the local doorbell register
655 * @ntb: NTB device context.
656 * @db_bits: Doorbell bits to set.
658 * Set bits in the local doorbell register, which may generate a local doorbell
659 * interrupt. Bits that were already set must remain set.
661 * This is unusual, and hardware may not support it.
663 * Return: Zero on success, otherwise an error number.
665 static inline int ntb_db_set(struct ntb_dev *ntb, u64 db_bits)
667 if (!ntb->ops->db_set)
670 return ntb->ops->db_set(ntb, db_bits);
674 * ntb_db_clear() - clear bits in the local doorbell register
675 * @ntb: NTB device context.
676 * @db_bits: Doorbell bits to clear.
678 * Clear bits in the local doorbell register, arming the bits for the next
681 * Return: Zero on success, otherwise an error number.
683 static inline int ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
685 return ntb->ops->db_clear(ntb, db_bits);
689 * ntb_db_read_mask() - read the local doorbell mask
690 * @ntb: NTB device context.
692 * Read the local doorbell mask register, and return the bits that are set.
694 * This is unusual, though hardware is likely to support it.
696 * Return: The bits currently set in the local doorbell mask register.
698 static inline u64 ntb_db_read_mask(struct ntb_dev *ntb)
700 if (!ntb->ops->db_read_mask)
703 return ntb->ops->db_read_mask(ntb);
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.
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
715 * Return: Zero on success, otherwise an error number.
717 static inline int ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
719 return ntb->ops->db_set_mask(ntb, db_bits);
723 * ntb_db_clear_mask() - clear bits in the local doorbell mask
724 * @ntb: NTB device context.
725 * @db_bits: Doorbell bits to clear.
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.
734 * Return: Zero on success, otherwise an error number.
736 static inline int ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
738 return ntb->ops->db_clear_mask(ntb, db_bits);
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.
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.
755 * Return: Zero on success, otherwise an error number.
757 static inline int ntb_peer_db_addr(struct ntb_dev *ntb,
758 phys_addr_t *db_addr,
759 resource_size_t *db_size)
761 return ntb->ops->peer_db_addr(ntb, db_addr, db_size);
765 * ntb_peer_db_read() - read the peer doorbell register
766 * @ntb: NTB device context.
768 * Read the peer doorbell register, and return the bits that are set.
770 * This is unusual, and hardware may not support it.
772 * Return: The bits currently set in the peer doorbell register.
774 static inline u64 ntb_peer_db_read(struct ntb_dev *ntb)
776 if (!ntb->ops->peer_db_read)
779 return ntb->ops->peer_db_read(ntb);
783 * ntb_peer_db_set() - set bits in the peer doorbell register
784 * @ntb: NTB device context.
785 * @db_bits: Doorbell bits to set.
787 * Set bits in the peer doorbell register, which may generate a peer doorbell
788 * interrupt. Bits that were already set must remain set.
790 * Return: Zero on success, otherwise an error number.
792 static inline int ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
794 return ntb->ops->peer_db_set(ntb, db_bits);
798 * ntb_peer_db_clear() - clear bits in the local doorbell register
799 * @ntb: NTB device context.
800 * @db_bits: Doorbell bits to clear.
802 * Clear bits in the peer doorbell register, arming the bits for the next
805 * This is unusual, and hardware may not support it.
807 * Return: Zero on success, otherwise an error number.
809 static inline int ntb_peer_db_clear(struct ntb_dev *ntb, u64 db_bits)
811 if (!ntb->ops->db_clear)
814 return ntb->ops->peer_db_clear(ntb, db_bits);
818 * ntb_peer_db_read_mask() - read the peer doorbell mask
819 * @ntb: NTB device context.
821 * Read the peer doorbell mask register, and return the bits that are set.
823 * This is unusual, and hardware may not support it.
825 * Return: The bits currently set in the peer doorbell mask register.
827 static inline u64 ntb_peer_db_read_mask(struct ntb_dev *ntb)
829 if (!ntb->ops->db_read_mask)
832 return ntb->ops->peer_db_read_mask(ntb);
836 * ntb_peer_db_set_mask() - set bits in the peer doorbell mask
837 * @ntb: NTB device context.
838 * @db_bits: Doorbell mask bits to set.
840 * Set bits in the peer doorbell mask register, preventing doorbell interrupts
841 * from being generated for those doorbell bits. Bits that were already set
844 * This is unusual, and hardware may not support it.
846 * Return: Zero on success, otherwise an error number.
848 static inline int ntb_peer_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
850 if (!ntb->ops->db_set_mask)
853 return ntb->ops->peer_db_set_mask(ntb, db_bits);
857 * ntb_peer_db_clear_mask() - clear bits in the peer doorbell mask
858 * @ntb: NTB device context.
859 * @db_bits: Doorbell bits to clear.
861 * Clear bits in the peer doorbell mask register, allowing doorbell interrupts
862 * from being generated for those doorbell bits. If the hardware does not
863 * generate the interrupt on clearing the mask bit, then the driver should not
864 * implement this function!
866 * This is unusual, and hardware may not support it.
868 * Return: Zero on success, otherwise an error number.
870 static inline int ntb_peer_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
872 if (!ntb->ops->db_clear_mask)
875 return ntb->ops->peer_db_clear_mask(ntb, db_bits);
879 * ntb_spad_is_unsafe() - check if it is safe to use the hardware scratchpads
880 * @ntb: NTB device context.
882 * It is possible for some ntb hardware to be affected by errata. Hardware
883 * drivers can advise clients to avoid using scratchpads. Clients may ignore
884 * this advice, though caution is recommended.
886 * Return: Zero if it is safe to use scratchpads, or One if it is not safe.
888 static inline int ntb_spad_is_unsafe(struct ntb_dev *ntb)
890 if (!ntb->ops->spad_is_unsafe)
893 return ntb->ops->spad_is_unsafe(ntb);
897 * ntb_mw_count() - get the number of scratchpads
898 * @ntb: NTB device context.
900 * Hardware and topology may support a different number of scratchpads.
902 * Return: the number of scratchpads.
904 static inline int ntb_spad_count(struct ntb_dev *ntb)
906 return ntb->ops->spad_count(ntb);
910 * ntb_spad_read() - read the local scratchpad register
911 * @ntb: NTB device context.
912 * @idx: Scratchpad index.
914 * Read the local scratchpad register, and return the value.
916 * Return: The value of the local scratchpad register.
918 static inline u32 ntb_spad_read(struct ntb_dev *ntb, int idx)
920 return ntb->ops->spad_read(ntb, idx);
924 * ntb_spad_write() - write the local scratchpad register
925 * @ntb: NTB device context.
926 * @idx: Scratchpad index.
927 * @val: Scratchpad value.
929 * Write the value to the local scratchpad register.
931 * Return: Zero on success, otherwise an error number.
933 static inline int ntb_spad_write(struct ntb_dev *ntb, int idx, u32 val)
935 return ntb->ops->spad_write(ntb, idx, val);
939 * ntb_peer_spad_addr() - address of the peer scratchpad register
940 * @ntb: NTB device context.
941 * @idx: Scratchpad index.
942 * @spad_addr: OUT - The address of the peer scratchpad register.
944 * Return the address of the peer doorbell register. This may be used, for
945 * example, by drivers that offload memory copy operations to a dma engine.
947 * Return: Zero on success, otherwise an error number.
949 static inline int ntb_peer_spad_addr(struct ntb_dev *ntb, int idx,
950 phys_addr_t *spad_addr)
952 return ntb->ops->peer_spad_addr(ntb, idx, spad_addr);
956 * ntb_peer_spad_read() - read the peer scratchpad register
957 * @ntb: NTB device context.
958 * @idx: Scratchpad index.
960 * Read the peer scratchpad register, and return the value.
962 * Return: The value of the local scratchpad register.
964 static inline u32 ntb_peer_spad_read(struct ntb_dev *ntb, int idx)
966 return ntb->ops->peer_spad_read(ntb, idx);
970 * ntb_peer_spad_write() - write the peer scratchpad register
971 * @ntb: NTB device context.
972 * @idx: Scratchpad index.
973 * @val: Scratchpad value.
975 * Write the value to the peer scratchpad register.
977 * Return: Zero on success, otherwise an error number.
979 static inline int ntb_peer_spad_write(struct ntb_dev *ntb, int idx, u32 val)
981 return ntb->ops->peer_spad_write(ntb, idx, val);