11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (c) 2004 Topspin Communications. All rights reserved. 32a1d9b7fSRoland Dreier * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This software is available to you under a choice of one of two 61da177e4SLinus Torvalds * licenses. You may choose to be licensed under the terms of the GNU 71da177e4SLinus Torvalds * General Public License (GPL) Version 2, available from the file 81da177e4SLinus Torvalds * COPYING in the main directory of this source tree, or the 91da177e4SLinus Torvalds * OpenIB.org BSD license below: 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * Redistribution and use in source and binary forms, with or 121da177e4SLinus Torvalds * without modification, are permitted provided that the following 131da177e4SLinus Torvalds * conditions are met: 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * - Redistributions of source code must retain the above 161da177e4SLinus Torvalds * copyright notice, this list of conditions and the following 171da177e4SLinus Torvalds * disclaimer. 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * - Redistributions in binary form must reproduce the above 201da177e4SLinus Torvalds * copyright notice, this list of conditions and the following 211da177e4SLinus Torvalds * disclaimer in the documentation and/or other materials 221da177e4SLinus Torvalds * provided with the distribution. 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 251da177e4SLinus Torvalds * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 261da177e4SLinus Torvalds * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 271da177e4SLinus Torvalds * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 281da177e4SLinus Torvalds * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 291da177e4SLinus Torvalds * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 301da177e4SLinus Torvalds * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 311da177e4SLinus Torvalds * SOFTWARE. 321da177e4SLinus Torvalds */ 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #include <linux/module.h> 351da177e4SLinus Torvalds #include <linux/string.h> 361da177e4SLinus Torvalds #include <linux/errno.h> 379a6b090cSAhmed S. Darwish #include <linux/kernel.h> 381da177e4SLinus Torvalds #include <linux/slab.h> 391da177e4SLinus Torvalds #include <linux/init.h> 409268f72dSYotam Kenneth #include <linux/netdevice.h> 414e0f7b90SParav Pandit #include <net/net_namespace.h> 428f408ab6SDaniel Jurgens #include <linux/security.h> 438f408ab6SDaniel Jurgens #include <linux/notifier.h> 44324e227eSJason Gunthorpe #include <linux/hashtable.h> 45b2cbae2cSRoland Dreier #include <rdma/rdma_netlink.h> 4603db3a2dSMatan Barak #include <rdma/ib_addr.h> 4703db3a2dSMatan Barak #include <rdma/ib_cache.h> 48413d3347SMark Zhang #include <rdma/rdma_counter.h> 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include "core_priv.h" 5141eda65cSLeon Romanovsky #include "restrack.h" 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds MODULE_AUTHOR("Roland Dreier"); 541da177e4SLinus Torvalds MODULE_DESCRIPTION("core kernel InfiniBand API"); 551da177e4SLinus Torvalds MODULE_LICENSE("Dual BSD/GPL"); 561da177e4SLinus Torvalds 5714d3a3b2SChristoph Hellwig struct workqueue_struct *ib_comp_wq; 58f794809aSJack Morgenstein struct workqueue_struct *ib_comp_unbound_wq; 59f0626710STejun Heo struct workqueue_struct *ib_wq; 60f0626710STejun Heo EXPORT_SYMBOL_GPL(ib_wq); 61f0626710STejun Heo 620df91bb6SJason Gunthorpe /* 63921eab11SJason Gunthorpe * Each of the three rwsem locks (devices, clients, client_data) protects the 64921eab11SJason Gunthorpe * xarray of the same name. Specifically it allows the caller to assert that 65921eab11SJason Gunthorpe * the MARK will/will not be changing under the lock, and for devices and 66921eab11SJason Gunthorpe * clients, that the value in the xarray is still a valid pointer. Change of 67921eab11SJason Gunthorpe * the MARK is linked to the object state, so holding the lock and testing the 68921eab11SJason Gunthorpe * MARK also asserts that the contained object is in a certain state. 69921eab11SJason Gunthorpe * 70921eab11SJason Gunthorpe * This is used to build a two stage register/unregister flow where objects 71921eab11SJason Gunthorpe * can continue to be in the xarray even though they are still in progress to 72921eab11SJason Gunthorpe * register/unregister. 73921eab11SJason Gunthorpe * 74921eab11SJason Gunthorpe * The xarray itself provides additional locking, and restartable iteration, 75921eab11SJason Gunthorpe * which is also relied on. 76921eab11SJason Gunthorpe * 77921eab11SJason Gunthorpe * Locks should not be nested, with the exception of client_data, which is 78921eab11SJason Gunthorpe * allowed to nest under the read side of the other two locks. 79921eab11SJason Gunthorpe * 80921eab11SJason Gunthorpe * The devices_rwsem also protects the device name list, any change or 81921eab11SJason Gunthorpe * assignment of device name must also hold the write side to guarantee unique 82921eab11SJason Gunthorpe * names. 83921eab11SJason Gunthorpe */ 84921eab11SJason Gunthorpe 85921eab11SJason Gunthorpe /* 860df91bb6SJason Gunthorpe * devices contains devices that have had their names assigned. The 870df91bb6SJason Gunthorpe * devices may not be registered. Users that care about the registration 880df91bb6SJason Gunthorpe * status need to call ib_device_try_get() on the device to ensure it is 890df91bb6SJason Gunthorpe * registered, and keep it registered, for the required duration. 900df91bb6SJason Gunthorpe * 910df91bb6SJason Gunthorpe */ 920df91bb6SJason Gunthorpe static DEFINE_XARRAY_FLAGS(devices, XA_FLAGS_ALLOC); 93921eab11SJason Gunthorpe static DECLARE_RWSEM(devices_rwsem); 940df91bb6SJason Gunthorpe #define DEVICE_REGISTERED XA_MARK_1 950df91bb6SJason Gunthorpe 969cd58817SJason Gunthorpe static u32 highest_client_id; 97e59178d8SJason Gunthorpe #define CLIENT_REGISTERED XA_MARK_1 98e59178d8SJason Gunthorpe static DEFINE_XARRAY_FLAGS(clients, XA_FLAGS_ALLOC); 99921eab11SJason Gunthorpe static DECLARE_RWSEM(clients_rwsem); 1001da177e4SLinus Torvalds 101621e55ffSJason Gunthorpe static void ib_client_put(struct ib_client *client) 102621e55ffSJason Gunthorpe { 103621e55ffSJason Gunthorpe if (refcount_dec_and_test(&client->uses)) 104621e55ffSJason Gunthorpe complete(&client->uses_zero); 105621e55ffSJason Gunthorpe } 106621e55ffSJason Gunthorpe 1071da177e4SLinus Torvalds /* 1080df91bb6SJason Gunthorpe * If client_data is registered then the corresponding client must also still 1090df91bb6SJason Gunthorpe * be registered. 1100df91bb6SJason Gunthorpe */ 1110df91bb6SJason Gunthorpe #define CLIENT_DATA_REGISTERED XA_MARK_1 1124e0f7b90SParav Pandit 1131d2fedd8SParav Pandit unsigned int rdma_dev_net_id; 1144e0f7b90SParav Pandit 1154e0f7b90SParav Pandit /* 1164e0f7b90SParav Pandit * A list of net namespaces is maintained in an xarray. This is necessary 1174e0f7b90SParav Pandit * because we can't get the locking right using the existing net ns list. We 1184e0f7b90SParav Pandit * would require a init_net callback after the list is updated. 1194e0f7b90SParav Pandit */ 1204e0f7b90SParav Pandit static DEFINE_XARRAY_FLAGS(rdma_nets, XA_FLAGS_ALLOC); 1214e0f7b90SParav Pandit /* 1224e0f7b90SParav Pandit * rwsem to protect accessing the rdma_nets xarray entries. 1234e0f7b90SParav Pandit */ 1244e0f7b90SParav Pandit static DECLARE_RWSEM(rdma_nets_rwsem); 1254e0f7b90SParav Pandit 126cb7e0e13SParav Pandit bool ib_devices_shared_netns = true; 127a56bc45bSParav Pandit module_param_named(netns_mode, ib_devices_shared_netns, bool, 0444); 128a56bc45bSParav Pandit MODULE_PARM_DESC(netns_mode, 129a56bc45bSParav Pandit "Share device among net namespaces; default=1 (shared)"); 13041c61401SParav Pandit /** 13141c61401SParav Pandit * rdma_dev_access_netns() - Return whether a rdma device can be accessed 13241c61401SParav Pandit * from a specified net namespace or not. 13341c61401SParav Pandit * @device: Pointer to rdma device which needs to be checked 13441c61401SParav Pandit * @net: Pointer to net namesapce for which access to be checked 13541c61401SParav Pandit * 13641c61401SParav Pandit * rdma_dev_access_netns() - Return whether a rdma device can be accessed 13741c61401SParav Pandit * from a specified net namespace or not. When 13841c61401SParav Pandit * rdma device is in shared mode, it ignores the 13941c61401SParav Pandit * net namespace. When rdma device is exclusive 14041c61401SParav Pandit * to a net namespace, rdma device net namespace is 14141c61401SParav Pandit * checked against the specified one. 14241c61401SParav Pandit */ 14341c61401SParav Pandit bool rdma_dev_access_netns(const struct ib_device *dev, const struct net *net) 14441c61401SParav Pandit { 14541c61401SParav Pandit return (ib_devices_shared_netns || 14641c61401SParav Pandit net_eq(read_pnet(&dev->coredev.rdma_net), net)); 14741c61401SParav Pandit } 14841c61401SParav Pandit EXPORT_SYMBOL(rdma_dev_access_netns); 14941c61401SParav Pandit 1500df91bb6SJason Gunthorpe /* 1510df91bb6SJason Gunthorpe * xarray has this behavior where it won't iterate over NULL values stored in 1520df91bb6SJason Gunthorpe * allocated arrays. So we need our own iterator to see all values stored in 1530df91bb6SJason Gunthorpe * the array. This does the same thing as xa_for_each except that it also 1540df91bb6SJason Gunthorpe * returns NULL valued entries if the array is allocating. Simplified to only 1550df91bb6SJason Gunthorpe * work on simple xarrays. 1560df91bb6SJason Gunthorpe */ 1570df91bb6SJason Gunthorpe static void *xan_find_marked(struct xarray *xa, unsigned long *indexp, 1580df91bb6SJason Gunthorpe xa_mark_t filter) 1590df91bb6SJason Gunthorpe { 1600df91bb6SJason Gunthorpe XA_STATE(xas, xa, *indexp); 1610df91bb6SJason Gunthorpe void *entry; 1620df91bb6SJason Gunthorpe 1630df91bb6SJason Gunthorpe rcu_read_lock(); 1640df91bb6SJason Gunthorpe do { 1650df91bb6SJason Gunthorpe entry = xas_find_marked(&xas, ULONG_MAX, filter); 1660df91bb6SJason Gunthorpe if (xa_is_zero(entry)) 1670df91bb6SJason Gunthorpe break; 1680df91bb6SJason Gunthorpe } while (xas_retry(&xas, entry)); 1690df91bb6SJason Gunthorpe rcu_read_unlock(); 1700df91bb6SJason Gunthorpe 1710df91bb6SJason Gunthorpe if (entry) { 1720df91bb6SJason Gunthorpe *indexp = xas.xa_index; 1730df91bb6SJason Gunthorpe if (xa_is_zero(entry)) 1740df91bb6SJason Gunthorpe return NULL; 1750df91bb6SJason Gunthorpe return entry; 1760df91bb6SJason Gunthorpe } 1770df91bb6SJason Gunthorpe return XA_ERROR(-ENOENT); 1780df91bb6SJason Gunthorpe } 1790df91bb6SJason Gunthorpe #define xan_for_each_marked(xa, index, entry, filter) \ 1800df91bb6SJason Gunthorpe for (index = 0, entry = xan_find_marked(xa, &(index), filter); \ 1810df91bb6SJason Gunthorpe !xa_is_err(entry); \ 1820df91bb6SJason Gunthorpe (index)++, entry = xan_find_marked(xa, &(index), filter)) 1830df91bb6SJason Gunthorpe 184324e227eSJason Gunthorpe /* RCU hash table mapping netdevice pointers to struct ib_port_data */ 185324e227eSJason Gunthorpe static DEFINE_SPINLOCK(ndev_hash_lock); 186324e227eSJason Gunthorpe static DECLARE_HASHTABLE(ndev_hash, 5); 187324e227eSJason Gunthorpe 188c2261dd7SJason Gunthorpe static void free_netdevs(struct ib_device *ib_dev); 189d0899892SJason Gunthorpe static void ib_unregister_work(struct work_struct *work); 190d0899892SJason Gunthorpe static void __ib_unregister_device(struct ib_device *device); 1918f408ab6SDaniel Jurgens static int ib_security_change(struct notifier_block *nb, unsigned long event, 1928f408ab6SDaniel Jurgens void *lsm_data); 1938f408ab6SDaniel Jurgens static void ib_policy_change_task(struct work_struct *work); 1948f408ab6SDaniel Jurgens static DECLARE_WORK(ib_policy_change_work, ib_policy_change_task); 1958f408ab6SDaniel Jurgens 196923abb9dSGal Pressman static void __ibdev_printk(const char *level, const struct ib_device *ibdev, 197923abb9dSGal Pressman struct va_format *vaf) 198923abb9dSGal Pressman { 199923abb9dSGal Pressman if (ibdev && ibdev->dev.parent) 200923abb9dSGal Pressman dev_printk_emit(level[1] - '0', 201923abb9dSGal Pressman ibdev->dev.parent, 202923abb9dSGal Pressman "%s %s %s: %pV", 203923abb9dSGal Pressman dev_driver_string(ibdev->dev.parent), 204923abb9dSGal Pressman dev_name(ibdev->dev.parent), 205923abb9dSGal Pressman dev_name(&ibdev->dev), 206923abb9dSGal Pressman vaf); 207923abb9dSGal Pressman else if (ibdev) 208923abb9dSGal Pressman printk("%s%s: %pV", 209923abb9dSGal Pressman level, dev_name(&ibdev->dev), vaf); 210923abb9dSGal Pressman else 211923abb9dSGal Pressman printk("%s(NULL ib_device): %pV", level, vaf); 212923abb9dSGal Pressman } 213923abb9dSGal Pressman 214923abb9dSGal Pressman void ibdev_printk(const char *level, const struct ib_device *ibdev, 215923abb9dSGal Pressman const char *format, ...) 216923abb9dSGal Pressman { 217923abb9dSGal Pressman struct va_format vaf; 218923abb9dSGal Pressman va_list args; 219923abb9dSGal Pressman 220923abb9dSGal Pressman va_start(args, format); 221923abb9dSGal Pressman 222923abb9dSGal Pressman vaf.fmt = format; 223923abb9dSGal Pressman vaf.va = &args; 224923abb9dSGal Pressman 225923abb9dSGal Pressman __ibdev_printk(level, ibdev, &vaf); 226923abb9dSGal Pressman 227923abb9dSGal Pressman va_end(args); 228923abb9dSGal Pressman } 229923abb9dSGal Pressman EXPORT_SYMBOL(ibdev_printk); 230923abb9dSGal Pressman 231923abb9dSGal Pressman #define define_ibdev_printk_level(func, level) \ 232923abb9dSGal Pressman void func(const struct ib_device *ibdev, const char *fmt, ...) \ 233923abb9dSGal Pressman { \ 234923abb9dSGal Pressman struct va_format vaf; \ 235923abb9dSGal Pressman va_list args; \ 236923abb9dSGal Pressman \ 237923abb9dSGal Pressman va_start(args, fmt); \ 238923abb9dSGal Pressman \ 239923abb9dSGal Pressman vaf.fmt = fmt; \ 240923abb9dSGal Pressman vaf.va = &args; \ 241923abb9dSGal Pressman \ 242923abb9dSGal Pressman __ibdev_printk(level, ibdev, &vaf); \ 243923abb9dSGal Pressman \ 244923abb9dSGal Pressman va_end(args); \ 245923abb9dSGal Pressman } \ 246923abb9dSGal Pressman EXPORT_SYMBOL(func); 247923abb9dSGal Pressman 248923abb9dSGal Pressman define_ibdev_printk_level(ibdev_emerg, KERN_EMERG); 249923abb9dSGal Pressman define_ibdev_printk_level(ibdev_alert, KERN_ALERT); 250923abb9dSGal Pressman define_ibdev_printk_level(ibdev_crit, KERN_CRIT); 251923abb9dSGal Pressman define_ibdev_printk_level(ibdev_err, KERN_ERR); 252923abb9dSGal Pressman define_ibdev_printk_level(ibdev_warn, KERN_WARNING); 253923abb9dSGal Pressman define_ibdev_printk_level(ibdev_notice, KERN_NOTICE); 254923abb9dSGal Pressman define_ibdev_printk_level(ibdev_info, KERN_INFO); 255923abb9dSGal Pressman 2568f408ab6SDaniel Jurgens static struct notifier_block ibdev_lsm_nb = { 2578f408ab6SDaniel Jurgens .notifier_call = ib_security_change, 2588f408ab6SDaniel Jurgens }; 2591da177e4SLinus Torvalds 260decbc7a6SParav Pandit static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net, 261decbc7a6SParav Pandit struct net *net); 262decbc7a6SParav Pandit 263324e227eSJason Gunthorpe /* Pointer to the RCU head at the start of the ib_port_data array */ 264324e227eSJason Gunthorpe struct ib_port_data_rcu { 265324e227eSJason Gunthorpe struct rcu_head rcu_head; 266324e227eSJason Gunthorpe struct ib_port_data pdata[]; 267324e227eSJason Gunthorpe }; 268324e227eSJason Gunthorpe 269deee3c7eSKamal Heib static void ib_device_check_mandatory(struct ib_device *device) 2701da177e4SLinus Torvalds { 2713023a1e9SKamal Heib #define IB_MANDATORY_FUNC(x) { offsetof(struct ib_device_ops, x), #x } 2721da177e4SLinus Torvalds static const struct { 2731da177e4SLinus Torvalds size_t offset; 2741da177e4SLinus Torvalds char *name; 2751da177e4SLinus Torvalds } mandatory_table[] = { 2761da177e4SLinus Torvalds IB_MANDATORY_FUNC(query_device), 2771da177e4SLinus Torvalds IB_MANDATORY_FUNC(query_port), 2781da177e4SLinus Torvalds IB_MANDATORY_FUNC(query_pkey), 2791da177e4SLinus Torvalds IB_MANDATORY_FUNC(alloc_pd), 2801da177e4SLinus Torvalds IB_MANDATORY_FUNC(dealloc_pd), 2811da177e4SLinus Torvalds IB_MANDATORY_FUNC(create_qp), 2821da177e4SLinus Torvalds IB_MANDATORY_FUNC(modify_qp), 2831da177e4SLinus Torvalds IB_MANDATORY_FUNC(destroy_qp), 2841da177e4SLinus Torvalds IB_MANDATORY_FUNC(post_send), 2851da177e4SLinus Torvalds IB_MANDATORY_FUNC(post_recv), 2861da177e4SLinus Torvalds IB_MANDATORY_FUNC(create_cq), 2871da177e4SLinus Torvalds IB_MANDATORY_FUNC(destroy_cq), 2881da177e4SLinus Torvalds IB_MANDATORY_FUNC(poll_cq), 2891da177e4SLinus Torvalds IB_MANDATORY_FUNC(req_notify_cq), 2901da177e4SLinus Torvalds IB_MANDATORY_FUNC(get_dma_mr), 2917738613eSIra Weiny IB_MANDATORY_FUNC(dereg_mr), 2927738613eSIra Weiny IB_MANDATORY_FUNC(get_port_immutable) 2931da177e4SLinus Torvalds }; 2941da177e4SLinus Torvalds int i; 2951da177e4SLinus Torvalds 2966780c4faSGal Pressman device->kverbs_provider = true; 2979a6b090cSAhmed S. Darwish for (i = 0; i < ARRAY_SIZE(mandatory_table); ++i) { 2983023a1e9SKamal Heib if (!*(void **) ((void *) &device->ops + 2993023a1e9SKamal Heib mandatory_table[i].offset)) { 3006780c4faSGal Pressman device->kverbs_provider = false; 3016780c4faSGal Pressman break; 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds 306f8978bd9SLeon Romanovsky /* 30701b67117SParav Pandit * Caller must perform ib_device_put() to return the device reference count 30801b67117SParav Pandit * when ib_device_get_by_index() returns valid device pointer. 309f8978bd9SLeon Romanovsky */ 31037eeab55SParav Pandit struct ib_device *ib_device_get_by_index(const struct net *net, u32 index) 311f8978bd9SLeon Romanovsky { 312f8978bd9SLeon Romanovsky struct ib_device *device; 313f8978bd9SLeon Romanovsky 314921eab11SJason Gunthorpe down_read(&devices_rwsem); 3150df91bb6SJason Gunthorpe device = xa_load(&devices, index); 31601b67117SParav Pandit if (device) { 31737eeab55SParav Pandit if (!rdma_dev_access_netns(device, net)) { 31837eeab55SParav Pandit device = NULL; 31937eeab55SParav Pandit goto out; 32037eeab55SParav Pandit } 32137eeab55SParav Pandit 322d79af724SJason Gunthorpe if (!ib_device_try_get(device)) 32301b67117SParav Pandit device = NULL; 32401b67117SParav Pandit } 32537eeab55SParav Pandit out: 326921eab11SJason Gunthorpe up_read(&devices_rwsem); 327f8978bd9SLeon Romanovsky return device; 328f8978bd9SLeon Romanovsky } 329f8978bd9SLeon Romanovsky 330d79af724SJason Gunthorpe /** 331d79af724SJason Gunthorpe * ib_device_put - Release IB device reference 332d79af724SJason Gunthorpe * @device: device whose reference to be released 333d79af724SJason Gunthorpe * 334d79af724SJason Gunthorpe * ib_device_put() releases reference to the IB device to allow it to be 335d79af724SJason Gunthorpe * unregistered and eventually free. 336d79af724SJason Gunthorpe */ 33701b67117SParav Pandit void ib_device_put(struct ib_device *device) 33801b67117SParav Pandit { 33901b67117SParav Pandit if (refcount_dec_and_test(&device->refcount)) 34001b67117SParav Pandit complete(&device->unreg_completion); 34101b67117SParav Pandit } 342d79af724SJason Gunthorpe EXPORT_SYMBOL(ib_device_put); 34301b67117SParav Pandit 3441da177e4SLinus Torvalds static struct ib_device *__ib_device_get_by_name(const char *name) 3451da177e4SLinus Torvalds { 3461da177e4SLinus Torvalds struct ib_device *device; 3470df91bb6SJason Gunthorpe unsigned long index; 3481da177e4SLinus Torvalds 3490df91bb6SJason Gunthorpe xa_for_each (&devices, index, device) 350896de009SJason Gunthorpe if (!strcmp(name, dev_name(&device->dev))) 3511da177e4SLinus Torvalds return device; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds return NULL; 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds 3566cc2c8e5SJason Gunthorpe /** 3576cc2c8e5SJason Gunthorpe * ib_device_get_by_name - Find an IB device by name 3586cc2c8e5SJason Gunthorpe * @name: The name to look for 3596cc2c8e5SJason Gunthorpe * @driver_id: The driver ID that must match (RDMA_DRIVER_UNKNOWN matches all) 3606cc2c8e5SJason Gunthorpe * 3616cc2c8e5SJason Gunthorpe * Find and hold an ib_device by its name. The caller must call 3626cc2c8e5SJason Gunthorpe * ib_device_put() on the returned pointer. 3636cc2c8e5SJason Gunthorpe */ 3646cc2c8e5SJason Gunthorpe struct ib_device *ib_device_get_by_name(const char *name, 3656cc2c8e5SJason Gunthorpe enum rdma_driver_id driver_id) 3666cc2c8e5SJason Gunthorpe { 3676cc2c8e5SJason Gunthorpe struct ib_device *device; 3686cc2c8e5SJason Gunthorpe 3696cc2c8e5SJason Gunthorpe down_read(&devices_rwsem); 3706cc2c8e5SJason Gunthorpe device = __ib_device_get_by_name(name); 3716cc2c8e5SJason Gunthorpe if (device && driver_id != RDMA_DRIVER_UNKNOWN && 372b9560a41SJason Gunthorpe device->ops.driver_id != driver_id) 3736cc2c8e5SJason Gunthorpe device = NULL; 3746cc2c8e5SJason Gunthorpe 3756cc2c8e5SJason Gunthorpe if (device) { 3766cc2c8e5SJason Gunthorpe if (!ib_device_try_get(device)) 3776cc2c8e5SJason Gunthorpe device = NULL; 3786cc2c8e5SJason Gunthorpe } 3796cc2c8e5SJason Gunthorpe up_read(&devices_rwsem); 3806cc2c8e5SJason Gunthorpe return device; 3816cc2c8e5SJason Gunthorpe } 3826cc2c8e5SJason Gunthorpe EXPORT_SYMBOL(ib_device_get_by_name); 3836cc2c8e5SJason Gunthorpe 3844e0f7b90SParav Pandit static int rename_compat_devs(struct ib_device *device) 3854e0f7b90SParav Pandit { 3864e0f7b90SParav Pandit struct ib_core_device *cdev; 3874e0f7b90SParav Pandit unsigned long index; 3884e0f7b90SParav Pandit int ret = 0; 3894e0f7b90SParav Pandit 3904e0f7b90SParav Pandit mutex_lock(&device->compat_devs_mutex); 3914e0f7b90SParav Pandit xa_for_each (&device->compat_devs, index, cdev) { 3924e0f7b90SParav Pandit ret = device_rename(&cdev->dev, dev_name(&device->dev)); 3934e0f7b90SParav Pandit if (ret) { 3944e0f7b90SParav Pandit dev_warn(&cdev->dev, 3954e0f7b90SParav Pandit "Fail to rename compatdev to new name %s\n", 3964e0f7b90SParav Pandit dev_name(&device->dev)); 3974e0f7b90SParav Pandit break; 3984e0f7b90SParav Pandit } 3994e0f7b90SParav Pandit } 4004e0f7b90SParav Pandit mutex_unlock(&device->compat_devs_mutex); 4014e0f7b90SParav Pandit return ret; 4024e0f7b90SParav Pandit } 4034e0f7b90SParav Pandit 404d21943ddSLeon Romanovsky int ib_device_rename(struct ib_device *ibdev, const char *name) 405d21943ddSLeon Romanovsky { 406dc1435c0SLeon Romanovsky unsigned long index; 407dc1435c0SLeon Romanovsky void *client_data; 408e3593b56SJason Gunthorpe int ret; 409d21943ddSLeon Romanovsky 410921eab11SJason Gunthorpe down_write(&devices_rwsem); 411e3593b56SJason Gunthorpe if (!strcmp(name, dev_name(&ibdev->dev))) { 412dc1435c0SLeon Romanovsky up_write(&devices_rwsem); 413dc1435c0SLeon Romanovsky return 0; 414e3593b56SJason Gunthorpe } 415e3593b56SJason Gunthorpe 416344684e6SJason Gunthorpe if (__ib_device_get_by_name(name)) { 417dc1435c0SLeon Romanovsky up_write(&devices_rwsem); 418dc1435c0SLeon Romanovsky return -EEXIST; 419d21943ddSLeon Romanovsky } 420d21943ddSLeon Romanovsky 421d21943ddSLeon Romanovsky ret = device_rename(&ibdev->dev, name); 422dc1435c0SLeon Romanovsky if (ret) { 423921eab11SJason Gunthorpe up_write(&devices_rwsem); 424d21943ddSLeon Romanovsky return ret; 425d21943ddSLeon Romanovsky } 426d21943ddSLeon Romanovsky 427dc1435c0SLeon Romanovsky strlcpy(ibdev->name, name, IB_DEVICE_NAME_MAX); 428dc1435c0SLeon Romanovsky ret = rename_compat_devs(ibdev); 429dc1435c0SLeon Romanovsky 430dc1435c0SLeon Romanovsky downgrade_write(&devices_rwsem); 431dc1435c0SLeon Romanovsky down_read(&ibdev->client_data_rwsem); 432dc1435c0SLeon Romanovsky xan_for_each_marked(&ibdev->client_data, index, client_data, 433dc1435c0SLeon Romanovsky CLIENT_DATA_REGISTERED) { 434dc1435c0SLeon Romanovsky struct ib_client *client = xa_load(&clients, index); 435dc1435c0SLeon Romanovsky 436dc1435c0SLeon Romanovsky if (!client || !client->rename) 437dc1435c0SLeon Romanovsky continue; 438dc1435c0SLeon Romanovsky 439dc1435c0SLeon Romanovsky client->rename(ibdev, client_data); 440dc1435c0SLeon Romanovsky } 441dc1435c0SLeon Romanovsky up_read(&ibdev->client_data_rwsem); 442dc1435c0SLeon Romanovsky up_read(&devices_rwsem); 443dc1435c0SLeon Romanovsky return 0; 444dc1435c0SLeon Romanovsky } 445dc1435c0SLeon Romanovsky 446f8fc8cd9SYamin Friedman int ib_device_set_dim(struct ib_device *ibdev, u8 use_dim) 447f8fc8cd9SYamin Friedman { 448f8fc8cd9SYamin Friedman if (use_dim > 1) 449f8fc8cd9SYamin Friedman return -EINVAL; 450f8fc8cd9SYamin Friedman ibdev->use_cq_dim = use_dim; 451f8fc8cd9SYamin Friedman 452f8fc8cd9SYamin Friedman return 0; 453f8fc8cd9SYamin Friedman } 454f8fc8cd9SYamin Friedman 455e349f858SJason Gunthorpe static int alloc_name(struct ib_device *ibdev, const char *name) 4561da177e4SLinus Torvalds { 4571da177e4SLinus Torvalds struct ib_device *device; 4580df91bb6SJason Gunthorpe unsigned long index; 4593b88afd3SJason Gunthorpe struct ida inuse; 4603b88afd3SJason Gunthorpe int rc; 4611da177e4SLinus Torvalds int i; 4621da177e4SLinus Torvalds 4639ffbe8acSNikolay Borisov lockdep_assert_held_write(&devices_rwsem); 4643b88afd3SJason Gunthorpe ida_init(&inuse); 4650df91bb6SJason Gunthorpe xa_for_each (&devices, index, device) { 466e349f858SJason Gunthorpe char buf[IB_DEVICE_NAME_MAX]; 467e349f858SJason Gunthorpe 468896de009SJason Gunthorpe if (sscanf(dev_name(&device->dev), name, &i) != 1) 4691da177e4SLinus Torvalds continue; 4703b88afd3SJason Gunthorpe if (i < 0 || i >= INT_MAX) 4711da177e4SLinus Torvalds continue; 4721da177e4SLinus Torvalds snprintf(buf, sizeof buf, name, i); 4733b88afd3SJason Gunthorpe if (strcmp(buf, dev_name(&device->dev)) != 0) 4743b88afd3SJason Gunthorpe continue; 4753b88afd3SJason Gunthorpe 4763b88afd3SJason Gunthorpe rc = ida_alloc_range(&inuse, i, i, GFP_KERNEL); 4773b88afd3SJason Gunthorpe if (rc < 0) 4783b88afd3SJason Gunthorpe goto out; 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds 4813b88afd3SJason Gunthorpe rc = ida_alloc(&inuse, GFP_KERNEL); 4823b88afd3SJason Gunthorpe if (rc < 0) 4833b88afd3SJason Gunthorpe goto out; 4841da177e4SLinus Torvalds 4853b88afd3SJason Gunthorpe rc = dev_set_name(&ibdev->dev, name, rc); 4863b88afd3SJason Gunthorpe out: 4873b88afd3SJason Gunthorpe ida_destroy(&inuse); 4883b88afd3SJason Gunthorpe return rc; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 49155aeed06SJason Gunthorpe static void ib_device_release(struct device *device) 49255aeed06SJason Gunthorpe { 49355aeed06SJason Gunthorpe struct ib_device *dev = container_of(device, struct ib_device, dev); 49455aeed06SJason Gunthorpe 495c2261dd7SJason Gunthorpe free_netdevs(dev); 496652432f3SJason Gunthorpe WARN_ON(refcount_read(&dev->refcount)); 49746bdf370SKamal Heib if (dev->port_data) { 49803db3a2dSMatan Barak ib_cache_release_one(dev); 499b34b269aSJason Gunthorpe ib_security_release_port_pkey_list(dev); 500413d3347SMark Zhang rdma_counter_release(dev); 501324e227eSJason Gunthorpe kfree_rcu(container_of(dev->port_data, struct ib_port_data_rcu, 502324e227eSJason Gunthorpe pdata[0]), 503324e227eSJason Gunthorpe rcu_head); 50446bdf370SKamal Heib } 505413d3347SMark Zhang 50656594ae1SParav Pandit mutex_destroy(&dev->unregistration_lock); 50756594ae1SParav Pandit mutex_destroy(&dev->compat_devs_mutex); 50856594ae1SParav Pandit 50946bdf370SKamal Heib xa_destroy(&dev->compat_devs); 51046bdf370SKamal Heib xa_destroy(&dev->client_data); 511324e227eSJason Gunthorpe kfree_rcu(dev, rcu_head); 51255aeed06SJason Gunthorpe } 51355aeed06SJason Gunthorpe 51455aeed06SJason Gunthorpe static int ib_device_uevent(struct device *device, 51555aeed06SJason Gunthorpe struct kobj_uevent_env *env) 51655aeed06SJason Gunthorpe { 517896de009SJason Gunthorpe if (add_uevent_var(env, "NAME=%s", dev_name(device))) 51855aeed06SJason Gunthorpe return -ENOMEM; 51955aeed06SJason Gunthorpe 52055aeed06SJason Gunthorpe /* 52155aeed06SJason Gunthorpe * It would be nice to pass the node GUID with the event... 52255aeed06SJason Gunthorpe */ 52355aeed06SJason Gunthorpe 52455aeed06SJason Gunthorpe return 0; 52555aeed06SJason Gunthorpe } 52655aeed06SJason Gunthorpe 52762dfa795SParav Pandit static const void *net_namespace(struct device *d) 52862dfa795SParav Pandit { 5294e0f7b90SParav Pandit struct ib_core_device *coredev = 5304e0f7b90SParav Pandit container_of(d, struct ib_core_device, dev); 5314e0f7b90SParav Pandit 5324e0f7b90SParav Pandit return read_pnet(&coredev->rdma_net); 53362dfa795SParav Pandit } 53462dfa795SParav Pandit 53555aeed06SJason Gunthorpe static struct class ib_class = { 53655aeed06SJason Gunthorpe .name = "infiniband", 53755aeed06SJason Gunthorpe .dev_release = ib_device_release, 53855aeed06SJason Gunthorpe .dev_uevent = ib_device_uevent, 53962dfa795SParav Pandit .ns_type = &net_ns_type_operations, 54062dfa795SParav Pandit .namespace = net_namespace, 54155aeed06SJason Gunthorpe }; 54255aeed06SJason Gunthorpe 543cebe556bSParav Pandit static void rdma_init_coredev(struct ib_core_device *coredev, 5444e0f7b90SParav Pandit struct ib_device *dev, struct net *net) 545cebe556bSParav Pandit { 546cebe556bSParav Pandit /* This BUILD_BUG_ON is intended to catch layout change 547cebe556bSParav Pandit * of union of ib_core_device and device. 548cebe556bSParav Pandit * dev must be the first element as ib_core and providers 549cebe556bSParav Pandit * driver uses it. Adding anything in ib_core_device before 550cebe556bSParav Pandit * device will break this assumption. 551cebe556bSParav Pandit */ 552cebe556bSParav Pandit BUILD_BUG_ON(offsetof(struct ib_device, coredev.dev) != 553cebe556bSParav Pandit offsetof(struct ib_device, dev)); 554cebe556bSParav Pandit 555cebe556bSParav Pandit coredev->dev.class = &ib_class; 556cebe556bSParav Pandit coredev->dev.groups = dev->groups; 557cebe556bSParav Pandit device_initialize(&coredev->dev); 558cebe556bSParav Pandit coredev->owner = dev; 559cebe556bSParav Pandit INIT_LIST_HEAD(&coredev->port_list); 5604e0f7b90SParav Pandit write_pnet(&coredev->rdma_net, net); 561cebe556bSParav Pandit } 562cebe556bSParav Pandit 5631da177e4SLinus Torvalds /** 564459cc69fSLeon Romanovsky * _ib_alloc_device - allocate an IB device struct 5651da177e4SLinus Torvalds * @size:size of structure to allocate 5661da177e4SLinus Torvalds * 5671da177e4SLinus Torvalds * Low-level drivers should use ib_alloc_device() to allocate &struct 5681da177e4SLinus Torvalds * ib_device. @size is the size of the structure to be allocated, 5691da177e4SLinus Torvalds * including any private data used by the low-level driver. 5701da177e4SLinus Torvalds * ib_dealloc_device() must be used to free structures allocated with 5711da177e4SLinus Torvalds * ib_alloc_device(). 5721da177e4SLinus Torvalds */ 573459cc69fSLeon Romanovsky struct ib_device *_ib_alloc_device(size_t size) 5741da177e4SLinus Torvalds { 57555aeed06SJason Gunthorpe struct ib_device *device; 5761da177e4SLinus Torvalds 57755aeed06SJason Gunthorpe if (WARN_ON(size < sizeof(struct ib_device))) 57855aeed06SJason Gunthorpe return NULL; 57955aeed06SJason Gunthorpe 58055aeed06SJason Gunthorpe device = kzalloc(size, GFP_KERNEL); 58155aeed06SJason Gunthorpe if (!device) 58255aeed06SJason Gunthorpe return NULL; 58355aeed06SJason Gunthorpe 58441eda65cSLeon Romanovsky if (rdma_restrack_init(device)) { 58541eda65cSLeon Romanovsky kfree(device); 58641eda65cSLeon Romanovsky return NULL; 58741eda65cSLeon Romanovsky } 58802d8883fSLeon Romanovsky 5895f8f5499SParav Pandit device->groups[0] = &ib_dev_attr_group; 5904e0f7b90SParav Pandit rdma_init_coredev(&device->coredev, device, &init_net); 59155aeed06SJason Gunthorpe 59255aeed06SJason Gunthorpe INIT_LIST_HEAD(&device->event_handler_list); 59355aeed06SJason Gunthorpe spin_lock_init(&device->event_handler_lock); 594d0899892SJason Gunthorpe mutex_init(&device->unregistration_lock); 5950df91bb6SJason Gunthorpe /* 5960df91bb6SJason Gunthorpe * client_data needs to be alloc because we don't want our mark to be 5970df91bb6SJason Gunthorpe * destroyed if the user stores NULL in the client data. 5980df91bb6SJason Gunthorpe */ 5990df91bb6SJason Gunthorpe xa_init_flags(&device->client_data, XA_FLAGS_ALLOC); 600921eab11SJason Gunthorpe init_rwsem(&device->client_data_rwsem); 6014e0f7b90SParav Pandit xa_init_flags(&device->compat_devs, XA_FLAGS_ALLOC); 6024e0f7b90SParav Pandit mutex_init(&device->compat_devs_mutex); 60301b67117SParav Pandit init_completion(&device->unreg_completion); 604d0899892SJason Gunthorpe INIT_WORK(&device->unregistration_work, ib_unregister_work); 60555aeed06SJason Gunthorpe 60655aeed06SJason Gunthorpe return device; 6071da177e4SLinus Torvalds } 608459cc69fSLeon Romanovsky EXPORT_SYMBOL(_ib_alloc_device); 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds /** 6111da177e4SLinus Torvalds * ib_dealloc_device - free an IB device struct 6121da177e4SLinus Torvalds * @device:structure to free 6131da177e4SLinus Torvalds * 6141da177e4SLinus Torvalds * Free a structure allocated with ib_alloc_device(). 6151da177e4SLinus Torvalds */ 6161da177e4SLinus Torvalds void ib_dealloc_device(struct ib_device *device) 6171da177e4SLinus Torvalds { 618d0899892SJason Gunthorpe if (device->ops.dealloc_driver) 619d0899892SJason Gunthorpe device->ops.dealloc_driver(device); 620d0899892SJason Gunthorpe 621d0899892SJason Gunthorpe /* 622d0899892SJason Gunthorpe * ib_unregister_driver() requires all devices to remain in the xarray 623d0899892SJason Gunthorpe * while their ops are callable. The last op we call is dealloc_driver 624d0899892SJason Gunthorpe * above. This is needed to create a fence on op callbacks prior to 625d0899892SJason Gunthorpe * allowing the driver module to unload. 626d0899892SJason Gunthorpe */ 627d0899892SJason Gunthorpe down_write(&devices_rwsem); 628d0899892SJason Gunthorpe if (xa_load(&devices, device->index) == device) 629d0899892SJason Gunthorpe xa_erase(&devices, device->index); 630d0899892SJason Gunthorpe up_write(&devices_rwsem); 631d0899892SJason Gunthorpe 632c2261dd7SJason Gunthorpe /* Expedite releasing netdev references */ 633c2261dd7SJason Gunthorpe free_netdevs(device); 634c2261dd7SJason Gunthorpe 6354e0f7b90SParav Pandit WARN_ON(!xa_empty(&device->compat_devs)); 6360df91bb6SJason Gunthorpe WARN_ON(!xa_empty(&device->client_data)); 637652432f3SJason Gunthorpe WARN_ON(refcount_read(&device->refcount)); 6380ad699c0SLeon Romanovsky rdma_restrack_clean(device); 639e155755eSParav Pandit /* Balances with device_initialize */ 640924b8900SLeon Romanovsky put_device(&device->dev); 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds EXPORT_SYMBOL(ib_dealloc_device); 6431da177e4SLinus Torvalds 644921eab11SJason Gunthorpe /* 645921eab11SJason Gunthorpe * add_client_context() and remove_client_context() must be safe against 646921eab11SJason Gunthorpe * parallel calls on the same device - registration/unregistration of both the 647921eab11SJason Gunthorpe * device and client can be occurring in parallel. 648921eab11SJason Gunthorpe * 649921eab11SJason Gunthorpe * The routines need to be a fence, any caller must not return until the add 650921eab11SJason Gunthorpe * or remove is fully completed. 651921eab11SJason Gunthorpe */ 652921eab11SJason Gunthorpe static int add_client_context(struct ib_device *device, 653921eab11SJason Gunthorpe struct ib_client *client) 6541da177e4SLinus Torvalds { 655921eab11SJason Gunthorpe int ret = 0; 6561da177e4SLinus Torvalds 6576780c4faSGal Pressman if (!device->kverbs_provider && !client->no_kverbs_req) 658921eab11SJason Gunthorpe return 0; 6596780c4faSGal Pressman 660921eab11SJason Gunthorpe down_write(&device->client_data_rwsem); 661921eab11SJason Gunthorpe /* 662621e55ffSJason Gunthorpe * So long as the client is registered hold both the client and device 663621e55ffSJason Gunthorpe * unregistration locks. 664621e55ffSJason Gunthorpe */ 665621e55ffSJason Gunthorpe if (!refcount_inc_not_zero(&client->uses)) 666621e55ffSJason Gunthorpe goto out_unlock; 667621e55ffSJason Gunthorpe refcount_inc(&device->refcount); 668621e55ffSJason Gunthorpe 669621e55ffSJason Gunthorpe /* 670921eab11SJason Gunthorpe * Another caller to add_client_context got here first and has already 671921eab11SJason Gunthorpe * completely initialized context. 672921eab11SJason Gunthorpe */ 673921eab11SJason Gunthorpe if (xa_get_mark(&device->client_data, client->client_id, 674921eab11SJason Gunthorpe CLIENT_DATA_REGISTERED)) 675921eab11SJason Gunthorpe goto out; 676921eab11SJason Gunthorpe 677921eab11SJason Gunthorpe ret = xa_err(xa_store(&device->client_data, client->client_id, NULL, 678921eab11SJason Gunthorpe GFP_KERNEL)); 679921eab11SJason Gunthorpe if (ret) 680921eab11SJason Gunthorpe goto out; 681921eab11SJason Gunthorpe downgrade_write(&device->client_data_rwsem); 682921eab11SJason Gunthorpe if (client->add) 683921eab11SJason Gunthorpe client->add(device); 684921eab11SJason Gunthorpe 685921eab11SJason Gunthorpe /* Readers shall not see a client until add has been completed */ 6860df91bb6SJason Gunthorpe xa_set_mark(&device->client_data, client->client_id, 6870df91bb6SJason Gunthorpe CLIENT_DATA_REGISTERED); 688921eab11SJason Gunthorpe up_read(&device->client_data_rwsem); 689921eab11SJason Gunthorpe return 0; 6901da177e4SLinus Torvalds 691921eab11SJason Gunthorpe out: 692621e55ffSJason Gunthorpe ib_device_put(device); 693621e55ffSJason Gunthorpe ib_client_put(client); 694621e55ffSJason Gunthorpe out_unlock: 695921eab11SJason Gunthorpe up_write(&device->client_data_rwsem); 696921eab11SJason Gunthorpe return ret; 697921eab11SJason Gunthorpe } 698921eab11SJason Gunthorpe 699921eab11SJason Gunthorpe static void remove_client_context(struct ib_device *device, 700921eab11SJason Gunthorpe unsigned int client_id) 701921eab11SJason Gunthorpe { 702921eab11SJason Gunthorpe struct ib_client *client; 703921eab11SJason Gunthorpe void *client_data; 704921eab11SJason Gunthorpe 705921eab11SJason Gunthorpe down_write(&device->client_data_rwsem); 706921eab11SJason Gunthorpe if (!xa_get_mark(&device->client_data, client_id, 707921eab11SJason Gunthorpe CLIENT_DATA_REGISTERED)) { 708921eab11SJason Gunthorpe up_write(&device->client_data_rwsem); 709921eab11SJason Gunthorpe return; 710921eab11SJason Gunthorpe } 711921eab11SJason Gunthorpe client_data = xa_load(&device->client_data, client_id); 712921eab11SJason Gunthorpe xa_clear_mark(&device->client_data, client_id, CLIENT_DATA_REGISTERED); 713921eab11SJason Gunthorpe client = xa_load(&clients, client_id); 714621e55ffSJason Gunthorpe up_write(&device->client_data_rwsem); 715921eab11SJason Gunthorpe 716921eab11SJason Gunthorpe /* 717921eab11SJason Gunthorpe * Notice we cannot be holding any exclusive locks when calling the 718921eab11SJason Gunthorpe * remove callback as the remove callback can recurse back into any 719921eab11SJason Gunthorpe * public functions in this module and thus try for any locks those 720921eab11SJason Gunthorpe * functions take. 721921eab11SJason Gunthorpe * 722921eab11SJason Gunthorpe * For this reason clients and drivers should not call the 723921eab11SJason Gunthorpe * unregistration functions will holdling any locks. 724921eab11SJason Gunthorpe */ 725921eab11SJason Gunthorpe if (client->remove) 726921eab11SJason Gunthorpe client->remove(device, client_data); 727921eab11SJason Gunthorpe 728921eab11SJason Gunthorpe xa_erase(&device->client_data, client_id); 729621e55ffSJason Gunthorpe ib_device_put(device); 730621e55ffSJason Gunthorpe ib_client_put(client); 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds 733c2261dd7SJason Gunthorpe static int alloc_port_data(struct ib_device *device) 7345eb620c8SYosef Etigin { 735324e227eSJason Gunthorpe struct ib_port_data_rcu *pdata_rcu; 736ea1075edSJason Gunthorpe unsigned int port; 737c2261dd7SJason Gunthorpe 738c2261dd7SJason Gunthorpe if (device->port_data) 739c2261dd7SJason Gunthorpe return 0; 740c2261dd7SJason Gunthorpe 741c2261dd7SJason Gunthorpe /* This can only be called once the physical port range is defined */ 742c2261dd7SJason Gunthorpe if (WARN_ON(!device->phys_port_cnt)) 743c2261dd7SJason Gunthorpe return -EINVAL; 7445eb620c8SYosef Etigin 7458ceb1357SJason Gunthorpe /* 7468ceb1357SJason Gunthorpe * device->port_data is indexed directly by the port number to make 7477738613eSIra Weiny * access to this data as efficient as possible. 7487738613eSIra Weiny * 7498ceb1357SJason Gunthorpe * Therefore port_data is declared as a 1 based array with potential 7508ceb1357SJason Gunthorpe * empty slots at the beginning. 7517738613eSIra Weiny */ 752324e227eSJason Gunthorpe pdata_rcu = kzalloc(struct_size(pdata_rcu, pdata, 753324e227eSJason Gunthorpe rdma_end_port(device) + 1), 754324e227eSJason Gunthorpe GFP_KERNEL); 755324e227eSJason Gunthorpe if (!pdata_rcu) 75655aeed06SJason Gunthorpe return -ENOMEM; 757324e227eSJason Gunthorpe /* 758324e227eSJason Gunthorpe * The rcu_head is put in front of the port data array and the stored 759324e227eSJason Gunthorpe * pointer is adjusted since we never need to see that member until 760324e227eSJason Gunthorpe * kfree_rcu. 761324e227eSJason Gunthorpe */ 762324e227eSJason Gunthorpe device->port_data = pdata_rcu->pdata; 7635eb620c8SYosef Etigin 764ea1075edSJason Gunthorpe rdma_for_each_port (device, port) { 7658ceb1357SJason Gunthorpe struct ib_port_data *pdata = &device->port_data[port]; 7668ceb1357SJason Gunthorpe 767324e227eSJason Gunthorpe pdata->ib_dev = device; 7688ceb1357SJason Gunthorpe spin_lock_init(&pdata->pkey_list_lock); 7698ceb1357SJason Gunthorpe INIT_LIST_HEAD(&pdata->pkey_list); 770c2261dd7SJason Gunthorpe spin_lock_init(&pdata->netdev_lock); 771324e227eSJason Gunthorpe INIT_HLIST_NODE(&pdata->ndev_hash_link); 772c2261dd7SJason Gunthorpe } 773c2261dd7SJason Gunthorpe return 0; 774c2261dd7SJason Gunthorpe } 775c2261dd7SJason Gunthorpe 776c2261dd7SJason Gunthorpe static int verify_immutable(const struct ib_device *dev, u8 port) 777c2261dd7SJason Gunthorpe { 778c2261dd7SJason Gunthorpe return WARN_ON(!rdma_cap_ib_mad(dev, port) && 779c2261dd7SJason Gunthorpe rdma_max_mad_size(dev, port) != 0); 780c2261dd7SJason Gunthorpe } 781c2261dd7SJason Gunthorpe 782c2261dd7SJason Gunthorpe static int setup_port_data(struct ib_device *device) 783c2261dd7SJason Gunthorpe { 784c2261dd7SJason Gunthorpe unsigned int port; 785c2261dd7SJason Gunthorpe int ret; 786c2261dd7SJason Gunthorpe 787c2261dd7SJason Gunthorpe ret = alloc_port_data(device); 788c2261dd7SJason Gunthorpe if (ret) 789c2261dd7SJason Gunthorpe return ret; 790c2261dd7SJason Gunthorpe 791c2261dd7SJason Gunthorpe rdma_for_each_port (device, port) { 792c2261dd7SJason Gunthorpe struct ib_port_data *pdata = &device->port_data[port]; 7938ceb1357SJason Gunthorpe 7948ceb1357SJason Gunthorpe ret = device->ops.get_port_immutable(device, port, 7958ceb1357SJason Gunthorpe &pdata->immutable); 7965eb620c8SYosef Etigin if (ret) 7975eb620c8SYosef Etigin return ret; 79855aeed06SJason Gunthorpe 79955aeed06SJason Gunthorpe if (verify_immutable(device, port)) 80055aeed06SJason Gunthorpe return -EINVAL; 80155aeed06SJason Gunthorpe } 80255aeed06SJason Gunthorpe return 0; 8035eb620c8SYosef Etigin } 8045eb620c8SYosef Etigin 8059abb0d1bSLeon Romanovsky void ib_get_device_fw_str(struct ib_device *dev, char *str) 8065fa76c20SIra Weiny { 8073023a1e9SKamal Heib if (dev->ops.get_dev_fw_str) 8083023a1e9SKamal Heib dev->ops.get_dev_fw_str(dev, str); 8095fa76c20SIra Weiny else 8105fa76c20SIra Weiny str[0] = '\0'; 8115fa76c20SIra Weiny } 8125fa76c20SIra Weiny EXPORT_SYMBOL(ib_get_device_fw_str); 8135fa76c20SIra Weiny 8148f408ab6SDaniel Jurgens static void ib_policy_change_task(struct work_struct *work) 8158f408ab6SDaniel Jurgens { 8168f408ab6SDaniel Jurgens struct ib_device *dev; 8170df91bb6SJason Gunthorpe unsigned long index; 8188f408ab6SDaniel Jurgens 819921eab11SJason Gunthorpe down_read(&devices_rwsem); 8200df91bb6SJason Gunthorpe xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) { 821ea1075edSJason Gunthorpe unsigned int i; 8228f408ab6SDaniel Jurgens 823ea1075edSJason Gunthorpe rdma_for_each_port (dev, i) { 8248f408ab6SDaniel Jurgens u64 sp; 8258f408ab6SDaniel Jurgens int ret = ib_get_cached_subnet_prefix(dev, 8268f408ab6SDaniel Jurgens i, 8278f408ab6SDaniel Jurgens &sp); 8288f408ab6SDaniel Jurgens 8298f408ab6SDaniel Jurgens WARN_ONCE(ret, 8308f408ab6SDaniel Jurgens "ib_get_cached_subnet_prefix err: %d, this should never happen here\n", 8318f408ab6SDaniel Jurgens ret); 832a750cfdeSDaniel Jurgens if (!ret) 8338f408ab6SDaniel Jurgens ib_security_cache_change(dev, i, sp); 8348f408ab6SDaniel Jurgens } 8358f408ab6SDaniel Jurgens } 836921eab11SJason Gunthorpe up_read(&devices_rwsem); 8378f408ab6SDaniel Jurgens } 8388f408ab6SDaniel Jurgens 8398f408ab6SDaniel Jurgens static int ib_security_change(struct notifier_block *nb, unsigned long event, 8408f408ab6SDaniel Jurgens void *lsm_data) 8418f408ab6SDaniel Jurgens { 8428f408ab6SDaniel Jurgens if (event != LSM_POLICY_CHANGE) 8438f408ab6SDaniel Jurgens return NOTIFY_DONE; 8448f408ab6SDaniel Jurgens 8458f408ab6SDaniel Jurgens schedule_work(&ib_policy_change_work); 846c66f6741SDaniel Jurgens ib_mad_agent_security_change(); 8478f408ab6SDaniel Jurgens 8488f408ab6SDaniel Jurgens return NOTIFY_OK; 8498f408ab6SDaniel Jurgens } 8508f408ab6SDaniel Jurgens 8514e0f7b90SParav Pandit static void compatdev_release(struct device *dev) 8524e0f7b90SParav Pandit { 8534e0f7b90SParav Pandit struct ib_core_device *cdev = 8544e0f7b90SParav Pandit container_of(dev, struct ib_core_device, dev); 8554e0f7b90SParav Pandit 8564e0f7b90SParav Pandit kfree(cdev); 8574e0f7b90SParav Pandit } 8584e0f7b90SParav Pandit 8594e0f7b90SParav Pandit static int add_one_compat_dev(struct ib_device *device, 8604e0f7b90SParav Pandit struct rdma_dev_net *rnet) 8614e0f7b90SParav Pandit { 8624e0f7b90SParav Pandit struct ib_core_device *cdev; 8634e0f7b90SParav Pandit int ret; 8644e0f7b90SParav Pandit 8652b34c558SParav Pandit lockdep_assert_held(&rdma_nets_rwsem); 866a56bc45bSParav Pandit if (!ib_devices_shared_netns) 867a56bc45bSParav Pandit return 0; 868a56bc45bSParav Pandit 8694e0f7b90SParav Pandit /* 8704e0f7b90SParav Pandit * Create and add compat device in all namespaces other than where it 8714e0f7b90SParav Pandit * is currently bound to. 8724e0f7b90SParav Pandit */ 8734e0f7b90SParav Pandit if (net_eq(read_pnet(&rnet->net), 8744e0f7b90SParav Pandit read_pnet(&device->coredev.rdma_net))) 8754e0f7b90SParav Pandit return 0; 8764e0f7b90SParav Pandit 8774e0f7b90SParav Pandit /* 8784e0f7b90SParav Pandit * The first of init_net() or ib_register_device() to take the 8794e0f7b90SParav Pandit * compat_devs_mutex wins and gets to add the device. Others will wait 8804e0f7b90SParav Pandit * for completion here. 8814e0f7b90SParav Pandit */ 8824e0f7b90SParav Pandit mutex_lock(&device->compat_devs_mutex); 8834e0f7b90SParav Pandit cdev = xa_load(&device->compat_devs, rnet->id); 8844e0f7b90SParav Pandit if (cdev) { 8854e0f7b90SParav Pandit ret = 0; 8864e0f7b90SParav Pandit goto done; 8874e0f7b90SParav Pandit } 8884e0f7b90SParav Pandit ret = xa_reserve(&device->compat_devs, rnet->id, GFP_KERNEL); 8894e0f7b90SParav Pandit if (ret) 8904e0f7b90SParav Pandit goto done; 8914e0f7b90SParav Pandit 8924e0f7b90SParav Pandit cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); 8934e0f7b90SParav Pandit if (!cdev) { 8944e0f7b90SParav Pandit ret = -ENOMEM; 8954e0f7b90SParav Pandit goto cdev_err; 8964e0f7b90SParav Pandit } 8974e0f7b90SParav Pandit 8984e0f7b90SParav Pandit cdev->dev.parent = device->dev.parent; 8994e0f7b90SParav Pandit rdma_init_coredev(cdev, device, read_pnet(&rnet->net)); 9004e0f7b90SParav Pandit cdev->dev.release = compatdev_release; 9014e0f7b90SParav Pandit dev_set_name(&cdev->dev, "%s", dev_name(&device->dev)); 9024e0f7b90SParav Pandit 9034e0f7b90SParav Pandit ret = device_add(&cdev->dev); 9044e0f7b90SParav Pandit if (ret) 9054e0f7b90SParav Pandit goto add_err; 906eb15c78bSParav Pandit ret = ib_setup_port_attrs(cdev); 9075417783eSParav Pandit if (ret) 9085417783eSParav Pandit goto port_err; 9094e0f7b90SParav Pandit 9104e0f7b90SParav Pandit ret = xa_err(xa_store(&device->compat_devs, rnet->id, 9114e0f7b90SParav Pandit cdev, GFP_KERNEL)); 9124e0f7b90SParav Pandit if (ret) 9134e0f7b90SParav Pandit goto insert_err; 9144e0f7b90SParav Pandit 9154e0f7b90SParav Pandit mutex_unlock(&device->compat_devs_mutex); 9164e0f7b90SParav Pandit return 0; 9174e0f7b90SParav Pandit 9184e0f7b90SParav Pandit insert_err: 9195417783eSParav Pandit ib_free_port_attrs(cdev); 9205417783eSParav Pandit port_err: 9214e0f7b90SParav Pandit device_del(&cdev->dev); 9224e0f7b90SParav Pandit add_err: 9234e0f7b90SParav Pandit put_device(&cdev->dev); 9244e0f7b90SParav Pandit cdev_err: 9254e0f7b90SParav Pandit xa_release(&device->compat_devs, rnet->id); 9264e0f7b90SParav Pandit done: 9274e0f7b90SParav Pandit mutex_unlock(&device->compat_devs_mutex); 9284e0f7b90SParav Pandit return ret; 9294e0f7b90SParav Pandit } 9304e0f7b90SParav Pandit 9314e0f7b90SParav Pandit static void remove_one_compat_dev(struct ib_device *device, u32 id) 9324e0f7b90SParav Pandit { 9334e0f7b90SParav Pandit struct ib_core_device *cdev; 9344e0f7b90SParav Pandit 9354e0f7b90SParav Pandit mutex_lock(&device->compat_devs_mutex); 9364e0f7b90SParav Pandit cdev = xa_erase(&device->compat_devs, id); 9374e0f7b90SParav Pandit mutex_unlock(&device->compat_devs_mutex); 9384e0f7b90SParav Pandit if (cdev) { 9395417783eSParav Pandit ib_free_port_attrs(cdev); 9404e0f7b90SParav Pandit device_del(&cdev->dev); 9414e0f7b90SParav Pandit put_device(&cdev->dev); 9424e0f7b90SParav Pandit } 9434e0f7b90SParav Pandit } 9444e0f7b90SParav Pandit 9454e0f7b90SParav Pandit static void remove_compat_devs(struct ib_device *device) 9464e0f7b90SParav Pandit { 9474e0f7b90SParav Pandit struct ib_core_device *cdev; 9484e0f7b90SParav Pandit unsigned long index; 9494e0f7b90SParav Pandit 9504e0f7b90SParav Pandit xa_for_each (&device->compat_devs, index, cdev) 9514e0f7b90SParav Pandit remove_one_compat_dev(device, index); 9524e0f7b90SParav Pandit } 9534e0f7b90SParav Pandit 9544e0f7b90SParav Pandit static int add_compat_devs(struct ib_device *device) 9554e0f7b90SParav Pandit { 9564e0f7b90SParav Pandit struct rdma_dev_net *rnet; 9574e0f7b90SParav Pandit unsigned long index; 9584e0f7b90SParav Pandit int ret = 0; 9594e0f7b90SParav Pandit 960decbc7a6SParav Pandit lockdep_assert_held(&devices_rwsem); 961decbc7a6SParav Pandit 9624e0f7b90SParav Pandit down_read(&rdma_nets_rwsem); 9634e0f7b90SParav Pandit xa_for_each (&rdma_nets, index, rnet) { 9644e0f7b90SParav Pandit ret = add_one_compat_dev(device, rnet); 9654e0f7b90SParav Pandit if (ret) 9664e0f7b90SParav Pandit break; 9674e0f7b90SParav Pandit } 9684e0f7b90SParav Pandit up_read(&rdma_nets_rwsem); 9694e0f7b90SParav Pandit return ret; 9704e0f7b90SParav Pandit } 9714e0f7b90SParav Pandit 9722b34c558SParav Pandit static void remove_all_compat_devs(void) 9732b34c558SParav Pandit { 9742b34c558SParav Pandit struct ib_compat_device *cdev; 9752b34c558SParav Pandit struct ib_device *dev; 9762b34c558SParav Pandit unsigned long index; 9772b34c558SParav Pandit 9782b34c558SParav Pandit down_read(&devices_rwsem); 9792b34c558SParav Pandit xa_for_each (&devices, index, dev) { 9802b34c558SParav Pandit unsigned long c_index = 0; 9812b34c558SParav Pandit 9822b34c558SParav Pandit /* Hold nets_rwsem so that any other thread modifying this 9832b34c558SParav Pandit * system param can sync with this thread. 9842b34c558SParav Pandit */ 9852b34c558SParav Pandit down_read(&rdma_nets_rwsem); 9862b34c558SParav Pandit xa_for_each (&dev->compat_devs, c_index, cdev) 9872b34c558SParav Pandit remove_one_compat_dev(dev, c_index); 9882b34c558SParav Pandit up_read(&rdma_nets_rwsem); 9892b34c558SParav Pandit } 9902b34c558SParav Pandit up_read(&devices_rwsem); 9912b34c558SParav Pandit } 9922b34c558SParav Pandit 9932b34c558SParav Pandit static int add_all_compat_devs(void) 9942b34c558SParav Pandit { 9952b34c558SParav Pandit struct rdma_dev_net *rnet; 9962b34c558SParav Pandit struct ib_device *dev; 9972b34c558SParav Pandit unsigned long index; 9982b34c558SParav Pandit int ret = 0; 9992b34c558SParav Pandit 10002b34c558SParav Pandit down_read(&devices_rwsem); 10012b34c558SParav Pandit xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) { 10022b34c558SParav Pandit unsigned long net_index = 0; 10032b34c558SParav Pandit 10042b34c558SParav Pandit /* Hold nets_rwsem so that any other thread modifying this 10052b34c558SParav Pandit * system param can sync with this thread. 10062b34c558SParav Pandit */ 10072b34c558SParav Pandit down_read(&rdma_nets_rwsem); 10082b34c558SParav Pandit xa_for_each (&rdma_nets, net_index, rnet) { 10092b34c558SParav Pandit ret = add_one_compat_dev(dev, rnet); 10102b34c558SParav Pandit if (ret) 10112b34c558SParav Pandit break; 10122b34c558SParav Pandit } 10132b34c558SParav Pandit up_read(&rdma_nets_rwsem); 10142b34c558SParav Pandit } 10152b34c558SParav Pandit up_read(&devices_rwsem); 10162b34c558SParav Pandit if (ret) 10172b34c558SParav Pandit remove_all_compat_devs(); 10182b34c558SParav Pandit return ret; 10192b34c558SParav Pandit } 10202b34c558SParav Pandit 10212b34c558SParav Pandit int rdma_compatdev_set(u8 enable) 10222b34c558SParav Pandit { 10232b34c558SParav Pandit struct rdma_dev_net *rnet; 10242b34c558SParav Pandit unsigned long index; 10252b34c558SParav Pandit int ret = 0; 10262b34c558SParav Pandit 10272b34c558SParav Pandit down_write(&rdma_nets_rwsem); 10282b34c558SParav Pandit if (ib_devices_shared_netns == enable) { 10292b34c558SParav Pandit up_write(&rdma_nets_rwsem); 10302b34c558SParav Pandit return 0; 10312b34c558SParav Pandit } 10322b34c558SParav Pandit 10332b34c558SParav Pandit /* enable/disable of compat devices is not supported 10342b34c558SParav Pandit * when more than default init_net exists. 10352b34c558SParav Pandit */ 10362b34c558SParav Pandit xa_for_each (&rdma_nets, index, rnet) { 10372b34c558SParav Pandit ret++; 10382b34c558SParav Pandit break; 10392b34c558SParav Pandit } 10402b34c558SParav Pandit if (!ret) 10412b34c558SParav Pandit ib_devices_shared_netns = enable; 10422b34c558SParav Pandit up_write(&rdma_nets_rwsem); 10432b34c558SParav Pandit if (ret) 10442b34c558SParav Pandit return -EBUSY; 10452b34c558SParav Pandit 10462b34c558SParav Pandit if (enable) 10472b34c558SParav Pandit ret = add_all_compat_devs(); 10482b34c558SParav Pandit else 10492b34c558SParav Pandit remove_all_compat_devs(); 10502b34c558SParav Pandit return ret; 10512b34c558SParav Pandit } 10522b34c558SParav Pandit 10534e0f7b90SParav Pandit static void rdma_dev_exit_net(struct net *net) 10544e0f7b90SParav Pandit { 10551d2fedd8SParav Pandit struct rdma_dev_net *rnet = rdma_net_to_dev_net(net); 10564e0f7b90SParav Pandit struct ib_device *dev; 10574e0f7b90SParav Pandit unsigned long index; 10584e0f7b90SParav Pandit int ret; 10594e0f7b90SParav Pandit 10604e0f7b90SParav Pandit down_write(&rdma_nets_rwsem); 10614e0f7b90SParav Pandit /* 10624e0f7b90SParav Pandit * Prevent the ID from being re-used and hide the id from xa_for_each. 10634e0f7b90SParav Pandit */ 10644e0f7b90SParav Pandit ret = xa_err(xa_store(&rdma_nets, rnet->id, NULL, GFP_KERNEL)); 10654e0f7b90SParav Pandit WARN_ON(ret); 10664e0f7b90SParav Pandit up_write(&rdma_nets_rwsem); 10674e0f7b90SParav Pandit 10684e0f7b90SParav Pandit down_read(&devices_rwsem); 10694e0f7b90SParav Pandit xa_for_each (&devices, index, dev) { 10704e0f7b90SParav Pandit get_device(&dev->dev); 10714e0f7b90SParav Pandit /* 10724e0f7b90SParav Pandit * Release the devices_rwsem so that pontentially blocking 10734e0f7b90SParav Pandit * device_del, doesn't hold the devices_rwsem for too long. 10744e0f7b90SParav Pandit */ 10754e0f7b90SParav Pandit up_read(&devices_rwsem); 10764e0f7b90SParav Pandit 10774e0f7b90SParav Pandit remove_one_compat_dev(dev, rnet->id); 10784e0f7b90SParav Pandit 1079decbc7a6SParav Pandit /* 1080decbc7a6SParav Pandit * If the real device is in the NS then move it back to init. 1081decbc7a6SParav Pandit */ 1082decbc7a6SParav Pandit rdma_dev_change_netns(dev, net, &init_net); 1083decbc7a6SParav Pandit 10844e0f7b90SParav Pandit put_device(&dev->dev); 10854e0f7b90SParav Pandit down_read(&devices_rwsem); 10864e0f7b90SParav Pandit } 10874e0f7b90SParav Pandit up_read(&devices_rwsem); 10884e0f7b90SParav Pandit 10891d2fedd8SParav Pandit rdma_nl_net_exit(rnet); 10904e0f7b90SParav Pandit xa_erase(&rdma_nets, rnet->id); 10914e0f7b90SParav Pandit } 10924e0f7b90SParav Pandit 10934e0f7b90SParav Pandit static __net_init int rdma_dev_init_net(struct net *net) 10944e0f7b90SParav Pandit { 10951d2fedd8SParav Pandit struct rdma_dev_net *rnet = rdma_net_to_dev_net(net); 10964e0f7b90SParav Pandit unsigned long index; 10974e0f7b90SParav Pandit struct ib_device *dev; 10984e0f7b90SParav Pandit int ret; 10994e0f7b90SParav Pandit 11001d2fedd8SParav Pandit write_pnet(&rnet->net, net); 11011d2fedd8SParav Pandit 11021d2fedd8SParav Pandit ret = rdma_nl_net_init(rnet); 11031d2fedd8SParav Pandit if (ret) 11041d2fedd8SParav Pandit return ret; 11051d2fedd8SParav Pandit 11064e0f7b90SParav Pandit /* No need to create any compat devices in default init_net. */ 11074e0f7b90SParav Pandit if (net_eq(net, &init_net)) 11084e0f7b90SParav Pandit return 0; 11094e0f7b90SParav Pandit 11104e0f7b90SParav Pandit ret = xa_alloc(&rdma_nets, &rnet->id, rnet, xa_limit_32b, GFP_KERNEL); 11111d2fedd8SParav Pandit if (ret) { 11121d2fedd8SParav Pandit rdma_nl_net_exit(rnet); 11134e0f7b90SParav Pandit return ret; 11141d2fedd8SParav Pandit } 11154e0f7b90SParav Pandit 11164e0f7b90SParav Pandit down_read(&devices_rwsem); 11174e0f7b90SParav Pandit xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) { 11182b34c558SParav Pandit /* Hold nets_rwsem so that netlink command cannot change 11192b34c558SParav Pandit * system configuration for device sharing mode. 11202b34c558SParav Pandit */ 11212b34c558SParav Pandit down_read(&rdma_nets_rwsem); 11224e0f7b90SParav Pandit ret = add_one_compat_dev(dev, rnet); 11232b34c558SParav Pandit up_read(&rdma_nets_rwsem); 11244e0f7b90SParav Pandit if (ret) 11254e0f7b90SParav Pandit break; 11264e0f7b90SParav Pandit } 11274e0f7b90SParav Pandit up_read(&devices_rwsem); 11284e0f7b90SParav Pandit 11294e0f7b90SParav Pandit if (ret) 11304e0f7b90SParav Pandit rdma_dev_exit_net(net); 11314e0f7b90SParav Pandit 11324e0f7b90SParav Pandit return ret; 11334e0f7b90SParav Pandit } 11344e0f7b90SParav Pandit 1135ecc82c53SLeon Romanovsky /* 1136d0899892SJason Gunthorpe * Assign the unique string device name and the unique device index. This is 1137d0899892SJason Gunthorpe * undone by ib_dealloc_device. 1138ecc82c53SLeon Romanovsky */ 11390df91bb6SJason Gunthorpe static int assign_name(struct ib_device *device, const char *name) 11400df91bb6SJason Gunthorpe { 11410df91bb6SJason Gunthorpe static u32 last_id; 11420df91bb6SJason Gunthorpe int ret; 1143ecc82c53SLeon Romanovsky 1144921eab11SJason Gunthorpe down_write(&devices_rwsem); 11450df91bb6SJason Gunthorpe /* Assign a unique name to the device */ 11460df91bb6SJason Gunthorpe if (strchr(name, '%')) 11470df91bb6SJason Gunthorpe ret = alloc_name(device, name); 11480df91bb6SJason Gunthorpe else 11490df91bb6SJason Gunthorpe ret = dev_set_name(&device->dev, name); 11500df91bb6SJason Gunthorpe if (ret) 11510df91bb6SJason Gunthorpe goto out; 1152ecc82c53SLeon Romanovsky 11530df91bb6SJason Gunthorpe if (__ib_device_get_by_name(dev_name(&device->dev))) { 11540df91bb6SJason Gunthorpe ret = -ENFILE; 11550df91bb6SJason Gunthorpe goto out; 1156ecc82c53SLeon Romanovsky } 11570df91bb6SJason Gunthorpe strlcpy(device->name, dev_name(&device->dev), IB_DEVICE_NAME_MAX); 11580df91bb6SJason Gunthorpe 1159ea295481SLinus Torvalds ret = xa_alloc_cyclic(&devices, &device->index, device, xa_limit_31b, 1160ea295481SLinus Torvalds &last_id, GFP_KERNEL); 1161ea295481SLinus Torvalds if (ret > 0) 11620df91bb6SJason Gunthorpe ret = 0; 1163921eab11SJason Gunthorpe 11640df91bb6SJason Gunthorpe out: 1165921eab11SJason Gunthorpe up_write(&devices_rwsem); 11660df91bb6SJason Gunthorpe return ret; 11670df91bb6SJason Gunthorpe } 11680df91bb6SJason Gunthorpe 1169548cb4fbSParav Pandit static void setup_dma_device(struct ib_device *device) 11701da177e4SLinus Torvalds { 117199db9494SBart Van Assche struct device *parent = device->dev.parent; 11721da177e4SLinus Torvalds 11730957c29fSBart Van Assche WARN_ON_ONCE(device->dma_device); 11740957c29fSBart Van Assche if (device->dev.dma_ops) { 11750957c29fSBart Van Assche /* 11760957c29fSBart Van Assche * The caller provided custom DMA operations. Copy the 11770957c29fSBart Van Assche * DMA-related fields that are used by e.g. dma_alloc_coherent() 11780957c29fSBart Van Assche * into device->dev. 11790957c29fSBart Van Assche */ 11800957c29fSBart Van Assche device->dma_device = &device->dev; 118102ee9da3SBart Van Assche if (!device->dev.dma_mask) { 118202ee9da3SBart Van Assche if (parent) 118399db9494SBart Van Assche device->dev.dma_mask = parent->dma_mask; 118402ee9da3SBart Van Assche else 118502ee9da3SBart Van Assche WARN_ON_ONCE(true); 118602ee9da3SBart Van Assche } 118702ee9da3SBart Van Assche if (!device->dev.coherent_dma_mask) { 118802ee9da3SBart Van Assche if (parent) 11890957c29fSBart Van Assche device->dev.coherent_dma_mask = 11900957c29fSBart Van Assche parent->coherent_dma_mask; 119102ee9da3SBart Van Assche else 119202ee9da3SBart Van Assche WARN_ON_ONCE(true); 119302ee9da3SBart Van Assche } 11940957c29fSBart Van Assche } else { 11950957c29fSBart Van Assche /* 11960957c29fSBart Van Assche * The caller did not provide custom DMA operations. Use the 11970957c29fSBart Van Assche * DMA mapping operations of the parent device. 11980957c29fSBart Van Assche */ 119902ee9da3SBart Van Assche WARN_ON_ONCE(!parent); 12000957c29fSBart Van Assche device->dma_device = parent; 12010957c29fSBart Van Assche } 1202d10bcf94SShiraz Saleem /* Setup default max segment size for all IB devices */ 1203d10bcf94SShiraz Saleem dma_set_max_seg_size(device->dma_device, SZ_2G); 1204d10bcf94SShiraz Saleem 1205548cb4fbSParav Pandit } 1206548cb4fbSParav Pandit 1207921eab11SJason Gunthorpe /* 1208921eab11SJason Gunthorpe * setup_device() allocates memory and sets up data that requires calling the 1209921eab11SJason Gunthorpe * device ops, this is the only reason these actions are not done during 1210921eab11SJason Gunthorpe * ib_alloc_device. It is undone by ib_dealloc_device(). 1211921eab11SJason Gunthorpe */ 1212548cb4fbSParav Pandit static int setup_device(struct ib_device *device) 1213548cb4fbSParav Pandit { 1214548cb4fbSParav Pandit struct ib_udata uhw = {.outlen = 0, .inlen = 0}; 1215548cb4fbSParav Pandit int ret; 1216548cb4fbSParav Pandit 1217921eab11SJason Gunthorpe setup_dma_device(device); 1218deee3c7eSKamal Heib ib_device_check_mandatory(device); 1219548cb4fbSParav Pandit 12208ceb1357SJason Gunthorpe ret = setup_port_data(device); 1221548cb4fbSParav Pandit if (ret) { 12228ceb1357SJason Gunthorpe dev_warn(&device->dev, "Couldn't create per-port data\n"); 1223548cb4fbSParav Pandit return ret; 1224548cb4fbSParav Pandit } 1225548cb4fbSParav Pandit 1226548cb4fbSParav Pandit memset(&device->attrs, 0, sizeof(device->attrs)); 12273023a1e9SKamal Heib ret = device->ops.query_device(device, &device->attrs, &uhw); 1228548cb4fbSParav Pandit if (ret) { 1229548cb4fbSParav Pandit dev_warn(&device->dev, 1230548cb4fbSParav Pandit "Couldn't query the device attributes\n"); 1231d45f89d5SJason Gunthorpe return ret; 1232548cb4fbSParav Pandit } 1233548cb4fbSParav Pandit 1234548cb4fbSParav Pandit return 0; 1235548cb4fbSParav Pandit } 1236548cb4fbSParav Pandit 1237921eab11SJason Gunthorpe static void disable_device(struct ib_device *device) 1238921eab11SJason Gunthorpe { 12399cd58817SJason Gunthorpe u32 cid; 1240921eab11SJason Gunthorpe 1241921eab11SJason Gunthorpe WARN_ON(!refcount_read(&device->refcount)); 1242921eab11SJason Gunthorpe 1243921eab11SJason Gunthorpe down_write(&devices_rwsem); 1244921eab11SJason Gunthorpe xa_clear_mark(&devices, device->index, DEVICE_REGISTERED); 1245921eab11SJason Gunthorpe up_write(&devices_rwsem); 1246921eab11SJason Gunthorpe 12479cd58817SJason Gunthorpe /* 12489cd58817SJason Gunthorpe * Remove clients in LIFO order, see assign_client_id. This could be 12499cd58817SJason Gunthorpe * more efficient if xarray learns to reverse iterate. Since no new 12509cd58817SJason Gunthorpe * clients can be added to this ib_device past this point we only need 12519cd58817SJason Gunthorpe * the maximum possible client_id value here. 12529cd58817SJason Gunthorpe */ 1253921eab11SJason Gunthorpe down_read(&clients_rwsem); 12549cd58817SJason Gunthorpe cid = highest_client_id; 1255921eab11SJason Gunthorpe up_read(&clients_rwsem); 12569cd58817SJason Gunthorpe while (cid) { 12579cd58817SJason Gunthorpe cid--; 12589cd58817SJason Gunthorpe remove_client_context(device, cid); 12599cd58817SJason Gunthorpe } 1260921eab11SJason Gunthorpe 1261921eab11SJason Gunthorpe /* Pairs with refcount_set in enable_device */ 1262921eab11SJason Gunthorpe ib_device_put(device); 1263921eab11SJason Gunthorpe wait_for_completion(&device->unreg_completion); 1264c2261dd7SJason Gunthorpe 12654e0f7b90SParav Pandit /* 12664e0f7b90SParav Pandit * compat devices must be removed after device refcount drops to zero. 12674e0f7b90SParav Pandit * Otherwise init_net() may add more compatdevs after removing compat 12684e0f7b90SParav Pandit * devices and before device is disabled. 12694e0f7b90SParav Pandit */ 12704e0f7b90SParav Pandit remove_compat_devs(device); 1271921eab11SJason Gunthorpe } 1272921eab11SJason Gunthorpe 1273921eab11SJason Gunthorpe /* 1274921eab11SJason Gunthorpe * An enabled device is visible to all clients and to all the public facing 1275d0899892SJason Gunthorpe * APIs that return a device pointer. This always returns with a new get, even 1276d0899892SJason Gunthorpe * if it fails. 1277921eab11SJason Gunthorpe */ 1278d0899892SJason Gunthorpe static int enable_device_and_get(struct ib_device *device) 1279921eab11SJason Gunthorpe { 1280921eab11SJason Gunthorpe struct ib_client *client; 1281921eab11SJason Gunthorpe unsigned long index; 1282d0899892SJason Gunthorpe int ret = 0; 1283921eab11SJason Gunthorpe 1284d0899892SJason Gunthorpe /* 1285d0899892SJason Gunthorpe * One ref belongs to the xa and the other belongs to this 1286d0899892SJason Gunthorpe * thread. This is needed to guard against parallel unregistration. 1287d0899892SJason Gunthorpe */ 1288d0899892SJason Gunthorpe refcount_set(&device->refcount, 2); 1289921eab11SJason Gunthorpe down_write(&devices_rwsem); 1290921eab11SJason Gunthorpe xa_set_mark(&devices, device->index, DEVICE_REGISTERED); 1291d0899892SJason Gunthorpe 1292d0899892SJason Gunthorpe /* 1293d0899892SJason Gunthorpe * By using downgrade_write() we ensure that no other thread can clear 1294d0899892SJason Gunthorpe * DEVICE_REGISTERED while we are completing the client setup. 1295d0899892SJason Gunthorpe */ 1296d0899892SJason Gunthorpe downgrade_write(&devices_rwsem); 1297921eab11SJason Gunthorpe 1298ca22354bSJason Gunthorpe if (device->ops.enable_driver) { 1299ca22354bSJason Gunthorpe ret = device->ops.enable_driver(device); 1300ca22354bSJason Gunthorpe if (ret) 1301ca22354bSJason Gunthorpe goto out; 1302ca22354bSJason Gunthorpe } 1303ca22354bSJason Gunthorpe 1304921eab11SJason Gunthorpe down_read(&clients_rwsem); 1305921eab11SJason Gunthorpe xa_for_each_marked (&clients, index, client, CLIENT_REGISTERED) { 1306921eab11SJason Gunthorpe ret = add_client_context(device, client); 1307d0899892SJason Gunthorpe if (ret) 1308d0899892SJason Gunthorpe break; 1309d0899892SJason Gunthorpe } 1310921eab11SJason Gunthorpe up_read(&clients_rwsem); 13114e0f7b90SParav Pandit if (!ret) 13124e0f7b90SParav Pandit ret = add_compat_devs(device); 1313ca22354bSJason Gunthorpe out: 1314d0899892SJason Gunthorpe up_read(&devices_rwsem); 1315921eab11SJason Gunthorpe return ret; 1316921eab11SJason Gunthorpe } 1317921eab11SJason Gunthorpe 1318548cb4fbSParav Pandit /** 1319548cb4fbSParav Pandit * ib_register_device - Register an IB device with IB core 1320548cb4fbSParav Pandit * @device:Device to register 1321548cb4fbSParav Pandit * 1322548cb4fbSParav Pandit * Low-level drivers use ib_register_device() to register their 1323548cb4fbSParav Pandit * devices with the IB core. All registered clients will receive a 1324548cb4fbSParav Pandit * callback for each device that is added. @device must be allocated 1325548cb4fbSParav Pandit * with ib_alloc_device(). 1326d0899892SJason Gunthorpe * 1327d0899892SJason Gunthorpe * If the driver uses ops.dealloc_driver and calls any ib_unregister_device() 1328d0899892SJason Gunthorpe * asynchronously then the device pointer may become freed as soon as this 1329d0899892SJason Gunthorpe * function returns. 1330548cb4fbSParav Pandit */ 1331ea4baf7fSParav Pandit int ib_register_device(struct ib_device *device, const char *name) 1332548cb4fbSParav Pandit { 1333548cb4fbSParav Pandit int ret; 13341da177e4SLinus Torvalds 13350df91bb6SJason Gunthorpe ret = assign_name(device, name); 1336e349f858SJason Gunthorpe if (ret) 1337921eab11SJason Gunthorpe return ret; 13381da177e4SLinus Torvalds 1339548cb4fbSParav Pandit ret = setup_device(device); 1340548cb4fbSParav Pandit if (ret) 1341d0899892SJason Gunthorpe return ret; 134203db3a2dSMatan Barak 1343d45f89d5SJason Gunthorpe ret = ib_cache_setup_one(device); 1344d45f89d5SJason Gunthorpe if (ret) { 1345d45f89d5SJason Gunthorpe dev_warn(&device->dev, 1346d45f89d5SJason Gunthorpe "Couldn't set up InfiniBand P_Key/GID cache\n"); 1347d0899892SJason Gunthorpe return ret; 1348d45f89d5SJason Gunthorpe } 1349d45f89d5SJason Gunthorpe 13507527a7b1SParav Pandit ib_device_register_rdmacg(device); 13513e153a93SIra Weiny 1352413d3347SMark Zhang rdma_counter_init(device); 1353413d3347SMark Zhang 1354e7a5b4aaSLeon Romanovsky /* 1355e7a5b4aaSLeon Romanovsky * Ensure that ADD uevent is not fired because it 1356e7a5b4aaSLeon Romanovsky * is too early amd device is not initialized yet. 1357e7a5b4aaSLeon Romanovsky */ 1358e7a5b4aaSLeon Romanovsky dev_set_uevent_suppress(&device->dev, true); 13595f8f5499SParav Pandit ret = device_add(&device->dev); 13605f8f5499SParav Pandit if (ret) 13615f8f5499SParav Pandit goto cg_cleanup; 13625f8f5499SParav Pandit 1363ea4baf7fSParav Pandit ret = ib_device_register_sysfs(device); 13641da177e4SLinus Torvalds if (ret) { 136543c7c851SJason Gunthorpe dev_warn(&device->dev, 136643c7c851SJason Gunthorpe "Couldn't register device with driver model\n"); 13675f8f5499SParav Pandit goto dev_cleanup; 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 1370d0899892SJason Gunthorpe ret = enable_device_and_get(device); 1371e7a5b4aaSLeon Romanovsky dev_set_uevent_suppress(&device->dev, false); 1372e7a5b4aaSLeon Romanovsky /* Mark for userspace that device is ready */ 1373e7a5b4aaSLeon Romanovsky kobject_uevent(&device->dev.kobj, KOBJ_ADD); 1374d0899892SJason Gunthorpe if (ret) { 1375d0899892SJason Gunthorpe void (*dealloc_fn)(struct ib_device *); 1376d0899892SJason Gunthorpe 1377d0899892SJason Gunthorpe /* 1378d0899892SJason Gunthorpe * If we hit this error flow then we don't want to 1379d0899892SJason Gunthorpe * automatically dealloc the device since the caller is 1380d0899892SJason Gunthorpe * expected to call ib_dealloc_device() after 1381d0899892SJason Gunthorpe * ib_register_device() fails. This is tricky due to the 1382d0899892SJason Gunthorpe * possibility for a parallel unregistration along with this 1383d0899892SJason Gunthorpe * error flow. Since we have a refcount here we know any 1384d0899892SJason Gunthorpe * parallel flow is stopped in disable_device and will see the 1385d0899892SJason Gunthorpe * NULL pointers, causing the responsibility to 1386d0899892SJason Gunthorpe * ib_dealloc_device() to revert back to this thread. 1387d0899892SJason Gunthorpe */ 1388d0899892SJason Gunthorpe dealloc_fn = device->ops.dealloc_driver; 1389d0899892SJason Gunthorpe device->ops.dealloc_driver = NULL; 1390d0899892SJason Gunthorpe ib_device_put(device); 1391d0899892SJason Gunthorpe __ib_unregister_device(device); 1392d0899892SJason Gunthorpe device->ops.dealloc_driver = dealloc_fn; 1393d0899892SJason Gunthorpe return ret; 1394d0899892SJason Gunthorpe } 1395d0899892SJason Gunthorpe ib_device_put(device); 13961da177e4SLinus Torvalds 13974be3a4faSParav Pandit return 0; 13984be3a4faSParav Pandit 13995f8f5499SParav Pandit dev_cleanup: 14005f8f5499SParav Pandit device_del(&device->dev); 14012fb4f4eaSParav Pandit cg_cleanup: 1402e7a5b4aaSLeon Romanovsky dev_set_uevent_suppress(&device->dev, false); 14032fb4f4eaSParav Pandit ib_device_unregister_rdmacg(device); 1404d45f89d5SJason Gunthorpe ib_cache_cleanup_one(device); 14051da177e4SLinus Torvalds return ret; 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds EXPORT_SYMBOL(ib_register_device); 14081da177e4SLinus Torvalds 1409d0899892SJason Gunthorpe /* Callers must hold a get on the device. */ 1410d0899892SJason Gunthorpe static void __ib_unregister_device(struct ib_device *ib_dev) 1411d0899892SJason Gunthorpe { 1412d0899892SJason Gunthorpe /* 1413d0899892SJason Gunthorpe * We have a registration lock so that all the calls to unregister are 1414d0899892SJason Gunthorpe * fully fenced, once any unregister returns the device is truely 1415d0899892SJason Gunthorpe * unregistered even if multiple callers are unregistering it at the 1416d0899892SJason Gunthorpe * same time. This also interacts with the registration flow and 1417d0899892SJason Gunthorpe * provides sane semantics if register and unregister are racing. 1418d0899892SJason Gunthorpe */ 1419d0899892SJason Gunthorpe mutex_lock(&ib_dev->unregistration_lock); 1420d0899892SJason Gunthorpe if (!refcount_read(&ib_dev->refcount)) 1421d0899892SJason Gunthorpe goto out; 1422d0899892SJason Gunthorpe 1423d0899892SJason Gunthorpe disable_device(ib_dev); 14243042492bSParav Pandit 14253042492bSParav Pandit /* Expedite removing unregistered pointers from the hash table */ 14263042492bSParav Pandit free_netdevs(ib_dev); 14273042492bSParav Pandit 1428d0899892SJason Gunthorpe ib_device_unregister_sysfs(ib_dev); 1429d0899892SJason Gunthorpe device_del(&ib_dev->dev); 1430d0899892SJason Gunthorpe ib_device_unregister_rdmacg(ib_dev); 1431d0899892SJason Gunthorpe ib_cache_cleanup_one(ib_dev); 1432d0899892SJason Gunthorpe 1433d0899892SJason Gunthorpe /* 1434d0899892SJason Gunthorpe * Drivers using the new flow may not call ib_dealloc_device except 1435d0899892SJason Gunthorpe * in error unwind prior to registration success. 1436d0899892SJason Gunthorpe */ 1437d0899892SJason Gunthorpe if (ib_dev->ops.dealloc_driver) { 1438d0899892SJason Gunthorpe WARN_ON(kref_read(&ib_dev->dev.kobj.kref) <= 1); 1439d0899892SJason Gunthorpe ib_dealloc_device(ib_dev); 1440d0899892SJason Gunthorpe } 1441d0899892SJason Gunthorpe out: 1442d0899892SJason Gunthorpe mutex_unlock(&ib_dev->unregistration_lock); 1443d0899892SJason Gunthorpe } 1444d0899892SJason Gunthorpe 14451da177e4SLinus Torvalds /** 14461da177e4SLinus Torvalds * ib_unregister_device - Unregister an IB device 1447d0899892SJason Gunthorpe * @device: The device to unregister 14481da177e4SLinus Torvalds * 14491da177e4SLinus Torvalds * Unregister an IB device. All clients will receive a remove callback. 1450d0899892SJason Gunthorpe * 1451d0899892SJason Gunthorpe * Callers should call this routine only once, and protect against races with 1452d0899892SJason Gunthorpe * registration. Typically it should only be called as part of a remove 1453d0899892SJason Gunthorpe * callback in an implementation of driver core's struct device_driver and 1454d0899892SJason Gunthorpe * related. 1455d0899892SJason Gunthorpe * 1456d0899892SJason Gunthorpe * If ops.dealloc_driver is used then ib_dev will be freed upon return from 1457d0899892SJason Gunthorpe * this function. 14581da177e4SLinus Torvalds */ 1459d0899892SJason Gunthorpe void ib_unregister_device(struct ib_device *ib_dev) 14601da177e4SLinus Torvalds { 1461d0899892SJason Gunthorpe get_device(&ib_dev->dev); 1462d0899892SJason Gunthorpe __ib_unregister_device(ib_dev); 1463d0899892SJason Gunthorpe put_device(&ib_dev->dev); 14641da177e4SLinus Torvalds } 14651da177e4SLinus Torvalds EXPORT_SYMBOL(ib_unregister_device); 14661da177e4SLinus Torvalds 1467d0899892SJason Gunthorpe /** 1468d0899892SJason Gunthorpe * ib_unregister_device_and_put - Unregister a device while holding a 'get' 1469d0899892SJason Gunthorpe * device: The device to unregister 1470d0899892SJason Gunthorpe * 1471d0899892SJason Gunthorpe * This is the same as ib_unregister_device(), except it includes an internal 1472d0899892SJason Gunthorpe * ib_device_put() that should match a 'get' obtained by the caller. 1473d0899892SJason Gunthorpe * 1474d0899892SJason Gunthorpe * It is safe to call this routine concurrently from multiple threads while 1475d0899892SJason Gunthorpe * holding the 'get'. When the function returns the device is fully 1476d0899892SJason Gunthorpe * unregistered. 1477d0899892SJason Gunthorpe * 1478d0899892SJason Gunthorpe * Drivers using this flow MUST use the driver_unregister callback to clean up 1479d0899892SJason Gunthorpe * their resources associated with the device and dealloc it. 1480d0899892SJason Gunthorpe */ 1481d0899892SJason Gunthorpe void ib_unregister_device_and_put(struct ib_device *ib_dev) 1482d0899892SJason Gunthorpe { 1483d0899892SJason Gunthorpe WARN_ON(!ib_dev->ops.dealloc_driver); 1484d0899892SJason Gunthorpe get_device(&ib_dev->dev); 1485d0899892SJason Gunthorpe ib_device_put(ib_dev); 1486d0899892SJason Gunthorpe __ib_unregister_device(ib_dev); 1487d0899892SJason Gunthorpe put_device(&ib_dev->dev); 1488d0899892SJason Gunthorpe } 1489d0899892SJason Gunthorpe EXPORT_SYMBOL(ib_unregister_device_and_put); 1490d0899892SJason Gunthorpe 1491d0899892SJason Gunthorpe /** 1492d0899892SJason Gunthorpe * ib_unregister_driver - Unregister all IB devices for a driver 1493d0899892SJason Gunthorpe * @driver_id: The driver to unregister 1494d0899892SJason Gunthorpe * 1495d0899892SJason Gunthorpe * This implements a fence for device unregistration. It only returns once all 1496d0899892SJason Gunthorpe * devices associated with the driver_id have fully completed their 1497d0899892SJason Gunthorpe * unregistration and returned from ib_unregister_device*(). 1498d0899892SJason Gunthorpe * 1499d0899892SJason Gunthorpe * If device's are not yet unregistered it goes ahead and starts unregistering 1500d0899892SJason Gunthorpe * them. 1501d0899892SJason Gunthorpe * 1502d0899892SJason Gunthorpe * This does not block creation of new devices with the given driver_id, that 1503d0899892SJason Gunthorpe * is the responsibility of the caller. 1504d0899892SJason Gunthorpe */ 1505d0899892SJason Gunthorpe void ib_unregister_driver(enum rdma_driver_id driver_id) 1506d0899892SJason Gunthorpe { 1507d0899892SJason Gunthorpe struct ib_device *ib_dev; 1508d0899892SJason Gunthorpe unsigned long index; 1509d0899892SJason Gunthorpe 1510d0899892SJason Gunthorpe down_read(&devices_rwsem); 1511d0899892SJason Gunthorpe xa_for_each (&devices, index, ib_dev) { 1512b9560a41SJason Gunthorpe if (ib_dev->ops.driver_id != driver_id) 1513d0899892SJason Gunthorpe continue; 1514d0899892SJason Gunthorpe 1515d0899892SJason Gunthorpe get_device(&ib_dev->dev); 1516d0899892SJason Gunthorpe up_read(&devices_rwsem); 1517d0899892SJason Gunthorpe 1518d0899892SJason Gunthorpe WARN_ON(!ib_dev->ops.dealloc_driver); 1519d0899892SJason Gunthorpe __ib_unregister_device(ib_dev); 1520d0899892SJason Gunthorpe 1521d0899892SJason Gunthorpe put_device(&ib_dev->dev); 1522d0899892SJason Gunthorpe down_read(&devices_rwsem); 1523d0899892SJason Gunthorpe } 1524d0899892SJason Gunthorpe up_read(&devices_rwsem); 1525d0899892SJason Gunthorpe } 1526d0899892SJason Gunthorpe EXPORT_SYMBOL(ib_unregister_driver); 1527d0899892SJason Gunthorpe 1528d0899892SJason Gunthorpe static void ib_unregister_work(struct work_struct *work) 1529d0899892SJason Gunthorpe { 1530d0899892SJason Gunthorpe struct ib_device *ib_dev = 1531d0899892SJason Gunthorpe container_of(work, struct ib_device, unregistration_work); 1532d0899892SJason Gunthorpe 1533d0899892SJason Gunthorpe __ib_unregister_device(ib_dev); 1534d0899892SJason Gunthorpe put_device(&ib_dev->dev); 1535d0899892SJason Gunthorpe } 1536d0899892SJason Gunthorpe 1537d0899892SJason Gunthorpe /** 1538d0899892SJason Gunthorpe * ib_unregister_device_queued - Unregister a device using a work queue 1539d0899892SJason Gunthorpe * device: The device to unregister 1540d0899892SJason Gunthorpe * 1541d0899892SJason Gunthorpe * This schedules an asynchronous unregistration using a WQ for the device. A 1542d0899892SJason Gunthorpe * driver should use this to avoid holding locks while doing unregistration, 1543d0899892SJason Gunthorpe * such as holding the RTNL lock. 1544d0899892SJason Gunthorpe * 1545d0899892SJason Gunthorpe * Drivers using this API must use ib_unregister_driver before module unload 1546d0899892SJason Gunthorpe * to ensure that all scheduled unregistrations have completed. 1547d0899892SJason Gunthorpe */ 1548d0899892SJason Gunthorpe void ib_unregister_device_queued(struct ib_device *ib_dev) 1549d0899892SJason Gunthorpe { 1550d0899892SJason Gunthorpe WARN_ON(!refcount_read(&ib_dev->refcount)); 1551d0899892SJason Gunthorpe WARN_ON(!ib_dev->ops.dealloc_driver); 1552d0899892SJason Gunthorpe get_device(&ib_dev->dev); 1553d0899892SJason Gunthorpe if (!queue_work(system_unbound_wq, &ib_dev->unregistration_work)) 1554d0899892SJason Gunthorpe put_device(&ib_dev->dev); 1555d0899892SJason Gunthorpe } 1556d0899892SJason Gunthorpe EXPORT_SYMBOL(ib_unregister_device_queued); 1557d0899892SJason Gunthorpe 1558decbc7a6SParav Pandit /* 1559decbc7a6SParav Pandit * The caller must pass in a device that has the kref held and the refcount 1560decbc7a6SParav Pandit * released. If the device is in cur_net and still registered then it is moved 1561decbc7a6SParav Pandit * into net. 1562decbc7a6SParav Pandit */ 1563decbc7a6SParav Pandit static int rdma_dev_change_netns(struct ib_device *device, struct net *cur_net, 1564decbc7a6SParav Pandit struct net *net) 1565decbc7a6SParav Pandit { 1566decbc7a6SParav Pandit int ret2 = -EINVAL; 1567decbc7a6SParav Pandit int ret; 1568decbc7a6SParav Pandit 1569decbc7a6SParav Pandit mutex_lock(&device->unregistration_lock); 1570decbc7a6SParav Pandit 1571decbc7a6SParav Pandit /* 15722e5b8a01SParav Pandit * If a device not under ib_device_get() or if the unregistration_lock 15732e5b8a01SParav Pandit * is not held, the namespace can be changed, or it can be unregistered. 15742e5b8a01SParav Pandit * Check again under the lock. 1575decbc7a6SParav Pandit */ 1576decbc7a6SParav Pandit if (refcount_read(&device->refcount) == 0 || 1577decbc7a6SParav Pandit !net_eq(cur_net, read_pnet(&device->coredev.rdma_net))) { 1578decbc7a6SParav Pandit ret = -ENODEV; 1579decbc7a6SParav Pandit goto out; 1580decbc7a6SParav Pandit } 1581decbc7a6SParav Pandit 1582decbc7a6SParav Pandit kobject_uevent(&device->dev.kobj, KOBJ_REMOVE); 1583decbc7a6SParav Pandit disable_device(device); 1584decbc7a6SParav Pandit 1585decbc7a6SParav Pandit /* 1586decbc7a6SParav Pandit * At this point no one can be using the device, so it is safe to 1587decbc7a6SParav Pandit * change the namespace. 1588decbc7a6SParav Pandit */ 1589decbc7a6SParav Pandit write_pnet(&device->coredev.rdma_net, net); 1590decbc7a6SParav Pandit 15912e5b8a01SParav Pandit down_read(&devices_rwsem); 1592decbc7a6SParav Pandit /* 1593decbc7a6SParav Pandit * Currently rdma devices are system wide unique. So the device name 1594decbc7a6SParav Pandit * is guaranteed free in the new namespace. Publish the new namespace 1595decbc7a6SParav Pandit * at the sysfs level. 1596decbc7a6SParav Pandit */ 1597decbc7a6SParav Pandit ret = device_rename(&device->dev, dev_name(&device->dev)); 1598decbc7a6SParav Pandit up_read(&devices_rwsem); 1599decbc7a6SParav Pandit if (ret) { 1600decbc7a6SParav Pandit dev_warn(&device->dev, 1601decbc7a6SParav Pandit "%s: Couldn't rename device after namespace change\n", 1602decbc7a6SParav Pandit __func__); 1603decbc7a6SParav Pandit /* Try and put things back and re-enable the device */ 1604decbc7a6SParav Pandit write_pnet(&device->coredev.rdma_net, cur_net); 1605decbc7a6SParav Pandit } 1606decbc7a6SParav Pandit 1607decbc7a6SParav Pandit ret2 = enable_device_and_get(device); 16082e5b8a01SParav Pandit if (ret2) { 1609decbc7a6SParav Pandit /* 1610decbc7a6SParav Pandit * This shouldn't really happen, but if it does, let the user 1611decbc7a6SParav Pandit * retry at later point. So don't disable the device. 1612decbc7a6SParav Pandit */ 1613decbc7a6SParav Pandit dev_warn(&device->dev, 1614decbc7a6SParav Pandit "%s: Couldn't re-enable device after namespace change\n", 1615decbc7a6SParav Pandit __func__); 16162e5b8a01SParav Pandit } 1617decbc7a6SParav Pandit kobject_uevent(&device->dev.kobj, KOBJ_ADD); 16182e5b8a01SParav Pandit 1619decbc7a6SParav Pandit ib_device_put(device); 1620decbc7a6SParav Pandit out: 1621decbc7a6SParav Pandit mutex_unlock(&device->unregistration_lock); 1622decbc7a6SParav Pandit if (ret) 1623decbc7a6SParav Pandit return ret; 1624decbc7a6SParav Pandit return ret2; 1625decbc7a6SParav Pandit } 1626decbc7a6SParav Pandit 16272e5b8a01SParav Pandit int ib_device_set_netns_put(struct sk_buff *skb, 16282e5b8a01SParav Pandit struct ib_device *dev, u32 ns_fd) 16292e5b8a01SParav Pandit { 16302e5b8a01SParav Pandit struct net *net; 16312e5b8a01SParav Pandit int ret; 16322e5b8a01SParav Pandit 16332e5b8a01SParav Pandit net = get_net_ns_by_fd(ns_fd); 16342e5b8a01SParav Pandit if (IS_ERR(net)) { 16352e5b8a01SParav Pandit ret = PTR_ERR(net); 16362e5b8a01SParav Pandit goto net_err; 16372e5b8a01SParav Pandit } 16382e5b8a01SParav Pandit 16392e5b8a01SParav Pandit if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) { 16402e5b8a01SParav Pandit ret = -EPERM; 16412e5b8a01SParav Pandit goto ns_err; 16422e5b8a01SParav Pandit } 16432e5b8a01SParav Pandit 16442e5b8a01SParav Pandit /* 16452e5b8a01SParav Pandit * Currently supported only for those providers which support 16462e5b8a01SParav Pandit * disassociation and don't do port specific sysfs init. Once a 16472e5b8a01SParav Pandit * port_cleanup infrastructure is implemented, this limitation will be 16482e5b8a01SParav Pandit * removed. 16492e5b8a01SParav Pandit */ 16502e5b8a01SParav Pandit if (!dev->ops.disassociate_ucontext || dev->ops.init_port || 16512e5b8a01SParav Pandit ib_devices_shared_netns) { 16522e5b8a01SParav Pandit ret = -EOPNOTSUPP; 16532e5b8a01SParav Pandit goto ns_err; 16542e5b8a01SParav Pandit } 16552e5b8a01SParav Pandit 16562e5b8a01SParav Pandit get_device(&dev->dev); 16572e5b8a01SParav Pandit ib_device_put(dev); 16582e5b8a01SParav Pandit ret = rdma_dev_change_netns(dev, current->nsproxy->net_ns, net); 16592e5b8a01SParav Pandit put_device(&dev->dev); 16602e5b8a01SParav Pandit 16612e5b8a01SParav Pandit put_net(net); 16622e5b8a01SParav Pandit return ret; 16632e5b8a01SParav Pandit 16642e5b8a01SParav Pandit ns_err: 16652e5b8a01SParav Pandit put_net(net); 16662e5b8a01SParav Pandit net_err: 16672e5b8a01SParav Pandit ib_device_put(dev); 16682e5b8a01SParav Pandit return ret; 16692e5b8a01SParav Pandit } 16702e5b8a01SParav Pandit 16714e0f7b90SParav Pandit static struct pernet_operations rdma_dev_net_ops = { 16724e0f7b90SParav Pandit .init = rdma_dev_init_net, 16734e0f7b90SParav Pandit .exit = rdma_dev_exit_net, 16744e0f7b90SParav Pandit .id = &rdma_dev_net_id, 16754e0f7b90SParav Pandit .size = sizeof(struct rdma_dev_net), 16764e0f7b90SParav Pandit }; 16774e0f7b90SParav Pandit 1678e59178d8SJason Gunthorpe static int assign_client_id(struct ib_client *client) 1679e59178d8SJason Gunthorpe { 1680e59178d8SJason Gunthorpe int ret; 1681e59178d8SJason Gunthorpe 1682921eab11SJason Gunthorpe down_write(&clients_rwsem); 1683e59178d8SJason Gunthorpe /* 1684e59178d8SJason Gunthorpe * The add/remove callbacks must be called in FIFO/LIFO order. To 1685e59178d8SJason Gunthorpe * achieve this we assign client_ids so they are sorted in 16869cd58817SJason Gunthorpe * registration order. 1687e59178d8SJason Gunthorpe */ 16889cd58817SJason Gunthorpe client->client_id = highest_client_id; 1689ea295481SLinus Torvalds ret = xa_insert(&clients, client->client_id, client, GFP_KERNEL); 1690e59178d8SJason Gunthorpe if (ret) 1691e59178d8SJason Gunthorpe goto out; 1692e59178d8SJason Gunthorpe 16939cd58817SJason Gunthorpe highest_client_id++; 1694921eab11SJason Gunthorpe xa_set_mark(&clients, client->client_id, CLIENT_REGISTERED); 1695921eab11SJason Gunthorpe 1696e59178d8SJason Gunthorpe out: 1697921eab11SJason Gunthorpe up_write(&clients_rwsem); 1698e59178d8SJason Gunthorpe return ret; 1699e59178d8SJason Gunthorpe } 1700e59178d8SJason Gunthorpe 17019cd58817SJason Gunthorpe static void remove_client_id(struct ib_client *client) 17029cd58817SJason Gunthorpe { 17039cd58817SJason Gunthorpe down_write(&clients_rwsem); 17049cd58817SJason Gunthorpe xa_erase(&clients, client->client_id); 17059cd58817SJason Gunthorpe for (; highest_client_id; highest_client_id--) 17069cd58817SJason Gunthorpe if (xa_load(&clients, highest_client_id - 1)) 17079cd58817SJason Gunthorpe break; 17089cd58817SJason Gunthorpe up_write(&clients_rwsem); 17099cd58817SJason Gunthorpe } 17109cd58817SJason Gunthorpe 17111da177e4SLinus Torvalds /** 17121da177e4SLinus Torvalds * ib_register_client - Register an IB client 17131da177e4SLinus Torvalds * @client:Client to register 17141da177e4SLinus Torvalds * 17151da177e4SLinus Torvalds * Upper level users of the IB drivers can use ib_register_client() to 17161da177e4SLinus Torvalds * register callbacks for IB device addition and removal. When an IB 17171da177e4SLinus Torvalds * device is added, each registered client's add method will be called 17181da177e4SLinus Torvalds * (in the order the clients were registered), and when a device is 17191da177e4SLinus Torvalds * removed, each client's remove method will be called (in the reverse 17201da177e4SLinus Torvalds * order that clients were registered). In addition, when 17211da177e4SLinus Torvalds * ib_register_client() is called, the client will receive an add 17221da177e4SLinus Torvalds * callback for all devices already registered. 17231da177e4SLinus Torvalds */ 17241da177e4SLinus Torvalds int ib_register_client(struct ib_client *client) 17251da177e4SLinus Torvalds { 17261da177e4SLinus Torvalds struct ib_device *device; 17270df91bb6SJason Gunthorpe unsigned long index; 1728e59178d8SJason Gunthorpe int ret; 17291da177e4SLinus Torvalds 1730621e55ffSJason Gunthorpe refcount_set(&client->uses, 1); 1731621e55ffSJason Gunthorpe init_completion(&client->uses_zero); 1732e59178d8SJason Gunthorpe ret = assign_client_id(client); 1733921eab11SJason Gunthorpe if (ret) 1734921eab11SJason Gunthorpe return ret; 1735921eab11SJason Gunthorpe 1736921eab11SJason Gunthorpe down_read(&devices_rwsem); 1737921eab11SJason Gunthorpe xa_for_each_marked (&devices, index, device, DEVICE_REGISTERED) { 1738921eab11SJason Gunthorpe ret = add_client_context(device, client); 1739e59178d8SJason Gunthorpe if (ret) { 1740921eab11SJason Gunthorpe up_read(&devices_rwsem); 1741921eab11SJason Gunthorpe ib_unregister_client(client); 1742e59178d8SJason Gunthorpe return ret; 1743e59178d8SJason Gunthorpe } 1744921eab11SJason Gunthorpe } 1745921eab11SJason Gunthorpe up_read(&devices_rwsem); 17461da177e4SLinus Torvalds return 0; 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds EXPORT_SYMBOL(ib_register_client); 17491da177e4SLinus Torvalds 17501da177e4SLinus Torvalds /** 17511da177e4SLinus Torvalds * ib_unregister_client - Unregister an IB client 17521da177e4SLinus Torvalds * @client:Client to unregister 17531da177e4SLinus Torvalds * 17541da177e4SLinus Torvalds * Upper level users use ib_unregister_client() to remove their client 17551da177e4SLinus Torvalds * registration. When ib_unregister_client() is called, the client 17561da177e4SLinus Torvalds * will receive a remove callback for each IB device still registered. 1757921eab11SJason Gunthorpe * 1758921eab11SJason Gunthorpe * This is a full fence, once it returns no client callbacks will be called, 1759921eab11SJason Gunthorpe * or are running in another thread. 17601da177e4SLinus Torvalds */ 17611da177e4SLinus Torvalds void ib_unregister_client(struct ib_client *client) 17621da177e4SLinus Torvalds { 17631da177e4SLinus Torvalds struct ib_device *device; 17640df91bb6SJason Gunthorpe unsigned long index; 17651da177e4SLinus Torvalds 1766921eab11SJason Gunthorpe down_write(&clients_rwsem); 1767621e55ffSJason Gunthorpe ib_client_put(client); 1768e59178d8SJason Gunthorpe xa_clear_mark(&clients, client->client_id, CLIENT_REGISTERED); 1769921eab11SJason Gunthorpe up_write(&clients_rwsem); 17705aa44bb9SHaggai Eran 1771621e55ffSJason Gunthorpe /* We do not want to have locks while calling client->remove() */ 1772621e55ffSJason Gunthorpe rcu_read_lock(); 1773621e55ffSJason Gunthorpe xa_for_each (&devices, index, device) { 1774621e55ffSJason Gunthorpe if (!ib_device_try_get(device)) 1775621e55ffSJason Gunthorpe continue; 1776621e55ffSJason Gunthorpe rcu_read_unlock(); 1777621e55ffSJason Gunthorpe 17781da177e4SLinus Torvalds remove_client_context(device, client->client_id); 1779621e55ffSJason Gunthorpe 1780621e55ffSJason Gunthorpe ib_device_put(device); 1781621e55ffSJason Gunthorpe rcu_read_lock(); 1782621e55ffSJason Gunthorpe } 1783621e55ffSJason Gunthorpe rcu_read_unlock(); 1784621e55ffSJason Gunthorpe 1785621e55ffSJason Gunthorpe /* 1786621e55ffSJason Gunthorpe * remove_client_context() is not a fence, it can return even though a 1787621e55ffSJason Gunthorpe * removal is ongoing. Wait until all removals are completed. 1788621e55ffSJason Gunthorpe */ 1789621e55ffSJason Gunthorpe wait_for_completion(&client->uses_zero); 17909cd58817SJason Gunthorpe remove_client_id(client); 17911da177e4SLinus Torvalds } 17921da177e4SLinus Torvalds EXPORT_SYMBOL(ib_unregister_client); 17931da177e4SLinus Torvalds 17940e2d00ebSJason Gunthorpe static int __ib_get_global_client_nl_info(const char *client_name, 17950e2d00ebSJason Gunthorpe struct ib_client_nl_info *res) 17960e2d00ebSJason Gunthorpe { 17970e2d00ebSJason Gunthorpe struct ib_client *client; 17980e2d00ebSJason Gunthorpe unsigned long index; 17990e2d00ebSJason Gunthorpe int ret = -ENOENT; 18000e2d00ebSJason Gunthorpe 18010e2d00ebSJason Gunthorpe down_read(&clients_rwsem); 18020e2d00ebSJason Gunthorpe xa_for_each_marked (&clients, index, client, CLIENT_REGISTERED) { 18030e2d00ebSJason Gunthorpe if (strcmp(client->name, client_name) != 0) 18040e2d00ebSJason Gunthorpe continue; 18050e2d00ebSJason Gunthorpe if (!client->get_global_nl_info) { 18060e2d00ebSJason Gunthorpe ret = -EOPNOTSUPP; 18070e2d00ebSJason Gunthorpe break; 18080e2d00ebSJason Gunthorpe } 18090e2d00ebSJason Gunthorpe ret = client->get_global_nl_info(res); 18100e2d00ebSJason Gunthorpe if (WARN_ON(ret == -ENOENT)) 18110e2d00ebSJason Gunthorpe ret = -EINVAL; 18120e2d00ebSJason Gunthorpe if (!ret && res->cdev) 18130e2d00ebSJason Gunthorpe get_device(res->cdev); 18140e2d00ebSJason Gunthorpe break; 18150e2d00ebSJason Gunthorpe } 18160e2d00ebSJason Gunthorpe up_read(&clients_rwsem); 18170e2d00ebSJason Gunthorpe return ret; 18180e2d00ebSJason Gunthorpe } 18190e2d00ebSJason Gunthorpe 18200e2d00ebSJason Gunthorpe static int __ib_get_client_nl_info(struct ib_device *ibdev, 18210e2d00ebSJason Gunthorpe const char *client_name, 18220e2d00ebSJason Gunthorpe struct ib_client_nl_info *res) 18230e2d00ebSJason Gunthorpe { 18240e2d00ebSJason Gunthorpe unsigned long index; 18250e2d00ebSJason Gunthorpe void *client_data; 18260e2d00ebSJason Gunthorpe int ret = -ENOENT; 18270e2d00ebSJason Gunthorpe 18280e2d00ebSJason Gunthorpe down_read(&ibdev->client_data_rwsem); 18290e2d00ebSJason Gunthorpe xan_for_each_marked (&ibdev->client_data, index, client_data, 18300e2d00ebSJason Gunthorpe CLIENT_DATA_REGISTERED) { 18310e2d00ebSJason Gunthorpe struct ib_client *client = xa_load(&clients, index); 18320e2d00ebSJason Gunthorpe 18330e2d00ebSJason Gunthorpe if (!client || strcmp(client->name, client_name) != 0) 18340e2d00ebSJason Gunthorpe continue; 18350e2d00ebSJason Gunthorpe if (!client->get_nl_info) { 18360e2d00ebSJason Gunthorpe ret = -EOPNOTSUPP; 18370e2d00ebSJason Gunthorpe break; 18380e2d00ebSJason Gunthorpe } 18390e2d00ebSJason Gunthorpe ret = client->get_nl_info(ibdev, client_data, res); 18400e2d00ebSJason Gunthorpe if (WARN_ON(ret == -ENOENT)) 18410e2d00ebSJason Gunthorpe ret = -EINVAL; 18420e2d00ebSJason Gunthorpe 18430e2d00ebSJason Gunthorpe /* 18440e2d00ebSJason Gunthorpe * The cdev is guaranteed valid as long as we are inside the 18450e2d00ebSJason Gunthorpe * client_data_rwsem as remove_one can't be called. Keep it 18460e2d00ebSJason Gunthorpe * valid for the caller. 18470e2d00ebSJason Gunthorpe */ 18480e2d00ebSJason Gunthorpe if (!ret && res->cdev) 18490e2d00ebSJason Gunthorpe get_device(res->cdev); 18500e2d00ebSJason Gunthorpe break; 18510e2d00ebSJason Gunthorpe } 18520e2d00ebSJason Gunthorpe up_read(&ibdev->client_data_rwsem); 18530e2d00ebSJason Gunthorpe 18540e2d00ebSJason Gunthorpe return ret; 18550e2d00ebSJason Gunthorpe } 18560e2d00ebSJason Gunthorpe 18570e2d00ebSJason Gunthorpe /** 18580e2d00ebSJason Gunthorpe * ib_get_client_nl_info - Fetch the nl_info from a client 18590e2d00ebSJason Gunthorpe * @device - IB device 18600e2d00ebSJason Gunthorpe * @client_name - Name of the client 18610e2d00ebSJason Gunthorpe * @res - Result of the query 18620e2d00ebSJason Gunthorpe */ 18630e2d00ebSJason Gunthorpe int ib_get_client_nl_info(struct ib_device *ibdev, const char *client_name, 18640e2d00ebSJason Gunthorpe struct ib_client_nl_info *res) 18650e2d00ebSJason Gunthorpe { 18660e2d00ebSJason Gunthorpe int ret; 18670e2d00ebSJason Gunthorpe 18680e2d00ebSJason Gunthorpe if (ibdev) 18690e2d00ebSJason Gunthorpe ret = __ib_get_client_nl_info(ibdev, client_name, res); 18700e2d00ebSJason Gunthorpe else 18710e2d00ebSJason Gunthorpe ret = __ib_get_global_client_nl_info(client_name, res); 18720e2d00ebSJason Gunthorpe #ifdef CONFIG_MODULES 18730e2d00ebSJason Gunthorpe if (ret == -ENOENT) { 18740e2d00ebSJason Gunthorpe request_module("rdma-client-%s", client_name); 18750e2d00ebSJason Gunthorpe if (ibdev) 18760e2d00ebSJason Gunthorpe ret = __ib_get_client_nl_info(ibdev, client_name, res); 18770e2d00ebSJason Gunthorpe else 18780e2d00ebSJason Gunthorpe ret = __ib_get_global_client_nl_info(client_name, res); 18790e2d00ebSJason Gunthorpe } 18800e2d00ebSJason Gunthorpe #endif 18810e2d00ebSJason Gunthorpe if (ret) { 18820e2d00ebSJason Gunthorpe if (ret == -ENOENT) 18830e2d00ebSJason Gunthorpe return -EOPNOTSUPP; 18840e2d00ebSJason Gunthorpe return ret; 18850e2d00ebSJason Gunthorpe } 18860e2d00ebSJason Gunthorpe 18870e2d00ebSJason Gunthorpe if (WARN_ON(!res->cdev)) 18880e2d00ebSJason Gunthorpe return -EINVAL; 18890e2d00ebSJason Gunthorpe return 0; 18900e2d00ebSJason Gunthorpe } 18910e2d00ebSJason Gunthorpe 18921da177e4SLinus Torvalds /** 18939cd330d3SKrishna Kumar * ib_set_client_data - Set IB client context 18941da177e4SLinus Torvalds * @device:Device to set context for 18951da177e4SLinus Torvalds * @client:Client to set context for 18961da177e4SLinus Torvalds * @data:Context to set 18971da177e4SLinus Torvalds * 18980df91bb6SJason Gunthorpe * ib_set_client_data() sets client context data that can be retrieved with 18990df91bb6SJason Gunthorpe * ib_get_client_data(). This can only be called while the client is 19000df91bb6SJason Gunthorpe * registered to the device, once the ib_client remove() callback returns this 19010df91bb6SJason Gunthorpe * cannot be called. 19021da177e4SLinus Torvalds */ 19031da177e4SLinus Torvalds void ib_set_client_data(struct ib_device *device, struct ib_client *client, 19041da177e4SLinus Torvalds void *data) 19051da177e4SLinus Torvalds { 19060df91bb6SJason Gunthorpe void *rc; 19071da177e4SLinus Torvalds 19080df91bb6SJason Gunthorpe if (WARN_ON(IS_ERR(data))) 19090df91bb6SJason Gunthorpe data = NULL; 19101da177e4SLinus Torvalds 19110df91bb6SJason Gunthorpe rc = xa_store(&device->client_data, client->client_id, data, 19120df91bb6SJason Gunthorpe GFP_KERNEL); 19130df91bb6SJason Gunthorpe WARN_ON(xa_is_err(rc)); 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds EXPORT_SYMBOL(ib_set_client_data); 19161da177e4SLinus Torvalds 19171da177e4SLinus Torvalds /** 19181da177e4SLinus Torvalds * ib_register_event_handler - Register an IB event handler 19191da177e4SLinus Torvalds * @event_handler:Handler to register 19201da177e4SLinus Torvalds * 19211da177e4SLinus Torvalds * ib_register_event_handler() registers an event handler that will be 19221da177e4SLinus Torvalds * called back when asynchronous IB events occur (as defined in 19231da177e4SLinus Torvalds * chapter 11 of the InfiniBand Architecture Specification). This 19241da177e4SLinus Torvalds * callback may occur in interrupt context. 19251da177e4SLinus Torvalds */ 1926dcc9881eSLeon Romanovsky void ib_register_event_handler(struct ib_event_handler *event_handler) 19271da177e4SLinus Torvalds { 19281da177e4SLinus Torvalds unsigned long flags; 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds spin_lock_irqsave(&event_handler->device->event_handler_lock, flags); 19311da177e4SLinus Torvalds list_add_tail(&event_handler->list, 19321da177e4SLinus Torvalds &event_handler->device->event_handler_list); 19331da177e4SLinus Torvalds spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags); 19341da177e4SLinus Torvalds } 19351da177e4SLinus Torvalds EXPORT_SYMBOL(ib_register_event_handler); 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds /** 19381da177e4SLinus Torvalds * ib_unregister_event_handler - Unregister an event handler 19391da177e4SLinus Torvalds * @event_handler:Handler to unregister 19401da177e4SLinus Torvalds * 19411da177e4SLinus Torvalds * Unregister an event handler registered with 19421da177e4SLinus Torvalds * ib_register_event_handler(). 19431da177e4SLinus Torvalds */ 1944dcc9881eSLeon Romanovsky void ib_unregister_event_handler(struct ib_event_handler *event_handler) 19451da177e4SLinus Torvalds { 19461da177e4SLinus Torvalds unsigned long flags; 19471da177e4SLinus Torvalds 19481da177e4SLinus Torvalds spin_lock_irqsave(&event_handler->device->event_handler_lock, flags); 19491da177e4SLinus Torvalds list_del(&event_handler->list); 19501da177e4SLinus Torvalds spin_unlock_irqrestore(&event_handler->device->event_handler_lock, flags); 19511da177e4SLinus Torvalds } 19521da177e4SLinus Torvalds EXPORT_SYMBOL(ib_unregister_event_handler); 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds /** 19551da177e4SLinus Torvalds * ib_dispatch_event - Dispatch an asynchronous event 19561da177e4SLinus Torvalds * @event:Event to dispatch 19571da177e4SLinus Torvalds * 19581da177e4SLinus Torvalds * Low-level drivers must call ib_dispatch_event() to dispatch the 19591da177e4SLinus Torvalds * event to all registered event handlers when an asynchronous event 19601da177e4SLinus Torvalds * occurs. 19611da177e4SLinus Torvalds */ 19621da177e4SLinus Torvalds void ib_dispatch_event(struct ib_event *event) 19631da177e4SLinus Torvalds { 19641da177e4SLinus Torvalds unsigned long flags; 19651da177e4SLinus Torvalds struct ib_event_handler *handler; 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds spin_lock_irqsave(&event->device->event_handler_lock, flags); 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds list_for_each_entry(handler, &event->device->event_handler_list, list) 19701da177e4SLinus Torvalds handler->handler(handler, event); 19711da177e4SLinus Torvalds 19721da177e4SLinus Torvalds spin_unlock_irqrestore(&event->device->event_handler_lock, flags); 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds EXPORT_SYMBOL(ib_dispatch_event); 19751da177e4SLinus Torvalds 19764929116bSKamal Heib static int iw_query_port(struct ib_device *device, 19774929116bSKamal Heib u8 port_num, 19784929116bSKamal Heib struct ib_port_attr *port_attr) 19794929116bSKamal Heib { 19804929116bSKamal Heib struct in_device *inetdev; 19814929116bSKamal Heib struct net_device *netdev; 19824929116bSKamal Heib int err; 19834929116bSKamal Heib 19844929116bSKamal Heib memset(port_attr, 0, sizeof(*port_attr)); 19854929116bSKamal Heib 19864929116bSKamal Heib netdev = ib_device_get_netdev(device, port_num); 19874929116bSKamal Heib if (!netdev) 19884929116bSKamal Heib return -ENODEV; 19894929116bSKamal Heib 19904929116bSKamal Heib dev_put(netdev); 19914929116bSKamal Heib 19924929116bSKamal Heib port_attr->max_mtu = IB_MTU_4096; 19934929116bSKamal Heib port_attr->active_mtu = ib_mtu_int_to_enum(netdev->mtu); 19944929116bSKamal Heib 19954929116bSKamal Heib if (!netif_carrier_ok(netdev)) { 19964929116bSKamal Heib port_attr->state = IB_PORT_DOWN; 19974929116bSKamal Heib port_attr->phys_state = IB_PORT_PHYS_STATE_DISABLED; 19984929116bSKamal Heib } else { 19994929116bSKamal Heib inetdev = in_dev_get(netdev); 20004929116bSKamal Heib 20014929116bSKamal Heib if (inetdev && inetdev->ifa_list) { 20024929116bSKamal Heib port_attr->state = IB_PORT_ACTIVE; 20034929116bSKamal Heib port_attr->phys_state = IB_PORT_PHYS_STATE_LINK_UP; 20044929116bSKamal Heib in_dev_put(inetdev); 20054929116bSKamal Heib } else { 20064929116bSKamal Heib port_attr->state = IB_PORT_INIT; 20074929116bSKamal Heib port_attr->phys_state = 20084929116bSKamal Heib IB_PORT_PHYS_STATE_PORT_CONFIGURATION_TRAINING; 20094929116bSKamal Heib } 20104929116bSKamal Heib } 20114929116bSKamal Heib 20124929116bSKamal Heib err = device->ops.query_port(device, port_num, port_attr); 20134929116bSKamal Heib if (err) 20144929116bSKamal Heib return err; 20154929116bSKamal Heib 20164929116bSKamal Heib return 0; 20174929116bSKamal Heib } 20184929116bSKamal Heib 20194929116bSKamal Heib static int __ib_query_port(struct ib_device *device, 20204929116bSKamal Heib u8 port_num, 20214929116bSKamal Heib struct ib_port_attr *port_attr) 20224929116bSKamal Heib { 20234929116bSKamal Heib union ib_gid gid = {}; 20244929116bSKamal Heib int err; 20254929116bSKamal Heib 20264929116bSKamal Heib memset(port_attr, 0, sizeof(*port_attr)); 20274929116bSKamal Heib 20284929116bSKamal Heib err = device->ops.query_port(device, port_num, port_attr); 20294929116bSKamal Heib if (err || port_attr->subnet_prefix) 20304929116bSKamal Heib return err; 20314929116bSKamal Heib 20324929116bSKamal Heib if (rdma_port_get_link_layer(device, port_num) != 20334929116bSKamal Heib IB_LINK_LAYER_INFINIBAND) 20344929116bSKamal Heib return 0; 20354929116bSKamal Heib 20364929116bSKamal Heib err = device->ops.query_gid(device, port_num, 0, &gid); 20374929116bSKamal Heib if (err) 20384929116bSKamal Heib return err; 20394929116bSKamal Heib 20404929116bSKamal Heib port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix); 20414929116bSKamal Heib return 0; 20424929116bSKamal Heib } 20434929116bSKamal Heib 20441da177e4SLinus Torvalds /** 20451da177e4SLinus Torvalds * ib_query_port - Query IB port attributes 20461da177e4SLinus Torvalds * @device:Device to query 20471da177e4SLinus Torvalds * @port_num:Port number to query 20481da177e4SLinus Torvalds * @port_attr:Port attributes 20491da177e4SLinus Torvalds * 20501da177e4SLinus Torvalds * ib_query_port() returns the attributes of a port through the 20511da177e4SLinus Torvalds * @port_attr pointer. 20521da177e4SLinus Torvalds */ 20531da177e4SLinus Torvalds int ib_query_port(struct ib_device *device, 20541da177e4SLinus Torvalds u8 port_num, 20551da177e4SLinus Torvalds struct ib_port_attr *port_attr) 20561da177e4SLinus Torvalds { 205724dc831bSYuval Shaia if (!rdma_is_port_valid(device, port_num)) 2058116c0074SRoland Dreier return -EINVAL; 2059116c0074SRoland Dreier 20604929116bSKamal Heib if (rdma_protocol_iwarp(device, port_num)) 20614929116bSKamal Heib return iw_query_port(device, port_num, port_attr); 20624929116bSKamal Heib else 20634929116bSKamal Heib return __ib_query_port(device, port_num, port_attr); 20641da177e4SLinus Torvalds } 20651da177e4SLinus Torvalds EXPORT_SYMBOL(ib_query_port); 20661da177e4SLinus Torvalds 2067324e227eSJason Gunthorpe static void add_ndev_hash(struct ib_port_data *pdata) 2068324e227eSJason Gunthorpe { 2069324e227eSJason Gunthorpe unsigned long flags; 2070324e227eSJason Gunthorpe 2071324e227eSJason Gunthorpe might_sleep(); 2072324e227eSJason Gunthorpe 2073324e227eSJason Gunthorpe spin_lock_irqsave(&ndev_hash_lock, flags); 2074324e227eSJason Gunthorpe if (hash_hashed(&pdata->ndev_hash_link)) { 2075324e227eSJason Gunthorpe hash_del_rcu(&pdata->ndev_hash_link); 2076324e227eSJason Gunthorpe spin_unlock_irqrestore(&ndev_hash_lock, flags); 2077324e227eSJason Gunthorpe /* 2078324e227eSJason Gunthorpe * We cannot do hash_add_rcu after a hash_del_rcu until the 2079324e227eSJason Gunthorpe * grace period 2080324e227eSJason Gunthorpe */ 2081324e227eSJason Gunthorpe synchronize_rcu(); 2082324e227eSJason Gunthorpe spin_lock_irqsave(&ndev_hash_lock, flags); 2083324e227eSJason Gunthorpe } 2084324e227eSJason Gunthorpe if (pdata->netdev) 2085324e227eSJason Gunthorpe hash_add_rcu(ndev_hash, &pdata->ndev_hash_link, 2086324e227eSJason Gunthorpe (uintptr_t)pdata->netdev); 2087324e227eSJason Gunthorpe spin_unlock_irqrestore(&ndev_hash_lock, flags); 2088324e227eSJason Gunthorpe } 2089324e227eSJason Gunthorpe 20901da177e4SLinus Torvalds /** 2091c2261dd7SJason Gunthorpe * ib_device_set_netdev - Associate the ib_dev with an underlying net_device 2092c2261dd7SJason Gunthorpe * @ib_dev: Device to modify 2093c2261dd7SJason Gunthorpe * @ndev: net_device to affiliate, may be NULL 2094c2261dd7SJason Gunthorpe * @port: IB port the net_device is connected to 2095c2261dd7SJason Gunthorpe * 2096c2261dd7SJason Gunthorpe * Drivers should use this to link the ib_device to a netdev so the netdev 2097c2261dd7SJason Gunthorpe * shows up in interfaces like ib_enum_roce_netdev. Only one netdev may be 2098c2261dd7SJason Gunthorpe * affiliated with any port. 2099c2261dd7SJason Gunthorpe * 2100c2261dd7SJason Gunthorpe * The caller must ensure that the given ndev is not unregistered or 2101c2261dd7SJason Gunthorpe * unregistering, and that either the ib_device is unregistered or 2102c2261dd7SJason Gunthorpe * ib_device_set_netdev() is called with NULL when the ndev sends a 2103c2261dd7SJason Gunthorpe * NETDEV_UNREGISTER event. 2104c2261dd7SJason Gunthorpe */ 2105c2261dd7SJason Gunthorpe int ib_device_set_netdev(struct ib_device *ib_dev, struct net_device *ndev, 2106c2261dd7SJason Gunthorpe unsigned int port) 2107c2261dd7SJason Gunthorpe { 2108c2261dd7SJason Gunthorpe struct net_device *old_ndev; 2109c2261dd7SJason Gunthorpe struct ib_port_data *pdata; 2110c2261dd7SJason Gunthorpe unsigned long flags; 2111c2261dd7SJason Gunthorpe int ret; 2112c2261dd7SJason Gunthorpe 2113c2261dd7SJason Gunthorpe /* 2114c2261dd7SJason Gunthorpe * Drivers wish to call this before ib_register_driver, so we have to 2115c2261dd7SJason Gunthorpe * setup the port data early. 2116c2261dd7SJason Gunthorpe */ 2117c2261dd7SJason Gunthorpe ret = alloc_port_data(ib_dev); 2118c2261dd7SJason Gunthorpe if (ret) 2119c2261dd7SJason Gunthorpe return ret; 2120c2261dd7SJason Gunthorpe 2121c2261dd7SJason Gunthorpe if (!rdma_is_port_valid(ib_dev, port)) 2122c2261dd7SJason Gunthorpe return -EINVAL; 2123c2261dd7SJason Gunthorpe 2124c2261dd7SJason Gunthorpe pdata = &ib_dev->port_data[port]; 2125c2261dd7SJason Gunthorpe spin_lock_irqsave(&pdata->netdev_lock, flags); 2126324e227eSJason Gunthorpe old_ndev = rcu_dereference_protected( 2127324e227eSJason Gunthorpe pdata->netdev, lockdep_is_held(&pdata->netdev_lock)); 2128324e227eSJason Gunthorpe if (old_ndev == ndev) { 2129c2261dd7SJason Gunthorpe spin_unlock_irqrestore(&pdata->netdev_lock, flags); 2130c2261dd7SJason Gunthorpe return 0; 2131c2261dd7SJason Gunthorpe } 2132c2261dd7SJason Gunthorpe 2133c2261dd7SJason Gunthorpe if (ndev) 2134c2261dd7SJason Gunthorpe dev_hold(ndev); 2135324e227eSJason Gunthorpe rcu_assign_pointer(pdata->netdev, ndev); 2136c2261dd7SJason Gunthorpe spin_unlock_irqrestore(&pdata->netdev_lock, flags); 2137c2261dd7SJason Gunthorpe 2138324e227eSJason Gunthorpe add_ndev_hash(pdata); 2139c2261dd7SJason Gunthorpe if (old_ndev) 2140c2261dd7SJason Gunthorpe dev_put(old_ndev); 2141c2261dd7SJason Gunthorpe 2142c2261dd7SJason Gunthorpe return 0; 2143c2261dd7SJason Gunthorpe } 2144c2261dd7SJason Gunthorpe EXPORT_SYMBOL(ib_device_set_netdev); 2145c2261dd7SJason Gunthorpe 2146c2261dd7SJason Gunthorpe static void free_netdevs(struct ib_device *ib_dev) 2147c2261dd7SJason Gunthorpe { 2148c2261dd7SJason Gunthorpe unsigned long flags; 2149c2261dd7SJason Gunthorpe unsigned int port; 2150c2261dd7SJason Gunthorpe 215146bdf370SKamal Heib if (!ib_dev->port_data) 215246bdf370SKamal Heib return; 215346bdf370SKamal Heib 2154c2261dd7SJason Gunthorpe rdma_for_each_port (ib_dev, port) { 2155c2261dd7SJason Gunthorpe struct ib_port_data *pdata = &ib_dev->port_data[port]; 2156324e227eSJason Gunthorpe struct net_device *ndev; 2157c2261dd7SJason Gunthorpe 2158c2261dd7SJason Gunthorpe spin_lock_irqsave(&pdata->netdev_lock, flags); 2159324e227eSJason Gunthorpe ndev = rcu_dereference_protected( 2160324e227eSJason Gunthorpe pdata->netdev, lockdep_is_held(&pdata->netdev_lock)); 2161324e227eSJason Gunthorpe if (ndev) { 2162324e227eSJason Gunthorpe spin_lock(&ndev_hash_lock); 2163324e227eSJason Gunthorpe hash_del_rcu(&pdata->ndev_hash_link); 2164324e227eSJason Gunthorpe spin_unlock(&ndev_hash_lock); 2165324e227eSJason Gunthorpe 2166324e227eSJason Gunthorpe /* 2167324e227eSJason Gunthorpe * If this is the last dev_put there is still a 2168324e227eSJason Gunthorpe * synchronize_rcu before the netdev is kfreed, so we 2169324e227eSJason Gunthorpe * can continue to rely on unlocked pointer 2170324e227eSJason Gunthorpe * comparisons after the put 2171324e227eSJason Gunthorpe */ 2172324e227eSJason Gunthorpe rcu_assign_pointer(pdata->netdev, NULL); 2173324e227eSJason Gunthorpe dev_put(ndev); 2174c2261dd7SJason Gunthorpe } 2175c2261dd7SJason Gunthorpe spin_unlock_irqrestore(&pdata->netdev_lock, flags); 2176c2261dd7SJason Gunthorpe } 2177c2261dd7SJason Gunthorpe } 2178c2261dd7SJason Gunthorpe 2179c2261dd7SJason Gunthorpe struct net_device *ib_device_get_netdev(struct ib_device *ib_dev, 2180c2261dd7SJason Gunthorpe unsigned int port) 2181c2261dd7SJason Gunthorpe { 2182c2261dd7SJason Gunthorpe struct ib_port_data *pdata; 2183c2261dd7SJason Gunthorpe struct net_device *res; 2184c2261dd7SJason Gunthorpe 2185c2261dd7SJason Gunthorpe if (!rdma_is_port_valid(ib_dev, port)) 2186c2261dd7SJason Gunthorpe return NULL; 2187c2261dd7SJason Gunthorpe 2188c2261dd7SJason Gunthorpe pdata = &ib_dev->port_data[port]; 2189c2261dd7SJason Gunthorpe 2190c2261dd7SJason Gunthorpe /* 2191c2261dd7SJason Gunthorpe * New drivers should use ib_device_set_netdev() not the legacy 2192c2261dd7SJason Gunthorpe * get_netdev(). 2193c2261dd7SJason Gunthorpe */ 2194c2261dd7SJason Gunthorpe if (ib_dev->ops.get_netdev) 2195c2261dd7SJason Gunthorpe res = ib_dev->ops.get_netdev(ib_dev, port); 2196c2261dd7SJason Gunthorpe else { 2197c2261dd7SJason Gunthorpe spin_lock(&pdata->netdev_lock); 2198324e227eSJason Gunthorpe res = rcu_dereference_protected( 2199324e227eSJason Gunthorpe pdata->netdev, lockdep_is_held(&pdata->netdev_lock)); 2200c2261dd7SJason Gunthorpe if (res) 2201c2261dd7SJason Gunthorpe dev_hold(res); 2202c2261dd7SJason Gunthorpe spin_unlock(&pdata->netdev_lock); 2203c2261dd7SJason Gunthorpe } 2204c2261dd7SJason Gunthorpe 2205c2261dd7SJason Gunthorpe /* 2206c2261dd7SJason Gunthorpe * If we are starting to unregister expedite things by preventing 2207c2261dd7SJason Gunthorpe * propagation of an unregistering netdev. 2208c2261dd7SJason Gunthorpe */ 2209c2261dd7SJason Gunthorpe if (res && res->reg_state != NETREG_REGISTERED) { 2210c2261dd7SJason Gunthorpe dev_put(res); 2211c2261dd7SJason Gunthorpe return NULL; 2212c2261dd7SJason Gunthorpe } 2213c2261dd7SJason Gunthorpe 2214c2261dd7SJason Gunthorpe return res; 2215c2261dd7SJason Gunthorpe } 2216c2261dd7SJason Gunthorpe 2217c2261dd7SJason Gunthorpe /** 2218324e227eSJason Gunthorpe * ib_device_get_by_netdev - Find an IB device associated with a netdev 2219324e227eSJason Gunthorpe * @ndev: netdev to locate 2220324e227eSJason Gunthorpe * @driver_id: The driver ID that must match (RDMA_DRIVER_UNKNOWN matches all) 2221324e227eSJason Gunthorpe * 2222324e227eSJason Gunthorpe * Find and hold an ib_device that is associated with a netdev via 2223324e227eSJason Gunthorpe * ib_device_set_netdev(). The caller must call ib_device_put() on the 2224324e227eSJason Gunthorpe * returned pointer. 2225324e227eSJason Gunthorpe */ 2226324e227eSJason Gunthorpe struct ib_device *ib_device_get_by_netdev(struct net_device *ndev, 2227324e227eSJason Gunthorpe enum rdma_driver_id driver_id) 2228324e227eSJason Gunthorpe { 2229324e227eSJason Gunthorpe struct ib_device *res = NULL; 2230324e227eSJason Gunthorpe struct ib_port_data *cur; 2231324e227eSJason Gunthorpe 2232324e227eSJason Gunthorpe rcu_read_lock(); 2233324e227eSJason Gunthorpe hash_for_each_possible_rcu (ndev_hash, cur, ndev_hash_link, 2234324e227eSJason Gunthorpe (uintptr_t)ndev) { 2235324e227eSJason Gunthorpe if (rcu_access_pointer(cur->netdev) == ndev && 2236324e227eSJason Gunthorpe (driver_id == RDMA_DRIVER_UNKNOWN || 2237b9560a41SJason Gunthorpe cur->ib_dev->ops.driver_id == driver_id) && 2238324e227eSJason Gunthorpe ib_device_try_get(cur->ib_dev)) { 2239324e227eSJason Gunthorpe res = cur->ib_dev; 2240324e227eSJason Gunthorpe break; 2241324e227eSJason Gunthorpe } 2242324e227eSJason Gunthorpe } 2243324e227eSJason Gunthorpe rcu_read_unlock(); 2244324e227eSJason Gunthorpe 2245324e227eSJason Gunthorpe return res; 2246324e227eSJason Gunthorpe } 2247324e227eSJason Gunthorpe EXPORT_SYMBOL(ib_device_get_by_netdev); 2248324e227eSJason Gunthorpe 2249324e227eSJason Gunthorpe /** 225003db3a2dSMatan Barak * ib_enum_roce_netdev - enumerate all RoCE ports 225103db3a2dSMatan Barak * @ib_dev : IB device we want to query 225203db3a2dSMatan Barak * @filter: Should we call the callback? 225303db3a2dSMatan Barak * @filter_cookie: Cookie passed to filter 225403db3a2dSMatan Barak * @cb: Callback to call for each found RoCE ports 225503db3a2dSMatan Barak * @cookie: Cookie passed back to the callback 225603db3a2dSMatan Barak * 225703db3a2dSMatan Barak * Enumerates all of the physical RoCE ports of ib_dev 225803db3a2dSMatan Barak * which are related to netdevice and calls callback() on each 225903db3a2dSMatan Barak * device for which filter() function returns non zero. 226003db3a2dSMatan Barak */ 226103db3a2dSMatan Barak void ib_enum_roce_netdev(struct ib_device *ib_dev, 226203db3a2dSMatan Barak roce_netdev_filter filter, 226303db3a2dSMatan Barak void *filter_cookie, 226403db3a2dSMatan Barak roce_netdev_callback cb, 226503db3a2dSMatan Barak void *cookie) 226603db3a2dSMatan Barak { 2267ea1075edSJason Gunthorpe unsigned int port; 226803db3a2dSMatan Barak 2269ea1075edSJason Gunthorpe rdma_for_each_port (ib_dev, port) 227003db3a2dSMatan Barak if (rdma_protocol_roce(ib_dev, port)) { 2271c2261dd7SJason Gunthorpe struct net_device *idev = 2272c2261dd7SJason Gunthorpe ib_device_get_netdev(ib_dev, port); 227303db3a2dSMatan Barak 227403db3a2dSMatan Barak if (filter(ib_dev, port, idev, filter_cookie)) 227503db3a2dSMatan Barak cb(ib_dev, port, idev, cookie); 227603db3a2dSMatan Barak 227703db3a2dSMatan Barak if (idev) 227803db3a2dSMatan Barak dev_put(idev); 227903db3a2dSMatan Barak } 228003db3a2dSMatan Barak } 228103db3a2dSMatan Barak 228203db3a2dSMatan Barak /** 228303db3a2dSMatan Barak * ib_enum_all_roce_netdevs - enumerate all RoCE devices 228403db3a2dSMatan Barak * @filter: Should we call the callback? 228503db3a2dSMatan Barak * @filter_cookie: Cookie passed to filter 228603db3a2dSMatan Barak * @cb: Callback to call for each found RoCE ports 228703db3a2dSMatan Barak * @cookie: Cookie passed back to the callback 228803db3a2dSMatan Barak * 228903db3a2dSMatan Barak * Enumerates all RoCE devices' physical ports which are related 229003db3a2dSMatan Barak * to netdevices and calls callback() on each device for which 229103db3a2dSMatan Barak * filter() function returns non zero. 229203db3a2dSMatan Barak */ 229303db3a2dSMatan Barak void ib_enum_all_roce_netdevs(roce_netdev_filter filter, 229403db3a2dSMatan Barak void *filter_cookie, 229503db3a2dSMatan Barak roce_netdev_callback cb, 229603db3a2dSMatan Barak void *cookie) 229703db3a2dSMatan Barak { 229803db3a2dSMatan Barak struct ib_device *dev; 22990df91bb6SJason Gunthorpe unsigned long index; 230003db3a2dSMatan Barak 2301921eab11SJason Gunthorpe down_read(&devices_rwsem); 23020df91bb6SJason Gunthorpe xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) 230303db3a2dSMatan Barak ib_enum_roce_netdev(dev, filter, filter_cookie, cb, cookie); 2304921eab11SJason Gunthorpe up_read(&devices_rwsem); 230503db3a2dSMatan Barak } 230603db3a2dSMatan Barak 230703db3a2dSMatan Barak /** 23088030c835SLeon Romanovsky * ib_enum_all_devs - enumerate all ib_devices 23098030c835SLeon Romanovsky * @cb: Callback to call for each found ib_device 23108030c835SLeon Romanovsky * 23118030c835SLeon Romanovsky * Enumerates all ib_devices and calls callback() on each device. 23128030c835SLeon Romanovsky */ 23138030c835SLeon Romanovsky int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb, 23148030c835SLeon Romanovsky struct netlink_callback *cb) 23158030c835SLeon Romanovsky { 23160df91bb6SJason Gunthorpe unsigned long index; 23178030c835SLeon Romanovsky struct ib_device *dev; 23188030c835SLeon Romanovsky unsigned int idx = 0; 23198030c835SLeon Romanovsky int ret = 0; 23208030c835SLeon Romanovsky 2321921eab11SJason Gunthorpe down_read(&devices_rwsem); 23220df91bb6SJason Gunthorpe xa_for_each_marked (&devices, index, dev, DEVICE_REGISTERED) { 232337eeab55SParav Pandit if (!rdma_dev_access_netns(dev, sock_net(skb->sk))) 232437eeab55SParav Pandit continue; 232537eeab55SParav Pandit 23268030c835SLeon Romanovsky ret = nldev_cb(dev, skb, cb, idx); 23278030c835SLeon Romanovsky if (ret) 23288030c835SLeon Romanovsky break; 23298030c835SLeon Romanovsky idx++; 23308030c835SLeon Romanovsky } 2331921eab11SJason Gunthorpe up_read(&devices_rwsem); 23328030c835SLeon Romanovsky return ret; 23338030c835SLeon Romanovsky } 23348030c835SLeon Romanovsky 23358030c835SLeon Romanovsky /** 23361da177e4SLinus Torvalds * ib_query_pkey - Get P_Key table entry 23371da177e4SLinus Torvalds * @device:Device to query 23381da177e4SLinus Torvalds * @port_num:Port number to query 23391da177e4SLinus Torvalds * @index:P_Key table index to query 23401da177e4SLinus Torvalds * @pkey:Returned P_Key 23411da177e4SLinus Torvalds * 23421da177e4SLinus Torvalds * ib_query_pkey() fetches the specified P_Key table entry. 23431da177e4SLinus Torvalds */ 23441da177e4SLinus Torvalds int ib_query_pkey(struct ib_device *device, 23451da177e4SLinus Torvalds u8 port_num, u16 index, u16 *pkey) 23461da177e4SLinus Torvalds { 23479af3f5cfSYuval Shaia if (!rdma_is_port_valid(device, port_num)) 23489af3f5cfSYuval Shaia return -EINVAL; 23499af3f5cfSYuval Shaia 23503023a1e9SKamal Heib return device->ops.query_pkey(device, port_num, index, pkey); 23511da177e4SLinus Torvalds } 23521da177e4SLinus Torvalds EXPORT_SYMBOL(ib_query_pkey); 23531da177e4SLinus Torvalds 23541da177e4SLinus Torvalds /** 23551da177e4SLinus Torvalds * ib_modify_device - Change IB device attributes 23561da177e4SLinus Torvalds * @device:Device to modify 23571da177e4SLinus Torvalds * @device_modify_mask:Mask of attributes to change 23581da177e4SLinus Torvalds * @device_modify:New attribute values 23591da177e4SLinus Torvalds * 23601da177e4SLinus Torvalds * ib_modify_device() changes a device's attributes as specified by 23611da177e4SLinus Torvalds * the @device_modify_mask and @device_modify structure. 23621da177e4SLinus Torvalds */ 23631da177e4SLinus Torvalds int ib_modify_device(struct ib_device *device, 23641da177e4SLinus Torvalds int device_modify_mask, 23651da177e4SLinus Torvalds struct ib_device_modify *device_modify) 23661da177e4SLinus Torvalds { 23673023a1e9SKamal Heib if (!device->ops.modify_device) 236810e1b54bSBart Van Assche return -ENOSYS; 236910e1b54bSBart Van Assche 23703023a1e9SKamal Heib return device->ops.modify_device(device, device_modify_mask, 23711da177e4SLinus Torvalds device_modify); 23721da177e4SLinus Torvalds } 23731da177e4SLinus Torvalds EXPORT_SYMBOL(ib_modify_device); 23741da177e4SLinus Torvalds 23751da177e4SLinus Torvalds /** 23761da177e4SLinus Torvalds * ib_modify_port - Modifies the attributes for the specified port. 23771da177e4SLinus Torvalds * @device: The device to modify. 23781da177e4SLinus Torvalds * @port_num: The number of the port to modify. 23791da177e4SLinus Torvalds * @port_modify_mask: Mask used to specify which attributes of the port 23801da177e4SLinus Torvalds * to change. 23811da177e4SLinus Torvalds * @port_modify: New attribute values for the port. 23821da177e4SLinus Torvalds * 23831da177e4SLinus Torvalds * ib_modify_port() changes a port's attributes as specified by the 23841da177e4SLinus Torvalds * @port_modify_mask and @port_modify structure. 23851da177e4SLinus Torvalds */ 23861da177e4SLinus Torvalds int ib_modify_port(struct ib_device *device, 23871da177e4SLinus Torvalds u8 port_num, int port_modify_mask, 23881da177e4SLinus Torvalds struct ib_port_modify *port_modify) 23891da177e4SLinus Torvalds { 239061e0962dSSelvin Xavier int rc; 239110e1b54bSBart Van Assche 239224dc831bSYuval Shaia if (!rdma_is_port_valid(device, port_num)) 2393116c0074SRoland Dreier return -EINVAL; 2394116c0074SRoland Dreier 23953023a1e9SKamal Heib if (device->ops.modify_port) 23963023a1e9SKamal Heib rc = device->ops.modify_port(device, port_num, 23973023a1e9SKamal Heib port_modify_mask, 23981da177e4SLinus Torvalds port_modify); 239961e0962dSSelvin Xavier else 240061e0962dSSelvin Xavier rc = rdma_protocol_roce(device, port_num) ? 0 : -ENOSYS; 240161e0962dSSelvin Xavier return rc; 24021da177e4SLinus Torvalds } 24031da177e4SLinus Torvalds EXPORT_SYMBOL(ib_modify_port); 24041da177e4SLinus Torvalds 24055eb620c8SYosef Etigin /** 24065eb620c8SYosef Etigin * ib_find_gid - Returns the port number and GID table index where 2407dbb12562SParav Pandit * a specified GID value occurs. Its searches only for IB link layer. 24085eb620c8SYosef Etigin * @device: The device to query. 24095eb620c8SYosef Etigin * @gid: The GID value to search for. 24105eb620c8SYosef Etigin * @port_num: The port number of the device where the GID value was found. 24115eb620c8SYosef Etigin * @index: The index into the GID table where the GID was found. This 24125eb620c8SYosef Etigin * parameter may be NULL. 24135eb620c8SYosef Etigin */ 24145eb620c8SYosef Etigin int ib_find_gid(struct ib_device *device, union ib_gid *gid, 2415b26c4a11SParav Pandit u8 *port_num, u16 *index) 24165eb620c8SYosef Etigin { 24175eb620c8SYosef Etigin union ib_gid tmp_gid; 2418ea1075edSJason Gunthorpe unsigned int port; 2419ea1075edSJason Gunthorpe int ret, i; 24205eb620c8SYosef Etigin 2421ea1075edSJason Gunthorpe rdma_for_each_port (device, port) { 242222d24f75SParav Pandit if (!rdma_protocol_ib(device, port)) 2423b39ffa1dSMatan Barak continue; 2424b39ffa1dSMatan Barak 24258ceb1357SJason Gunthorpe for (i = 0; i < device->port_data[port].immutable.gid_tbl_len; 24268ceb1357SJason Gunthorpe ++i) { 24271dfce294SParav Pandit ret = rdma_query_gid(device, port, i, &tmp_gid); 24285eb620c8SYosef Etigin if (ret) 24295eb620c8SYosef Etigin return ret; 24305eb620c8SYosef Etigin if (!memcmp(&tmp_gid, gid, sizeof *gid)) { 24315eb620c8SYosef Etigin *port_num = port; 24325eb620c8SYosef Etigin if (index) 24335eb620c8SYosef Etigin *index = i; 24345eb620c8SYosef Etigin return 0; 24355eb620c8SYosef Etigin } 24365eb620c8SYosef Etigin } 24375eb620c8SYosef Etigin } 24385eb620c8SYosef Etigin 24395eb620c8SYosef Etigin return -ENOENT; 24405eb620c8SYosef Etigin } 24415eb620c8SYosef Etigin EXPORT_SYMBOL(ib_find_gid); 24425eb620c8SYosef Etigin 24435eb620c8SYosef Etigin /** 24445eb620c8SYosef Etigin * ib_find_pkey - Returns the PKey table index where a specified 24455eb620c8SYosef Etigin * PKey value occurs. 24465eb620c8SYosef Etigin * @device: The device to query. 24475eb620c8SYosef Etigin * @port_num: The port number of the device to search for the PKey. 24485eb620c8SYosef Etigin * @pkey: The PKey value to search for. 24495eb620c8SYosef Etigin * @index: The index into the PKey table where the PKey was found. 24505eb620c8SYosef Etigin */ 24515eb620c8SYosef Etigin int ib_find_pkey(struct ib_device *device, 24525eb620c8SYosef Etigin u8 port_num, u16 pkey, u16 *index) 24535eb620c8SYosef Etigin { 24545eb620c8SYosef Etigin int ret, i; 24555eb620c8SYosef Etigin u16 tmp_pkey; 2456ff7166c4SJack Morgenstein int partial_ix = -1; 24575eb620c8SYosef Etigin 24588ceb1357SJason Gunthorpe for (i = 0; i < device->port_data[port_num].immutable.pkey_tbl_len; 24598ceb1357SJason Gunthorpe ++i) { 24605eb620c8SYosef Etigin ret = ib_query_pkey(device, port_num, i, &tmp_pkey); 24615eb620c8SYosef Etigin if (ret) 24625eb620c8SYosef Etigin return ret; 246336026eccSMoni Shoua if ((pkey & 0x7fff) == (tmp_pkey & 0x7fff)) { 2464ff7166c4SJack Morgenstein /* if there is full-member pkey take it.*/ 2465ff7166c4SJack Morgenstein if (tmp_pkey & 0x8000) { 24665eb620c8SYosef Etigin *index = i; 24675eb620c8SYosef Etigin return 0; 24685eb620c8SYosef Etigin } 2469ff7166c4SJack Morgenstein if (partial_ix < 0) 2470ff7166c4SJack Morgenstein partial_ix = i; 2471ff7166c4SJack Morgenstein } 24725eb620c8SYosef Etigin } 24735eb620c8SYosef Etigin 2474ff7166c4SJack Morgenstein /*no full-member, if exists take the limited*/ 2475ff7166c4SJack Morgenstein if (partial_ix >= 0) { 2476ff7166c4SJack Morgenstein *index = partial_ix; 2477ff7166c4SJack Morgenstein return 0; 2478ff7166c4SJack Morgenstein } 24795eb620c8SYosef Etigin return -ENOENT; 24805eb620c8SYosef Etigin } 24815eb620c8SYosef Etigin EXPORT_SYMBOL(ib_find_pkey); 24825eb620c8SYosef Etigin 24839268f72dSYotam Kenneth /** 24849268f72dSYotam Kenneth * ib_get_net_dev_by_params() - Return the appropriate net_dev 24859268f72dSYotam Kenneth * for a received CM request 24869268f72dSYotam Kenneth * @dev: An RDMA device on which the request has been received. 24879268f72dSYotam Kenneth * @port: Port number on the RDMA device. 24889268f72dSYotam Kenneth * @pkey: The Pkey the request came on. 24899268f72dSYotam Kenneth * @gid: A GID that the net_dev uses to communicate. 24909268f72dSYotam Kenneth * @addr: Contains the IP address that the request specified as its 24919268f72dSYotam Kenneth * destination. 2492921eab11SJason Gunthorpe * 24939268f72dSYotam Kenneth */ 24949268f72dSYotam Kenneth struct net_device *ib_get_net_dev_by_params(struct ib_device *dev, 24959268f72dSYotam Kenneth u8 port, 24969268f72dSYotam Kenneth u16 pkey, 24979268f72dSYotam Kenneth const union ib_gid *gid, 24989268f72dSYotam Kenneth const struct sockaddr *addr) 24999268f72dSYotam Kenneth { 25009268f72dSYotam Kenneth struct net_device *net_dev = NULL; 25010df91bb6SJason Gunthorpe unsigned long index; 25020df91bb6SJason Gunthorpe void *client_data; 25039268f72dSYotam Kenneth 25049268f72dSYotam Kenneth if (!rdma_protocol_ib(dev, port)) 25059268f72dSYotam Kenneth return NULL; 25069268f72dSYotam Kenneth 2507921eab11SJason Gunthorpe /* 2508921eab11SJason Gunthorpe * Holding the read side guarantees that the client will not become 2509921eab11SJason Gunthorpe * unregistered while we are calling get_net_dev_by_params() 2510921eab11SJason Gunthorpe */ 2511921eab11SJason Gunthorpe down_read(&dev->client_data_rwsem); 25120df91bb6SJason Gunthorpe xan_for_each_marked (&dev->client_data, index, client_data, 25130df91bb6SJason Gunthorpe CLIENT_DATA_REGISTERED) { 25140df91bb6SJason Gunthorpe struct ib_client *client = xa_load(&clients, index); 25159268f72dSYotam Kenneth 25160df91bb6SJason Gunthorpe if (!client || !client->get_net_dev_by_params) 25179268f72dSYotam Kenneth continue; 25189268f72dSYotam Kenneth 25190df91bb6SJason Gunthorpe net_dev = client->get_net_dev_by_params(dev, port, pkey, gid, 25200df91bb6SJason Gunthorpe addr, client_data); 25219268f72dSYotam Kenneth if (net_dev) 25229268f72dSYotam Kenneth break; 25239268f72dSYotam Kenneth } 2524921eab11SJason Gunthorpe up_read(&dev->client_data_rwsem); 25259268f72dSYotam Kenneth 25269268f72dSYotam Kenneth return net_dev; 25279268f72dSYotam Kenneth } 25289268f72dSYotam Kenneth EXPORT_SYMBOL(ib_get_net_dev_by_params); 25299268f72dSYotam Kenneth 2530521ed0d9SKamal Heib void ib_set_device_ops(struct ib_device *dev, const struct ib_device_ops *ops) 2531521ed0d9SKamal Heib { 25323023a1e9SKamal Heib struct ib_device_ops *dev_ops = &dev->ops; 2533521ed0d9SKamal Heib #define SET_DEVICE_OP(ptr, name) \ 2534521ed0d9SKamal Heib do { \ 2535521ed0d9SKamal Heib if (ops->name) \ 2536521ed0d9SKamal Heib if (!((ptr)->name)) \ 2537521ed0d9SKamal Heib (ptr)->name = ops->name; \ 2538521ed0d9SKamal Heib } while (0) 2539521ed0d9SKamal Heib 254030471d4bSLeon Romanovsky #define SET_OBJ_SIZE(ptr, name) SET_DEVICE_OP(ptr, size_##name) 254130471d4bSLeon Romanovsky 2542b9560a41SJason Gunthorpe if (ops->driver_id != RDMA_DRIVER_UNKNOWN) { 2543b9560a41SJason Gunthorpe WARN_ON(dev_ops->driver_id != RDMA_DRIVER_UNKNOWN && 2544b9560a41SJason Gunthorpe dev_ops->driver_id != ops->driver_id); 2545b9560a41SJason Gunthorpe dev_ops->driver_id = ops->driver_id; 2546b9560a41SJason Gunthorpe } 25477a154142SJason Gunthorpe if (ops->owner) { 25487a154142SJason Gunthorpe WARN_ON(dev_ops->owner && dev_ops->owner != ops->owner); 25497a154142SJason Gunthorpe dev_ops->owner = ops->owner; 25507a154142SJason Gunthorpe } 255172c6ec18SJason Gunthorpe if (ops->uverbs_abi_ver) 255272c6ec18SJason Gunthorpe dev_ops->uverbs_abi_ver = ops->uverbs_abi_ver; 2553b9560a41SJason Gunthorpe 25548f71bb00SJason Gunthorpe dev_ops->uverbs_no_driver_id_binding |= 25558f71bb00SJason Gunthorpe ops->uverbs_no_driver_id_binding; 25568f71bb00SJason Gunthorpe 25573023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, add_gid); 25582f1927b0SMoni Shoua SET_DEVICE_OP(dev_ops, advise_mr); 25593023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, alloc_dm); 25603023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, alloc_fmr); 25613023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, alloc_hw_stats); 25623023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, alloc_mr); 256326bc7eaeSIsrael Rukshin SET_DEVICE_OP(dev_ops, alloc_mr_integrity); 25643023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, alloc_mw); 25653023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, alloc_pd); 25663023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, alloc_rdma_netdev); 25673023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, alloc_ucontext); 25683023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, alloc_xrcd); 25693023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, attach_mcast); 25703023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, check_mr_status); 2571c4ffee7cSMark Zhang SET_DEVICE_OP(dev_ops, counter_alloc_stats); 257299fa331dSMark Zhang SET_DEVICE_OP(dev_ops, counter_bind_qp); 257399fa331dSMark Zhang SET_DEVICE_OP(dev_ops, counter_dealloc); 257499fa331dSMark Zhang SET_DEVICE_OP(dev_ops, counter_unbind_qp); 2575c4ffee7cSMark Zhang SET_DEVICE_OP(dev_ops, counter_update_stats); 25763023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, create_ah); 25773023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, create_counters); 25783023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, create_cq); 25793023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, create_flow); 25803023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, create_flow_action_esp); 25813023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, create_qp); 25823023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, create_rwq_ind_table); 25833023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, create_srq); 25843023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, create_wq); 25853023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, dealloc_dm); 2586d0899892SJason Gunthorpe SET_DEVICE_OP(dev_ops, dealloc_driver); 25873023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, dealloc_fmr); 25883023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, dealloc_mw); 25893023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, dealloc_pd); 25903023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, dealloc_ucontext); 25913023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, dealloc_xrcd); 25923023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, del_gid); 25933023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, dereg_mr); 25943023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, destroy_ah); 25953023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, destroy_counters); 25963023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, destroy_cq); 25973023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, destroy_flow); 25983023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, destroy_flow_action); 25993023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, destroy_qp); 26003023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, destroy_rwq_ind_table); 26013023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, destroy_srq); 26023023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, destroy_wq); 26033023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, detach_mcast); 26043023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, disassociate_ucontext); 26053023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, drain_rq); 26063023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, drain_sq); 2607ca22354bSJason Gunthorpe SET_DEVICE_OP(dev_ops, enable_driver); 260802da3750SLeon Romanovsky SET_DEVICE_OP(dev_ops, fill_res_entry); 26093023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, get_dev_fw_str); 26103023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, get_dma_mr); 26113023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, get_hw_stats); 26123023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, get_link_layer); 26133023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, get_netdev); 26143023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, get_port_immutable); 26153023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, get_vector_affinity); 26163023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, get_vf_config); 2617*bfcb3c5dSDanit Goldberg SET_DEVICE_OP(dev_ops, get_vf_guid); 26183023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, get_vf_stats); 2619ea4baf7fSParav Pandit SET_DEVICE_OP(dev_ops, init_port); 2620ce51346fSMoni Shoua SET_DEVICE_OP(dev_ops, invalidate_range); 2621dd05cb82SKamal Heib SET_DEVICE_OP(dev_ops, iw_accept); 2622dd05cb82SKamal Heib SET_DEVICE_OP(dev_ops, iw_add_ref); 2623dd05cb82SKamal Heib SET_DEVICE_OP(dev_ops, iw_connect); 2624dd05cb82SKamal Heib SET_DEVICE_OP(dev_ops, iw_create_listen); 2625dd05cb82SKamal Heib SET_DEVICE_OP(dev_ops, iw_destroy_listen); 2626dd05cb82SKamal Heib SET_DEVICE_OP(dev_ops, iw_get_qp); 2627dd05cb82SKamal Heib SET_DEVICE_OP(dev_ops, iw_reject); 2628dd05cb82SKamal Heib SET_DEVICE_OP(dev_ops, iw_rem_ref); 26293023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, map_mr_sg); 26302cdfcdd8SMax Gurtovoy SET_DEVICE_OP(dev_ops, map_mr_sg_pi); 26313023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, map_phys_fmr); 26323023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, mmap); 26333023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, modify_ah); 26343023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, modify_cq); 26353023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, modify_device); 26363023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, modify_flow_action_esp); 26373023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, modify_port); 26383023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, modify_qp); 26393023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, modify_srq); 26403023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, modify_wq); 26413023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, peek_cq); 26423023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, poll_cq); 26433023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, post_recv); 26443023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, post_send); 26453023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, post_srq_recv); 26463023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, process_mad); 26473023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, query_ah); 26483023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, query_device); 26493023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, query_gid); 26503023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, query_pkey); 26513023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, query_port); 26523023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, query_qp); 26533023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, query_srq); 26543023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, rdma_netdev_get_params); 26553023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, read_counters); 26563023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, reg_dm_mr); 26573023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, reg_user_mr); 26583023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, req_ncomp_notif); 26593023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, req_notify_cq); 26603023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, rereg_user_mr); 26613023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, resize_cq); 26623023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, set_vf_guid); 26633023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, set_vf_link_state); 26643023a1e9SKamal Heib SET_DEVICE_OP(dev_ops, unmap_fmr); 266521a428a0SLeon Romanovsky 2666d3456914SLeon Romanovsky SET_OBJ_SIZE(dev_ops, ib_ah); 2667e39afe3dSLeon Romanovsky SET_OBJ_SIZE(dev_ops, ib_cq); 266821a428a0SLeon Romanovsky SET_OBJ_SIZE(dev_ops, ib_pd); 266968e326deSLeon Romanovsky SET_OBJ_SIZE(dev_ops, ib_srq); 2670a2a074efSLeon Romanovsky SET_OBJ_SIZE(dev_ops, ib_ucontext); 2671521ed0d9SKamal Heib } 2672521ed0d9SKamal Heib EXPORT_SYMBOL(ib_set_device_ops); 2673521ed0d9SKamal Heib 2674d0e312feSLeon Romanovsky static const struct rdma_nl_cbs ibnl_ls_cb_table[RDMA_NL_LS_NUM_OPS] = { 2675735c631aSMark Bloch [RDMA_NL_LS_OP_RESOLVE] = { 2676647c75acSLeon Romanovsky .doit = ib_nl_handle_resolve_resp, 2677e3a2b93dSLeon Romanovsky .flags = RDMA_NL_ADMIN_PERM, 2678e3a2b93dSLeon Romanovsky }, 2679735c631aSMark Bloch [RDMA_NL_LS_OP_SET_TIMEOUT] = { 2680647c75acSLeon Romanovsky .doit = ib_nl_handle_set_timeout, 2681e3a2b93dSLeon Romanovsky .flags = RDMA_NL_ADMIN_PERM, 2682e3a2b93dSLeon Romanovsky }, 2683ae43f828SMark Bloch [RDMA_NL_LS_OP_IP_RESOLVE] = { 2684647c75acSLeon Romanovsky .doit = ib_nl_handle_ip_res_resp, 2685e3a2b93dSLeon Romanovsky .flags = RDMA_NL_ADMIN_PERM, 2686e3a2b93dSLeon Romanovsky }, 2687735c631aSMark Bloch }; 2688735c631aSMark Bloch 26891da177e4SLinus Torvalds static int __init ib_core_init(void) 26901da177e4SLinus Torvalds { 26911da177e4SLinus Torvalds int ret; 26921da177e4SLinus Torvalds 2693f0626710STejun Heo ib_wq = alloc_workqueue("infiniband", 0, 0); 2694f0626710STejun Heo if (!ib_wq) 2695f0626710STejun Heo return -ENOMEM; 2696f0626710STejun Heo 269714d3a3b2SChristoph Hellwig ib_comp_wq = alloc_workqueue("ib-comp-wq", 2698b7363e67SSagi Grimberg WQ_HIGHPRI | WQ_MEM_RECLAIM | WQ_SYSFS, 0); 269914d3a3b2SChristoph Hellwig if (!ib_comp_wq) { 270014d3a3b2SChristoph Hellwig ret = -ENOMEM; 270114d3a3b2SChristoph Hellwig goto err; 270214d3a3b2SChristoph Hellwig } 270314d3a3b2SChristoph Hellwig 2704f794809aSJack Morgenstein ib_comp_unbound_wq = 2705f794809aSJack Morgenstein alloc_workqueue("ib-comp-unb-wq", 2706f794809aSJack Morgenstein WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM | 2707f794809aSJack Morgenstein WQ_SYSFS, WQ_UNBOUND_MAX_ACTIVE); 2708f794809aSJack Morgenstein if (!ib_comp_unbound_wq) { 2709f794809aSJack Morgenstein ret = -ENOMEM; 2710f794809aSJack Morgenstein goto err_comp; 2711f794809aSJack Morgenstein } 2712f794809aSJack Morgenstein 271355aeed06SJason Gunthorpe ret = class_register(&ib_class); 2714fd75c789SNir Muchtar if (ret) { 2715aba25a3eSParav Pandit pr_warn("Couldn't create InfiniBand device class\n"); 2716f794809aSJack Morgenstein goto err_comp_unbound; 2717fd75c789SNir Muchtar } 27181da177e4SLinus Torvalds 2719e3f20f02SLeon Romanovsky ret = addr_init(); 2720e3f20f02SLeon Romanovsky if (ret) { 2721e3f20f02SLeon Romanovsky pr_warn("Could't init IB address resolution\n"); 2722e3f20f02SLeon Romanovsky goto err_ibnl; 2723e3f20f02SLeon Romanovsky } 2724e3f20f02SLeon Romanovsky 27254c2cb422SMark Bloch ret = ib_mad_init(); 27264c2cb422SMark Bloch if (ret) { 27274c2cb422SMark Bloch pr_warn("Couldn't init IB MAD\n"); 27284c2cb422SMark Bloch goto err_addr; 27294c2cb422SMark Bloch } 27304c2cb422SMark Bloch 2731c2e49c92SMark Bloch ret = ib_sa_init(); 2732c2e49c92SMark Bloch if (ret) { 2733c2e49c92SMark Bloch pr_warn("Couldn't init SA\n"); 2734c2e49c92SMark Bloch goto err_mad; 2735c2e49c92SMark Bloch } 2736c2e49c92SMark Bloch 273742df744cSJanne Karhunen ret = register_blocking_lsm_notifier(&ibdev_lsm_nb); 27388f408ab6SDaniel Jurgens if (ret) { 27398f408ab6SDaniel Jurgens pr_warn("Couldn't register LSM notifier. ret %d\n", ret); 2740c9901724SLeon Romanovsky goto err_sa; 27418f408ab6SDaniel Jurgens } 27428f408ab6SDaniel Jurgens 27434e0f7b90SParav Pandit ret = register_pernet_device(&rdma_dev_net_ops); 27444e0f7b90SParav Pandit if (ret) { 27454e0f7b90SParav Pandit pr_warn("Couldn't init compat dev. ret %d\n", ret); 27464e0f7b90SParav Pandit goto err_compat; 27474e0f7b90SParav Pandit } 27484e0f7b90SParav Pandit 27496c80b41aSLeon Romanovsky nldev_init(); 2750c9901724SLeon Romanovsky rdma_nl_register(RDMA_NL_LS, ibnl_ls_cb_table); 27515ef8c0c1SJason Gunthorpe roce_gid_mgmt_init(); 2752b2cbae2cSRoland Dreier 2753fd75c789SNir Muchtar return 0; 2754fd75c789SNir Muchtar 27554e0f7b90SParav Pandit err_compat: 275642df744cSJanne Karhunen unregister_blocking_lsm_notifier(&ibdev_lsm_nb); 2757735c631aSMark Bloch err_sa: 2758735c631aSMark Bloch ib_sa_cleanup(); 2759c2e49c92SMark Bloch err_mad: 2760c2e49c92SMark Bloch ib_mad_cleanup(); 27614c2cb422SMark Bloch err_addr: 27624c2cb422SMark Bloch addr_cleanup(); 2763e3f20f02SLeon Romanovsky err_ibnl: 276455aeed06SJason Gunthorpe class_unregister(&ib_class); 2765f794809aSJack Morgenstein err_comp_unbound: 2766f794809aSJack Morgenstein destroy_workqueue(ib_comp_unbound_wq); 276714d3a3b2SChristoph Hellwig err_comp: 276814d3a3b2SChristoph Hellwig destroy_workqueue(ib_comp_wq); 2769fd75c789SNir Muchtar err: 2770fd75c789SNir Muchtar destroy_workqueue(ib_wq); 27711da177e4SLinus Torvalds return ret; 27721da177e4SLinus Torvalds } 27731da177e4SLinus Torvalds 27741da177e4SLinus Torvalds static void __exit ib_core_cleanup(void) 27751da177e4SLinus Torvalds { 27765ef8c0c1SJason Gunthorpe roce_gid_mgmt_cleanup(); 27776c80b41aSLeon Romanovsky nldev_exit(); 2778c9901724SLeon Romanovsky rdma_nl_unregister(RDMA_NL_LS); 27794e0f7b90SParav Pandit unregister_pernet_device(&rdma_dev_net_ops); 278042df744cSJanne Karhunen unregister_blocking_lsm_notifier(&ibdev_lsm_nb); 2781c2e49c92SMark Bloch ib_sa_cleanup(); 27824c2cb422SMark Bloch ib_mad_cleanup(); 2783e3f20f02SLeon Romanovsky addr_cleanup(); 2784c9901724SLeon Romanovsky rdma_nl_exit(); 278555aeed06SJason Gunthorpe class_unregister(&ib_class); 2786f794809aSJack Morgenstein destroy_workqueue(ib_comp_unbound_wq); 278714d3a3b2SChristoph Hellwig destroy_workqueue(ib_comp_wq); 2788f7c6a7b5SRoland Dreier /* Make sure that any pending umem accounting work is done. */ 2789f0626710STejun Heo destroy_workqueue(ib_wq); 2790d0899892SJason Gunthorpe flush_workqueue(system_unbound_wq); 2791e59178d8SJason Gunthorpe WARN_ON(!xa_empty(&clients)); 27920df91bb6SJason Gunthorpe WARN_ON(!xa_empty(&devices)); 27931da177e4SLinus Torvalds } 27941da177e4SLinus Torvalds 2795e3bf14bdSJason Gunthorpe MODULE_ALIAS_RDMA_NETLINK(RDMA_NL_LS, 4); 2796e3bf14bdSJason Gunthorpe 279762dfa795SParav Pandit /* ib core relies on netdev stack to first register net_ns_type_operations 279862dfa795SParav Pandit * ns kobject type before ib_core initialization. 279962dfa795SParav Pandit */ 280062dfa795SParav Pandit fs_initcall(ib_core_init); 28011da177e4SLinus Torvalds module_exit(ib_core_cleanup); 2802