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