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