Revert "locking/lockdep: Add debug_locks check in __lock_downgrade()"
[linux-stable.git] / drivers / ntb / ntb.c
blob03b80d89b98003cd20a05823dd80610d26b23920
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.
5 * GPL LICENSE SUMMARY
7 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
8 * Copyright (C) 2016 T-Platforms. All Rights Reserved.
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.
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.
19 * BSD LICENSE
21 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
22 * Copyright (C) 2016 T-Platforms. All Rights Reserved.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
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.
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.
50 * PCIe NTB Linux driver
52 * Contact Information:
53 * Allen Hubbe <Allen.Hubbe@emc.com>
56 #include <linux/device.h>
57 #include <linux/kernel.h>
58 #include <linux/module.h>
60 #include <linux/ntb.h>
61 #include <linux/pci.h>
63 #define DRIVER_NAME "ntb"
64 #define DRIVER_DESCRIPTION "PCIe NTB Driver Framework"
66 #define DRIVER_LICENSE "Dual BSD/GPL"
67 #define DRIVER_VERSION "1.0"
68 #define DRIVER_RELDATE "24 March 2015"
69 #define DRIVER_AUTHOR "Allen Hubbe <Allen.Hubbe@emc.com>"
71 MODULE_LICENSE(DRIVER_LICENSE);
72 MODULE_VERSION(DRIVER_VERSION);
73 MODULE_AUTHOR(DRIVER_AUTHOR);
74 MODULE_DESCRIPTION(DRIVER_DESCRIPTION);
76 static struct bus_type ntb_bus;
77 static void ntb_dev_release(struct device *dev);
79 int __ntb_register_client(struct ntb_client *client, struct module *mod,
80 const char *mod_name)
82 if (!client)
83 return -EINVAL;
84 if (!ntb_client_ops_is_valid(&client->ops))
85 return -EINVAL;
87 memset(&client->drv, 0, sizeof(client->drv));
88 client->drv.bus = &ntb_bus;
89 client->drv.name = mod_name;
90 client->drv.owner = mod;
92 return driver_register(&client->drv);
94 EXPORT_SYMBOL(__ntb_register_client);
96 void ntb_unregister_client(struct ntb_client *client)
98 driver_unregister(&client->drv);
100 EXPORT_SYMBOL(ntb_unregister_client);
102 int ntb_register_device(struct ntb_dev *ntb)
104 if (!ntb)
105 return -EINVAL;
106 if (!ntb->pdev)
107 return -EINVAL;
108 if (!ntb->ops)
109 return -EINVAL;
110 if (!ntb_dev_ops_is_valid(ntb->ops))
111 return -EINVAL;
113 init_completion(&ntb->released);
115 memset(&ntb->dev, 0, sizeof(ntb->dev));
116 ntb->dev.bus = &ntb_bus;
117 ntb->dev.parent = &ntb->pdev->dev;
118 ntb->dev.release = ntb_dev_release;
119 dev_set_name(&ntb->dev, "%s", pci_name(ntb->pdev));
121 ntb->ctx = NULL;
122 ntb->ctx_ops = NULL;
123 spin_lock_init(&ntb->ctx_lock);
125 return device_register(&ntb->dev);
127 EXPORT_SYMBOL(ntb_register_device);
129 void ntb_unregister_device(struct ntb_dev *ntb)
131 device_unregister(&ntb->dev);
132 wait_for_completion(&ntb->released);
134 EXPORT_SYMBOL(ntb_unregister_device);
136 int ntb_set_ctx(struct ntb_dev *ntb, void *ctx,
137 const struct ntb_ctx_ops *ctx_ops)
139 unsigned long irqflags;
141 if (!ntb_ctx_ops_is_valid(ctx_ops))
142 return -EINVAL;
143 if (ntb->ctx_ops)
144 return -EINVAL;
146 spin_lock_irqsave(&ntb->ctx_lock, irqflags);
148 ntb->ctx = ctx;
149 ntb->ctx_ops = ctx_ops;
151 spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
153 return 0;
155 EXPORT_SYMBOL(ntb_set_ctx);
157 void ntb_clear_ctx(struct ntb_dev *ntb)
159 unsigned long irqflags;
161 spin_lock_irqsave(&ntb->ctx_lock, irqflags);
163 ntb->ctx_ops = NULL;
164 ntb->ctx = NULL;
166 spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
168 EXPORT_SYMBOL(ntb_clear_ctx);
170 void ntb_link_event(struct ntb_dev *ntb)
172 unsigned long irqflags;
174 spin_lock_irqsave(&ntb->ctx_lock, irqflags);
176 if (ntb->ctx_ops && ntb->ctx_ops->link_event)
177 ntb->ctx_ops->link_event(ntb->ctx);
179 spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
181 EXPORT_SYMBOL(ntb_link_event);
183 void ntb_db_event(struct ntb_dev *ntb, int vector)
185 unsigned long irqflags;
187 spin_lock_irqsave(&ntb->ctx_lock, irqflags);
189 if (ntb->ctx_ops && ntb->ctx_ops->db_event)
190 ntb->ctx_ops->db_event(ntb->ctx, vector);
192 spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
194 EXPORT_SYMBOL(ntb_db_event);
196 void ntb_msg_event(struct ntb_dev *ntb)
198 unsigned long irqflags;
200 spin_lock_irqsave(&ntb->ctx_lock, irqflags);
202 if (ntb->ctx_ops && ntb->ctx_ops->msg_event)
203 ntb->ctx_ops->msg_event(ntb->ctx);
205 spin_unlock_irqrestore(&ntb->ctx_lock, irqflags);
207 EXPORT_SYMBOL(ntb_msg_event);
209 int ntb_default_port_number(struct ntb_dev *ntb)
211 switch (ntb->topo) {
212 case NTB_TOPO_PRI:
213 case NTB_TOPO_B2B_USD:
214 return NTB_PORT_PRI_USD;
215 case NTB_TOPO_SEC:
216 case NTB_TOPO_B2B_DSD:
217 return NTB_PORT_SEC_DSD;
218 default:
219 break;
222 return -EINVAL;
224 EXPORT_SYMBOL(ntb_default_port_number);
226 int ntb_default_peer_port_count(struct ntb_dev *ntb)
228 return NTB_DEF_PEER_CNT;
230 EXPORT_SYMBOL(ntb_default_peer_port_count);
232 int ntb_default_peer_port_number(struct ntb_dev *ntb, int pidx)
234 if (pidx != NTB_DEF_PEER_IDX)
235 return -EINVAL;
237 switch (ntb->topo) {
238 case NTB_TOPO_PRI:
239 case NTB_TOPO_B2B_USD:
240 return NTB_PORT_SEC_DSD;
241 case NTB_TOPO_SEC:
242 case NTB_TOPO_B2B_DSD:
243 return NTB_PORT_PRI_USD;
244 default:
245 break;
248 return -EINVAL;
250 EXPORT_SYMBOL(ntb_default_peer_port_number);
252 int ntb_default_peer_port_idx(struct ntb_dev *ntb, int port)
254 int peer_port = ntb_default_peer_port_number(ntb, NTB_DEF_PEER_IDX);
256 if (peer_port == -EINVAL || port != peer_port)
257 return -EINVAL;
259 return 0;
261 EXPORT_SYMBOL(ntb_default_peer_port_idx);
263 static int ntb_probe(struct device *dev)
265 struct ntb_dev *ntb;
266 struct ntb_client *client;
267 int rc;
269 get_device(dev);
270 ntb = dev_ntb(dev);
271 client = drv_ntb_client(dev->driver);
273 rc = client->ops.probe(client, ntb);
274 if (rc)
275 put_device(dev);
277 return rc;
280 static int ntb_remove(struct device *dev)
282 struct ntb_dev *ntb;
283 struct ntb_client *client;
285 if (dev->driver) {
286 ntb = dev_ntb(dev);
287 client = drv_ntb_client(dev->driver);
289 client->ops.remove(client, ntb);
290 put_device(dev);
293 return 0;
296 static void ntb_dev_release(struct device *dev)
298 struct ntb_dev *ntb = dev_ntb(dev);
300 complete(&ntb->released);
303 static struct bus_type ntb_bus = {
304 .name = "ntb",
305 .probe = ntb_probe,
306 .remove = ntb_remove,
309 static int __init ntb_driver_init(void)
311 return bus_register(&ntb_bus);
313 module_init(ntb_driver_init);
315 static void __exit ntb_driver_exit(void)
317 bus_unregister(&ntb_bus);
319 module_exit(ntb_driver_exit);