blob: d23483bae6f3947250c14d7bce90e843f1f65f06 [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/**
Serge Semin1e530112016-12-14 02:49:14 +0300143 * enum ntb_default_port - NTB default port number
144 * @NTB_PORT_PRI_USD: Default port of the NTB_TOPO_PRI/NTB_TOPO_B2B_USD
145 * topologies
146 * @NTB_PORT_SEC_DSD: Default port of the NTB_TOPO_SEC/NTB_TOPO_B2B_DSD
147 * topologies
148 */
149enum ntb_default_port {
150 NTB_PORT_PRI_USD,
151 NTB_PORT_SEC_DSD
152};
153#define NTB_DEF_PEER_CNT (1)
154#define NTB_DEF_PEER_IDX (0)
155
156/**
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400157 * struct ntb_client_ops - ntb client operations
158 * @probe: Notify client of a new device.
159 * @remove: Notify client to remove a device.
160 */
161struct ntb_client_ops {
162 int (*probe)(struct ntb_client *client, struct ntb_dev *ntb);
163 void (*remove)(struct ntb_client *client, struct ntb_dev *ntb);
164};
165
166static inline int ntb_client_ops_is_valid(const struct ntb_client_ops *ops)
167{
168 /* commented callbacks are not required: */
169 return
170 ops->probe &&
171 ops->remove &&
172 1;
173}
174
175/**
176 * struct ntb_ctx_ops - ntb driver context operations
177 * @link_event: See ntb_link_event().
178 * @db_event: See ntb_db_event().
179 */
180struct ntb_ctx_ops {
181 void (*link_event)(void *ctx);
182 void (*db_event)(void *ctx, int db_vector);
183};
184
185static inline int ntb_ctx_ops_is_valid(const struct ntb_ctx_ops *ops)
186{
187 /* commented callbacks are not required: */
188 return
189 /* ops->link_event && */
190 /* ops->db_event && */
191 1;
192}
193
194/**
195 * struct ntb_ctx_ops - ntb device operations
Serge Semin1e530112016-12-14 02:49:14 +0300196 * @port_number: See ntb_port_number().
197 * @peer_port_count: See ntb_peer_port_count().
198 * @peer_port_number: See ntb_peer_port_number().
199 * @peer_port_idx: See ntb_peer_port_idx().
Serge Semin60934b22016-12-14 02:49:13 +0300200 * @link_is_up: See ntb_link_is_up().
201 * @link_enable: See ntb_link_enable().
202 * @link_disable: See ntb_link_disable().
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400203 * @mw_count: See ntb_mw_count().
204 * @mw_get_range: See ntb_mw_get_range().
205 * @mw_set_trans: See ntb_mw_set_trans().
206 * @mw_clear_trans: See ntb_mw_clear_trans().
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400207 * @db_is_unsafe: See ntb_db_is_unsafe().
208 * @db_valid_mask: See ntb_db_valid_mask().
209 * @db_vector_count: See ntb_db_vector_count().
210 * @db_vector_mask: See ntb_db_vector_mask().
211 * @db_read: See ntb_db_read().
212 * @db_set: See ntb_db_set().
213 * @db_clear: See ntb_db_clear().
214 * @db_read_mask: See ntb_db_read_mask().
215 * @db_set_mask: See ntb_db_set_mask().
216 * @db_clear_mask: See ntb_db_clear_mask().
217 * @peer_db_addr: See ntb_peer_db_addr().
218 * @peer_db_read: See ntb_peer_db_read().
219 * @peer_db_set: See ntb_peer_db_set().
220 * @peer_db_clear: See ntb_peer_db_clear().
221 * @peer_db_read_mask: See ntb_peer_db_read_mask().
222 * @peer_db_set_mask: See ntb_peer_db_set_mask().
223 * @peer_db_clear_mask: See ntb_peer_db_clear_mask().
224 * @spad_is_unsafe: See ntb_spad_is_unsafe().
225 * @spad_count: See ntb_spad_count().
226 * @spad_read: See ntb_spad_read().
227 * @spad_write: See ntb_spad_write().
228 * @peer_spad_addr: See ntb_peer_spad_addr().
229 * @peer_spad_read: See ntb_peer_spad_read().
230 * @peer_spad_write: See ntb_peer_spad_write().
231 */
232struct ntb_dev_ops {
Serge Semin1e530112016-12-14 02:49:14 +0300233 int (*port_number)(struct ntb_dev *ntb);
234 int (*peer_port_count)(struct ntb_dev *ntb);
235 int (*peer_port_number)(struct ntb_dev *ntb, int pidx);
236 int (*peer_port_idx)(struct ntb_dev *ntb, int port);
237
Serge Semin60934b22016-12-14 02:49:13 +0300238 int (*link_is_up)(struct ntb_dev *ntb,
239 enum ntb_speed *speed, enum ntb_width *width);
240 int (*link_enable)(struct ntb_dev *ntb,
241 enum ntb_speed max_speed, enum ntb_width max_width);
242 int (*link_disable)(struct ntb_dev *ntb);
243
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400244 int (*mw_count)(struct ntb_dev *ntb);
245 int (*mw_get_range)(struct ntb_dev *ntb, int idx,
246 phys_addr_t *base, resource_size_t *size,
247 resource_size_t *align, resource_size_t *align_size);
248 int (*mw_set_trans)(struct ntb_dev *ntb, int idx,
249 dma_addr_t addr, resource_size_t size);
250 int (*mw_clear_trans)(struct ntb_dev *ntb, int idx);
251
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400252 int (*db_is_unsafe)(struct ntb_dev *ntb);
253 u64 (*db_valid_mask)(struct ntb_dev *ntb);
254 int (*db_vector_count)(struct ntb_dev *ntb);
255 u64 (*db_vector_mask)(struct ntb_dev *ntb, int db_vector);
256
257 u64 (*db_read)(struct ntb_dev *ntb);
258 int (*db_set)(struct ntb_dev *ntb, u64 db_bits);
259 int (*db_clear)(struct ntb_dev *ntb, u64 db_bits);
260
261 u64 (*db_read_mask)(struct ntb_dev *ntb);
262 int (*db_set_mask)(struct ntb_dev *ntb, u64 db_bits);
263 int (*db_clear_mask)(struct ntb_dev *ntb, u64 db_bits);
264
265 int (*peer_db_addr)(struct ntb_dev *ntb,
266 phys_addr_t *db_addr, resource_size_t *db_size);
267 u64 (*peer_db_read)(struct ntb_dev *ntb);
268 int (*peer_db_set)(struct ntb_dev *ntb, u64 db_bits);
269 int (*peer_db_clear)(struct ntb_dev *ntb, u64 db_bits);
270
271 u64 (*peer_db_read_mask)(struct ntb_dev *ntb);
272 int (*peer_db_set_mask)(struct ntb_dev *ntb, u64 db_bits);
273 int (*peer_db_clear_mask)(struct ntb_dev *ntb, u64 db_bits);
274
275 int (*spad_is_unsafe)(struct ntb_dev *ntb);
276 int (*spad_count)(struct ntb_dev *ntb);
277
278 u32 (*spad_read)(struct ntb_dev *ntb, int idx);
279 int (*spad_write)(struct ntb_dev *ntb, int idx, u32 val);
280
281 int (*peer_spad_addr)(struct ntb_dev *ntb, int idx,
282 phys_addr_t *spad_addr);
283 u32 (*peer_spad_read)(struct ntb_dev *ntb, int idx);
284 int (*peer_spad_write)(struct ntb_dev *ntb, int idx, u32 val);
285};
286
287static inline int ntb_dev_ops_is_valid(const struct ntb_dev_ops *ops)
288{
289 /* commented callbacks are not required: */
290 return
Serge Semin1e530112016-12-14 02:49:14 +0300291 !ops->peer_port_count == !ops->port_number &&
292 !ops->peer_port_number == !ops->port_number &&
293 !ops->peer_port_idx == !ops->port_number &&
Serge Semin60934b22016-12-14 02:49:13 +0300294 ops->link_is_up &&
295 ops->link_enable &&
296 ops->link_disable &&
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400297 ops->mw_count &&
298 ops->mw_get_range &&
299 ops->mw_set_trans &&
300 /* ops->mw_clear_trans && */
Serge Semin60934b22016-12-14 02:49:13 +0300301
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400302 /* ops->db_is_unsafe && */
303 ops->db_valid_mask &&
304
305 /* both set, or both unset */
306 (!ops->db_vector_count == !ops->db_vector_mask) &&
307
308 ops->db_read &&
309 /* ops->db_set && */
310 ops->db_clear &&
311 /* ops->db_read_mask && */
312 ops->db_set_mask &&
313 ops->db_clear_mask &&
Allen Hubbeafc54992016-03-21 04:53:13 -0400314 /* ops->peer_db_addr && */
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400315 /* ops->peer_db_read && */
316 ops->peer_db_set &&
317 /* ops->peer_db_clear && */
318 /* ops->peer_db_read_mask && */
319 /* ops->peer_db_set_mask && */
320 /* ops->peer_db_clear_mask && */
321 /* ops->spad_is_unsafe && */
322 ops->spad_count &&
323 ops->spad_read &&
324 ops->spad_write &&
Allen Hubbeafc54992016-03-21 04:53:13 -0400325 /* ops->peer_spad_addr && */
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400326 /* ops->peer_spad_read && */
327 ops->peer_spad_write &&
328 1;
329}
330
331/**
332 * struct ntb_client - client interested in ntb devices
333 * @drv: Linux driver object.
334 * @ops: See &ntb_client_ops.
335 */
336struct ntb_client {
337 struct device_driver drv;
338 const struct ntb_client_ops ops;
339};
340
341#define drv_ntb_client(__drv) container_of((__drv), struct ntb_client, drv)
342
343/**
344 * struct ntb_device - ntb device
345 * @dev: Linux device object.
346 * @pdev: Pci device entry of the ntb.
347 * @topo: Detected topology of the ntb.
348 * @ops: See &ntb_dev_ops.
349 * @ctx: See &ntb_ctx_ops.
350 * @ctx_ops: See &ntb_ctx_ops.
351 */
352struct ntb_dev {
353 struct device dev;
354 struct pci_dev *pdev;
355 enum ntb_topo topo;
356 const struct ntb_dev_ops *ops;
357 void *ctx;
358 const struct ntb_ctx_ops *ctx_ops;
359
360 /* private: */
361
362 /* synchronize setting, clearing, and calling ctx_ops */
363 spinlock_t ctx_lock;
364 /* block unregister until device is fully released */
365 struct completion released;
366};
367
368#define dev_ntb(__dev) container_of((__dev), struct ntb_dev, dev)
369
370/**
371 * ntb_register_client() - register a client for interest in ntb devices
372 * @client: Client context.
373 *
374 * The client will be added to the list of clients interested in ntb devices.
375 * The client will be notified of any ntb devices that are not already
376 * associated with a client, or if ntb devices are registered later.
377 *
378 * Return: Zero if the client is registered, otherwise an error number.
379 */
380#define ntb_register_client(client) \
381 __ntb_register_client((client), THIS_MODULE, KBUILD_MODNAME)
382
383int __ntb_register_client(struct ntb_client *client, struct module *mod,
384 const char *mod_name);
385
386/**
387 * ntb_unregister_client() - unregister a client for interest in ntb devices
388 * @client: Client context.
389 *
390 * The client will be removed from the list of clients interested in ntb
391 * devices. If any ntb devices are associated with the client, the client will
392 * be notified to remove those devices.
393 */
394void ntb_unregister_client(struct ntb_client *client);
395
396#define module_ntb_client(__ntb_client) \
397 module_driver(__ntb_client, ntb_register_client, \
398 ntb_unregister_client)
399
400/**
401 * ntb_register_device() - register a ntb device
402 * @ntb: NTB device context.
403 *
404 * The device will be added to the list of ntb devices. If any clients are
405 * interested in ntb devices, each client will be notified of the ntb device,
406 * until at most one client accepts the device.
407 *
408 * Return: Zero if the device is registered, otherwise an error number.
409 */
410int ntb_register_device(struct ntb_dev *ntb);
411
412/**
413 * ntb_register_device() - unregister a ntb device
414 * @ntb: NTB device context.
415 *
416 * The device will be removed from the list of ntb devices. If the ntb device
417 * is associated with a client, the client will be notified to remove the
418 * device.
419 */
420void ntb_unregister_device(struct ntb_dev *ntb);
421
422/**
423 * ntb_set_ctx() - associate a driver context with an ntb device
424 * @ntb: NTB device context.
425 * @ctx: Driver context.
426 * @ctx_ops: Driver context operations.
427 *
428 * Associate a driver context and operations with a ntb device. The context is
429 * provided by the client driver, and the driver may associate a different
430 * context with each ntb device.
431 *
432 * Return: Zero if the context is associated, otherwise an error number.
433 */
434int ntb_set_ctx(struct ntb_dev *ntb, void *ctx,
435 const struct ntb_ctx_ops *ctx_ops);
436
437/**
438 * ntb_clear_ctx() - disassociate any driver context from an ntb device
439 * @ntb: NTB device context.
440 *
441 * Clear any association that may exist between a driver context and the ntb
442 * device.
443 */
444void ntb_clear_ctx(struct ntb_dev *ntb);
445
446/**
447 * ntb_link_event() - notify driver context of a change in link status
448 * @ntb: NTB device context.
449 *
450 * Notify the driver context that the link status may have changed. The driver
451 * should call ntb_link_is_up() to get the current status.
452 */
453void ntb_link_event(struct ntb_dev *ntb);
454
455/**
456 * ntb_db_event() - notify driver context of a doorbell event
457 * @ntb: NTB device context.
458 * @vector: Interrupt vector number.
459 *
460 * Notify the driver context of a doorbell event. If hardware supports
461 * multiple interrupt vectors for doorbells, the vector number indicates which
462 * vector received the interrupt. The vector number is relative to the first
463 * vector used for doorbells, starting at zero, and must be less than
464 ** ntb_db_vector_count(). The driver may call ntb_db_read() to check which
465 * doorbell bits need service, and ntb_db_vector_mask() to determine which of
466 * those bits are associated with the vector number.
467 */
468void ntb_db_event(struct ntb_dev *ntb, int vector);
469
470/**
Serge Semin1e530112016-12-14 02:49:14 +0300471 * ntb_default_port_number() - get the default local port number
472 * @ntb: NTB device context.
473 *
474 * If hardware driver doesn't specify port_number() callback method, the NTB
475 * is considered with just two ports. So this method returns default local
476 * port number in compliance with topology.
477 *
478 * NOTE Don't call this method directly. The ntb_port_number() function should
479 * be used instead.
480 *
481 * Return: the default local port number
482 */
483int ntb_default_port_number(struct ntb_dev *ntb);
484
485/**
486 * ntb_default_port_count() - get the default number of peer device ports
487 * @ntb: NTB device context.
488 *
489 * By default hardware driver supports just one peer device.
490 *
491 * NOTE Don't call this method directly. The ntb_peer_port_count() function
492 * should be used instead.
493 *
494 * Return: the default number of peer ports
495 */
496int ntb_default_peer_port_count(struct ntb_dev *ntb);
497
498/**
499 * ntb_default_peer_port_number() - get the default peer port by given index
500 * @ntb: NTB device context.
501 * @idx: Peer port index (should not differ from zero).
502 *
503 * By default hardware driver supports just one peer device, so this method
504 * shall return the corresponding value from enum ntb_default_port.
505 *
506 * NOTE Don't call this method directly. The ntb_peer_port_number() function
507 * should be used instead.
508 *
509 * Return: the peer device port or negative value indicating an error
510 */
511int ntb_default_peer_port_number(struct ntb_dev *ntb, int pidx);
512
513/**
514 * ntb_default_peer_port_idx() - get the default peer device port index by
515 * given port number
516 * @ntb: NTB device context.
517 * @port: Peer port number (should be one of enum ntb_default_port).
518 *
519 * By default hardware driver supports just one peer device, so while
520 * specified port-argument indicates peer port from enum ntb_default_port,
521 * the return value shall be zero.
522 *
523 * NOTE Don't call this method directly. The ntb_peer_port_idx() function
524 * should be used instead.
525 *
526 * Return: the peer port index or negative value indicating an error
527 */
528int ntb_default_peer_port_idx(struct ntb_dev *ntb, int port);
529
530/**
531 * ntb_port_number() - get the local port number
532 * @ntb: NTB device context.
533 *
534 * Hardware must support at least simple two-ports ntb connection
535 *
536 * Return: the local port number
537 */
538static inline int ntb_port_number(struct ntb_dev *ntb)
539{
540 if (!ntb->ops->port_number)
541 return ntb_default_port_number(ntb);
542
543 return ntb->ops->port_number(ntb);
544}
545
546/**
547 * ntb_peer_port_count() - get the number of peer device ports
548 * @ntb: NTB device context.
549 *
550 * Hardware may support an access to memory of several remote domains
551 * over multi-port NTB devices. This method returns the number of peers,
552 * local device can have shared memory with.
553 *
554 * Return: the number of peer ports
555 */
556static inline int ntb_peer_port_count(struct ntb_dev *ntb)
557{
558 if (!ntb->ops->peer_port_count)
559 return ntb_default_peer_port_count(ntb);
560
561 return ntb->ops->peer_port_count(ntb);
562}
563
564/**
565 * ntb_peer_port_number() - get the peer port by given index
566 * @ntb: NTB device context.
567 * @pidx: Peer port index.
568 *
569 * Peer ports are continuously enumerated by NTB API logic, so this method
570 * lets to retrieve port real number by its index.
571 *
572 * Return: the peer device port or negative value indicating an error
573 */
574static inline int ntb_peer_port_number(struct ntb_dev *ntb, int pidx)
575{
576 if (!ntb->ops->peer_port_number)
577 return ntb_default_peer_port_number(ntb, pidx);
578
579 return ntb->ops->peer_port_number(ntb, pidx);
580}
581
582/**
583 * ntb_peer_port_idx() - get the peer device port index by given port number
584 * @ntb: NTB device context.
585 * @port: Peer port number.
586 *
587 * Inverse operation of ntb_peer_port_number(), so one can get port index
588 * by specified port number.
589 *
590 * Return: the peer port index or negative value indicating an error
591 */
592static inline int ntb_peer_port_idx(struct ntb_dev *ntb, int port)
593{
594 if (!ntb->ops->peer_port_idx)
595 return ntb_default_peer_port_idx(ntb, port);
596
597 return ntb->ops->peer_port_idx(ntb, port);
598}
599
600/**
Serge Semin60934b22016-12-14 02:49:13 +0300601 * ntb_link_is_up() - get the current ntb link state
602 * @ntb: NTB device context.
603 * @speed: OUT - The link speed expressed as PCIe generation number.
604 * @width: OUT - The link width expressed as the number of PCIe lanes.
605 *
606 * Get the current state of the ntb link. It is recommended to query the link
607 * state once after every link event. It is safe to query the link state in
608 * the context of the link event callback.
609 *
610 * Return: One if the link is up, zero if the link is down, otherwise a
611 * negative value indicating the error number.
612 */
613static inline int ntb_link_is_up(struct ntb_dev *ntb,
614 enum ntb_speed *speed, enum ntb_width *width)
615{
616 return ntb->ops->link_is_up(ntb, speed, width);
617}
618
619/**
620 * ntb_link_enable() - enable the link on the secondary side of the ntb
621 * @ntb: NTB device context.
622 * @max_speed: The maximum link speed expressed as PCIe generation number.
623 * @max_width: The maximum link width expressed as the number of PCIe lanes.
624 *
625 * Enable the link on the secondary side of the ntb. This can only be done
626 * from the primary side of the ntb in primary or b2b topology. The ntb device
627 * should train the link to its maximum speed and width, or the requested speed
628 * and width, whichever is smaller, if supported.
629 *
630 * Return: Zero on success, otherwise an error number.
631 */
632static inline int ntb_link_enable(struct ntb_dev *ntb,
633 enum ntb_speed max_speed,
634 enum ntb_width max_width)
635{
636 return ntb->ops->link_enable(ntb, max_speed, max_width);
637}
638
639/**
640 * ntb_link_disable() - disable the link on the secondary side of the ntb
641 * @ntb: NTB device context.
642 *
643 * Disable the link on the secondary side of the ntb. This can only be
644 * done from the primary side of the ntb in primary or b2b topology. The ntb
645 * device should disable the link. Returning from this call must indicate that
646 * a barrier has passed, though with no more writes may pass in either
647 * direction across the link, except if this call returns an error number.
648 *
649 * Return: Zero on success, otherwise an error number.
650 */
651static inline int ntb_link_disable(struct ntb_dev *ntb)
652{
653 return ntb->ops->link_disable(ntb);
654}
655
656/**
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400657 * ntb_mw_count() - get the number of memory windows
658 * @ntb: NTB device context.
659 *
660 * Hardware and topology may support a different number of memory windows.
661 *
662 * Return: the number of memory windows.
663 */
664static inline int ntb_mw_count(struct ntb_dev *ntb)
665{
666 return ntb->ops->mw_count(ntb);
667}
668
669/**
670 * ntb_mw_get_range() - get the range of a memory window
671 * @ntb: NTB device context.
672 * @idx: Memory window number.
673 * @base: OUT - the base address for mapping the memory window
674 * @size: OUT - the size for mapping the memory window
675 * @align: OUT - the base alignment for translating the memory window
676 * @align_size: OUT - the size alignment for translating the memory window
677 *
678 * Get the range of a memory window. NULL may be given for any output
679 * parameter if the value is not needed. The base and size may be used for
680 * mapping the memory window, to access the peer memory. The alignment and
681 * size may be used for translating the memory window, for the peer to access
682 * memory on the local system.
683 *
684 * Return: Zero on success, otherwise an error number.
685 */
686static inline int ntb_mw_get_range(struct ntb_dev *ntb, int idx,
687 phys_addr_t *base, resource_size_t *size,
688 resource_size_t *align, resource_size_t *align_size)
689{
690 return ntb->ops->mw_get_range(ntb, idx, base, size,
691 align, align_size);
692}
693
694/**
695 * ntb_mw_set_trans() - set the translation of a memory window
696 * @ntb: NTB device context.
697 * @idx: Memory window number.
698 * @addr: The dma address local memory to expose to the peer.
699 * @size: The size of the local memory to expose to the peer.
700 *
701 * Set the translation of a memory window. The peer may access local memory
702 * through the window starting at the address, up to the size. The address
703 * must be aligned to the alignment specified by ntb_mw_get_range(). The size
704 * must be aligned to the size alignment specified by ntb_mw_get_range().
705 *
706 * Return: Zero on success, otherwise an error number.
707 */
708static inline int ntb_mw_set_trans(struct ntb_dev *ntb, int idx,
709 dma_addr_t addr, resource_size_t size)
710{
711 return ntb->ops->mw_set_trans(ntb, idx, addr, size);
712}
713
714/**
715 * ntb_mw_clear_trans() - clear the translation of a memory window
716 * @ntb: NTB device context.
717 * @idx: Memory window number.
718 *
719 * Clear the translation of a memory window. The peer may no longer access
720 * local memory through the window.
721 *
722 * Return: Zero on success, otherwise an error number.
723 */
724static inline int ntb_mw_clear_trans(struct ntb_dev *ntb, int idx)
725{
726 if (!ntb->ops->mw_clear_trans)
727 return ntb->ops->mw_set_trans(ntb, idx, 0, 0);
728
729 return ntb->ops->mw_clear_trans(ntb, idx);
730}
731
732/**
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400733 * ntb_db_is_unsafe() - check if it is safe to use hardware doorbell
734 * @ntb: NTB device context.
735 *
736 * It is possible for some ntb hardware to be affected by errata. Hardware
737 * drivers can advise clients to avoid using doorbells. Clients may ignore
738 * this advice, though caution is recommended.
739 *
740 * Return: Zero if it is safe to use doorbells, or One if it is not safe.
741 */
742static inline int ntb_db_is_unsafe(struct ntb_dev *ntb)
743{
744 if (!ntb->ops->db_is_unsafe)
745 return 0;
746
747 return ntb->ops->db_is_unsafe(ntb);
748}
749
750/**
751 * ntb_db_valid_mask() - get a mask of doorbell bits supported by the ntb
752 * @ntb: NTB device context.
753 *
754 * Hardware may support different number or arrangement of doorbell bits.
755 *
756 * Return: A mask of doorbell bits supported by the ntb.
757 */
758static inline u64 ntb_db_valid_mask(struct ntb_dev *ntb)
759{
760 return ntb->ops->db_valid_mask(ntb);
761}
762
763/**
764 * ntb_db_vector_count() - get the number of doorbell interrupt vectors
765 * @ntb: NTB device context.
766 *
767 * Hardware may support different number of interrupt vectors.
768 *
769 * Return: The number of doorbell interrupt vectors.
770 */
771static inline int ntb_db_vector_count(struct ntb_dev *ntb)
772{
773 if (!ntb->ops->db_vector_count)
774 return 1;
775
776 return ntb->ops->db_vector_count(ntb);
777}
778
779/**
780 * ntb_db_vector_mask() - get a mask of doorbell bits serviced by a vector
781 * @ntb: NTB device context.
782 * @vector: Doorbell vector number.
783 *
784 * Each interrupt vector may have a different number or arrangement of bits.
785 *
786 * Return: A mask of doorbell bits serviced by a vector.
787 */
788static inline u64 ntb_db_vector_mask(struct ntb_dev *ntb, int vector)
789{
790 if (!ntb->ops->db_vector_mask)
791 return ntb_db_valid_mask(ntb);
792
793 return ntb->ops->db_vector_mask(ntb, vector);
794}
795
796/**
797 * ntb_db_read() - read the local doorbell register
798 * @ntb: NTB device context.
799 *
800 * Read the local doorbell register, and return the bits that are set.
801 *
802 * Return: The bits currently set in the local doorbell register.
803 */
804static inline u64 ntb_db_read(struct ntb_dev *ntb)
805{
806 return ntb->ops->db_read(ntb);
807}
808
809/**
810 * ntb_db_set() - set bits in the local doorbell register
811 * @ntb: NTB device context.
812 * @db_bits: Doorbell bits to set.
813 *
814 * Set bits in the local doorbell register, which may generate a local doorbell
815 * interrupt. Bits that were already set must remain set.
816 *
817 * This is unusual, and hardware may not support it.
818 *
819 * Return: Zero on success, otherwise an error number.
820 */
821static inline int ntb_db_set(struct ntb_dev *ntb, u64 db_bits)
822{
823 if (!ntb->ops->db_set)
824 return -EINVAL;
825
826 return ntb->ops->db_set(ntb, db_bits);
827}
828
829/**
830 * ntb_db_clear() - clear bits in the local doorbell register
831 * @ntb: NTB device context.
832 * @db_bits: Doorbell bits to clear.
833 *
834 * Clear bits in the local doorbell register, arming the bits for the next
835 * doorbell.
836 *
837 * Return: Zero on success, otherwise an error number.
838 */
839static inline int ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
840{
841 return ntb->ops->db_clear(ntb, db_bits);
842}
843
844/**
845 * ntb_db_read_mask() - read the local doorbell mask
846 * @ntb: NTB device context.
847 *
848 * Read the local doorbell mask register, and return the bits that are set.
849 *
850 * This is unusual, though hardware is likely to support it.
851 *
852 * Return: The bits currently set in the local doorbell mask register.
853 */
854static inline u64 ntb_db_read_mask(struct ntb_dev *ntb)
855{
856 if (!ntb->ops->db_read_mask)
857 return 0;
858
859 return ntb->ops->db_read_mask(ntb);
860}
861
862/**
863 * ntb_db_set_mask() - set bits in the local doorbell mask
864 * @ntb: NTB device context.
865 * @db_bits: Doorbell mask bits to set.
866 *
867 * Set bits in the local doorbell mask register, preventing doorbell interrupts
868 * from being generated for those doorbell bits. Bits that were already set
869 * must remain set.
870 *
871 * Return: Zero on success, otherwise an error number.
872 */
873static inline int ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
874{
875 return ntb->ops->db_set_mask(ntb, db_bits);
876}
877
878/**
879 * ntb_db_clear_mask() - clear bits in the local doorbell mask
880 * @ntb: NTB device context.
881 * @db_bits: Doorbell bits to clear.
882 *
883 * Clear bits in the local doorbell mask register, allowing doorbell interrupts
884 * from being generated for those doorbell bits. If a doorbell bit is already
885 * set at the time the mask is cleared, and the corresponding mask bit is
886 * changed from set to clear, then the ntb driver must ensure that
887 * ntb_db_event() is called. If the hardware does not generate the interrupt
888 * on clearing the mask bit, then the driver must call ntb_db_event() anyway.
889 *
890 * Return: Zero on success, otherwise an error number.
891 */
892static inline int ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
893{
894 return ntb->ops->db_clear_mask(ntb, db_bits);
895}
896
897/**
898 * ntb_peer_db_addr() - address and size of the peer doorbell register
899 * @ntb: NTB device context.
900 * @db_addr: OUT - The address of the peer doorbell register.
901 * @db_size: OUT - The number of bytes to write the peer doorbell register.
902 *
903 * Return the address of the peer doorbell register. This may be used, for
904 * example, by drivers that offload memory copy operations to a dma engine.
905 * The drivers may wish to ring the peer doorbell at the completion of memory
906 * copy operations. For efficiency, and to simplify ordering of operations
907 * between the dma memory copies and the ringing doorbell, the driver may
908 * append one additional dma memory copy with the doorbell register as the
909 * destination, after the memory copy operations.
910 *
911 * Return: Zero on success, otherwise an error number.
912 */
913static inline int ntb_peer_db_addr(struct ntb_dev *ntb,
914 phys_addr_t *db_addr,
915 resource_size_t *db_size)
916{
Allen Hubbeafc54992016-03-21 04:53:13 -0400917 if (!ntb->ops->peer_db_addr)
918 return -EINVAL;
919
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400920 return ntb->ops->peer_db_addr(ntb, db_addr, db_size);
921}
922
923/**
924 * ntb_peer_db_read() - read the peer doorbell register
925 * @ntb: NTB device context.
926 *
927 * Read the peer doorbell register, and return the bits that are set.
928 *
929 * This is unusual, and hardware may not support it.
930 *
931 * Return: The bits currently set in the peer doorbell register.
932 */
933static inline u64 ntb_peer_db_read(struct ntb_dev *ntb)
934{
935 if (!ntb->ops->peer_db_read)
936 return 0;
937
938 return ntb->ops->peer_db_read(ntb);
939}
940
941/**
942 * ntb_peer_db_set() - set bits in the peer doorbell register
943 * @ntb: NTB device context.
944 * @db_bits: Doorbell bits to set.
945 *
946 * Set bits in the peer doorbell register, which may generate a peer doorbell
947 * interrupt. Bits that were already set must remain set.
948 *
949 * Return: Zero on success, otherwise an error number.
950 */
951static inline int ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
952{
953 return ntb->ops->peer_db_set(ntb, db_bits);
954}
955
956/**
Allen Hubbe86663c92015-07-15 12:43:21 -0400957 * ntb_peer_db_clear() - clear bits in the peer doorbell register
Allen Hubbea1bd3ba2015-04-09 10:33:20 -0400958 * @ntb: NTB device context.
959 * @db_bits: Doorbell bits to clear.
960 *
961 * Clear bits in the peer doorbell register, arming the bits for the next
962 * doorbell.
963 *
964 * This is unusual, and hardware may not support it.
965 *
966 * Return: Zero on success, otherwise an error number.
967 */
968static inline int ntb_peer_db_clear(struct ntb_dev *ntb, u64 db_bits)
969{
970 if (!ntb->ops->db_clear)
971 return -EINVAL;
972
973 return ntb->ops->peer_db_clear(ntb, db_bits);
974}
975
976/**
977 * ntb_peer_db_read_mask() - read the peer doorbell mask
978 * @ntb: NTB device context.
979 *
980 * Read the peer doorbell mask register, and return the bits that are set.
981 *
982 * This is unusual, and hardware may not support it.
983 *
984 * Return: The bits currently set in the peer doorbell mask register.
985 */
986static inline u64 ntb_peer_db_read_mask(struct ntb_dev *ntb)
987{
988 if (!ntb->ops->db_read_mask)
989 return 0;
990
991 return ntb->ops->peer_db_read_mask(ntb);
992}
993
994/**
995 * ntb_peer_db_set_mask() - set bits in the peer doorbell mask
996 * @ntb: NTB device context.
997 * @db_bits: Doorbell mask bits to set.
998 *
999 * Set bits in the peer doorbell mask register, preventing doorbell interrupts
1000 * from being generated for those doorbell bits. Bits that were already set
1001 * must remain set.
1002 *
1003 * This is unusual, and hardware may not support it.
1004 *
1005 * Return: Zero on success, otherwise an error number.
1006 */
1007static inline int ntb_peer_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
1008{
1009 if (!ntb->ops->db_set_mask)
1010 return -EINVAL;
1011
1012 return ntb->ops->peer_db_set_mask(ntb, db_bits);
1013}
1014
1015/**
1016 * ntb_peer_db_clear_mask() - clear bits in the peer doorbell mask
1017 * @ntb: NTB device context.
1018 * @db_bits: Doorbell bits to clear.
1019 *
1020 * Clear bits in the peer doorbell mask register, allowing doorbell interrupts
1021 * from being generated for those doorbell bits. If the hardware does not
1022 * generate the interrupt on clearing the mask bit, then the driver should not
1023 * implement this function!
1024 *
1025 * This is unusual, and hardware may not support it.
1026 *
1027 * Return: Zero on success, otherwise an error number.
1028 */
1029static inline int ntb_peer_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
1030{
1031 if (!ntb->ops->db_clear_mask)
1032 return -EINVAL;
1033
1034 return ntb->ops->peer_db_clear_mask(ntb, db_bits);
1035}
1036
1037/**
1038 * ntb_spad_is_unsafe() - check if it is safe to use the hardware scratchpads
1039 * @ntb: NTB device context.
1040 *
1041 * It is possible for some ntb hardware to be affected by errata. Hardware
1042 * drivers can advise clients to avoid using scratchpads. Clients may ignore
1043 * this advice, though caution is recommended.
1044 *
1045 * Return: Zero if it is safe to use scratchpads, or One if it is not safe.
1046 */
1047static inline int ntb_spad_is_unsafe(struct ntb_dev *ntb)
1048{
1049 if (!ntb->ops->spad_is_unsafe)
1050 return 0;
1051
1052 return ntb->ops->spad_is_unsafe(ntb);
1053}
1054
1055/**
Aaron Sierra74dcba32016-12-06 19:09:16 -06001056 * ntb_spad_count() - get the number of scratchpads
Allen Hubbea1bd3ba2015-04-09 10:33:20 -04001057 * @ntb: NTB device context.
1058 *
1059 * Hardware and topology may support a different number of scratchpads.
1060 *
1061 * Return: the number of scratchpads.
1062 */
1063static inline int ntb_spad_count(struct ntb_dev *ntb)
1064{
1065 return ntb->ops->spad_count(ntb);
1066}
1067
1068/**
1069 * ntb_spad_read() - read the local scratchpad register
1070 * @ntb: NTB device context.
1071 * @idx: Scratchpad index.
1072 *
1073 * Read the local scratchpad register, and return the value.
1074 *
1075 * Return: The value of the local scratchpad register.
1076 */
1077static inline u32 ntb_spad_read(struct ntb_dev *ntb, int idx)
1078{
1079 return ntb->ops->spad_read(ntb, idx);
1080}
1081
1082/**
1083 * ntb_spad_write() - write the local scratchpad register
1084 * @ntb: NTB device context.
1085 * @idx: Scratchpad index.
1086 * @val: Scratchpad value.
1087 *
1088 * Write the value to the local scratchpad register.
1089 *
1090 * Return: Zero on success, otherwise an error number.
1091 */
1092static inline int ntb_spad_write(struct ntb_dev *ntb, int idx, u32 val)
1093{
1094 return ntb->ops->spad_write(ntb, idx, val);
1095}
1096
1097/**
1098 * ntb_peer_spad_addr() - address of the peer scratchpad register
1099 * @ntb: NTB device context.
1100 * @idx: Scratchpad index.
1101 * @spad_addr: OUT - The address of the peer scratchpad register.
1102 *
1103 * Return the address of the peer doorbell register. This may be used, for
1104 * example, by drivers that offload memory copy operations to a dma engine.
1105 *
1106 * Return: Zero on success, otherwise an error number.
1107 */
1108static inline int ntb_peer_spad_addr(struct ntb_dev *ntb, int idx,
1109 phys_addr_t *spad_addr)
1110{
Allen Hubbeafc54992016-03-21 04:53:13 -04001111 if (!ntb->ops->peer_spad_addr)
1112 return -EINVAL;
1113
Allen Hubbea1bd3ba2015-04-09 10:33:20 -04001114 return ntb->ops->peer_spad_addr(ntb, idx, spad_addr);
1115}
1116
1117/**
1118 * ntb_peer_spad_read() - read the peer scratchpad register
1119 * @ntb: NTB device context.
1120 * @idx: Scratchpad index.
1121 *
1122 * Read the peer scratchpad register, and return the value.
1123 *
1124 * Return: The value of the local scratchpad register.
1125 */
1126static inline u32 ntb_peer_spad_read(struct ntb_dev *ntb, int idx)
1127{
Steven Wahl5c43c522016-12-08 17:02:28 +00001128 if (!ntb->ops->peer_spad_read)
1129 return 0;
1130
Allen Hubbea1bd3ba2015-04-09 10:33:20 -04001131 return ntb->ops->peer_spad_read(ntb, idx);
1132}
1133
1134/**
1135 * ntb_peer_spad_write() - write the peer scratchpad register
1136 * @ntb: NTB device context.
1137 * @idx: Scratchpad index.
1138 * @val: Scratchpad value.
1139 *
1140 * Write the value to the peer scratchpad register.
1141 *
1142 * Return: Zero on success, otherwise an error number.
1143 */
1144static inline int ntb_peer_spad_write(struct ntb_dev *ntb, int idx, u32 val)
1145{
1146 return ntb->ops->peer_spad_write(ntb, idx, val);
1147}
1148
1149#endif