device.c (8ceb1357b33790193e9d55d2d09bcfd6bd59dd6d) device.c (c2261dd76b549754c14c8ac7cadadd0993b182d6)
1/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the

--- 120 unchanged lines hidden (view full) ---

129 }
130 return XA_ERROR(-ENOENT);
131}
132#define xan_for_each_marked(xa, index, entry, filter) \
133 for (index = 0, entry = xan_find_marked(xa, &(index), filter); \
134 !xa_is_err(entry); \
135 (index)++, entry = xan_find_marked(xa, &(index), filter))
136
1/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the

--- 120 unchanged lines hidden (view full) ---

129 }
130 return XA_ERROR(-ENOENT);
131}
132#define xan_for_each_marked(xa, index, entry, filter) \
133 for (index = 0, entry = xan_find_marked(xa, &(index), filter); \
134 !xa_is_err(entry); \
135 (index)++, entry = xan_find_marked(xa, &(index), filter))
136
137static void free_netdevs(struct ib_device *ib_dev);
137static int ib_security_change(struct notifier_block *nb, unsigned long event,
138 void *lsm_data);
139static void ib_policy_change_task(struct work_struct *work);
140static DECLARE_WORK(ib_policy_change_work, ib_policy_change_task);
141
142static struct notifier_block ibdev_lsm_nb = {
143 .notifier_call = ib_security_change,
144};

--- 140 unchanged lines hidden (view full) ---

285 ida_destroy(&inuse);
286 return rc;
287}
288
289static void ib_device_release(struct device *device)
290{
291 struct ib_device *dev = container_of(device, struct ib_device, dev);
292
138static int ib_security_change(struct notifier_block *nb, unsigned long event,
139 void *lsm_data);
140static void ib_policy_change_task(struct work_struct *work);
141static DECLARE_WORK(ib_policy_change_work, ib_policy_change_task);
142
143static struct notifier_block ibdev_lsm_nb = {
144 .notifier_call = ib_security_change,
145};

--- 140 unchanged lines hidden (view full) ---

286 ida_destroy(&inuse);
287 return rc;
288}
289
290static void ib_device_release(struct device *device)
291{
292 struct ib_device *dev = container_of(device, struct ib_device, dev);
293
294 free_netdevs(dev);
293 WARN_ON(refcount_read(&dev->refcount));
294 ib_cache_release_one(dev);
295 ib_security_release_port_pkey_list(dev);
296 kfree(dev->port_data);
297 xa_destroy(&dev->client_data);
298 kfree(dev);
299}
300

--- 65 unchanged lines hidden (view full) ---

366/**
367 * ib_dealloc_device - free an IB device struct
368 * @device:structure to free
369 *
370 * Free a structure allocated with ib_alloc_device().
371 */
372void ib_dealloc_device(struct ib_device *device)
373{
295 WARN_ON(refcount_read(&dev->refcount));
296 ib_cache_release_one(dev);
297 ib_security_release_port_pkey_list(dev);
298 kfree(dev->port_data);
299 xa_destroy(&dev->client_data);
300 kfree(dev);
301}
302

--- 65 unchanged lines hidden (view full) ---

368/**
369 * ib_dealloc_device - free an IB device struct
370 * @device:structure to free
371 *
372 * Free a structure allocated with ib_alloc_device().
373 */
374void ib_dealloc_device(struct ib_device *device)
375{
376 /* Expedite releasing netdev references */
377 free_netdevs(device);
378
374 WARN_ON(!xa_empty(&device->client_data));
375 WARN_ON(refcount_read(&device->refcount));
376 rdma_restrack_clean(device);
377 /* Balances with device_initialize */
378 put_device(&device->dev);
379}
380EXPORT_SYMBOL(ib_dealloc_device);
381

--- 74 unchanged lines hidden (view full) ---

456 */
457 if (client->remove)
458 client->remove(device, client_data);
459
460 xa_erase(&device->client_data, client_id);
461 up_read(&device->client_data_rwsem);
462}
463
379 WARN_ON(!xa_empty(&device->client_data));
380 WARN_ON(refcount_read(&device->refcount));
381 rdma_restrack_clean(device);
382 /* Balances with device_initialize */
383 put_device(&device->dev);
384}
385EXPORT_SYMBOL(ib_dealloc_device);
386

--- 74 unchanged lines hidden (view full) ---

461 */
462 if (client->remove)
463 client->remove(device, client_data);
464
465 xa_erase(&device->client_data, client_id);
466 up_read(&device->client_data_rwsem);
467}
468
464static int verify_immutable(const struct ib_device *dev, u8 port)
469static int alloc_port_data(struct ib_device *device)
465{
470{
466 return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
467 rdma_max_mad_size(dev, port) != 0);
468}
469
470static int setup_port_data(struct ib_device *device)
471{
472 unsigned int port;
471 unsigned int port;
473 int ret;
474
472
473 if (device->port_data)
474 return 0;
475
476 /* This can only be called once the physical port range is defined */
477 if (WARN_ON(!device->phys_port_cnt))
478 return -EINVAL;
479
475 /*
476 * device->port_data is indexed directly by the port number to make
477 * access to this data as efficient as possible.
478 *
479 * Therefore port_data is declared as a 1 based array with potential
480 * empty slots at the beginning.
481 */
482 device->port_data = kcalloc(rdma_end_port(device) + 1,
483 sizeof(*device->port_data), GFP_KERNEL);
484 if (!device->port_data)
485 return -ENOMEM;
486
487 rdma_for_each_port (device, port) {
488 struct ib_port_data *pdata = &device->port_data[port];
489
490 spin_lock_init(&pdata->pkey_list_lock);
491 INIT_LIST_HEAD(&pdata->pkey_list);
480 /*
481 * device->port_data is indexed directly by the port number to make
482 * access to this data as efficient as possible.
483 *
484 * Therefore port_data is declared as a 1 based array with potential
485 * empty slots at the beginning.
486 */
487 device->port_data = kcalloc(rdma_end_port(device) + 1,
488 sizeof(*device->port_data), GFP_KERNEL);
489 if (!device->port_data)
490 return -ENOMEM;
491
492 rdma_for_each_port (device, port) {
493 struct ib_port_data *pdata = &device->port_data[port];
494
495 spin_lock_init(&pdata->pkey_list_lock);
496 INIT_LIST_HEAD(&pdata->pkey_list);
497 spin_lock_init(&pdata->netdev_lock);
498 }
499 return 0;
500}
492
501
502static int verify_immutable(const struct ib_device *dev, u8 port)
503{
504 return WARN_ON(!rdma_cap_ib_mad(dev, port) &&
505 rdma_max_mad_size(dev, port) != 0);
506}
507
508static int setup_port_data(struct ib_device *device)
509{
510 unsigned int port;
511 int ret;
512
513 ret = alloc_port_data(device);
514 if (ret)
515 return ret;
516
517 rdma_for_each_port (device, port) {
518 struct ib_port_data *pdata = &device->port_data[port];
519
493 ret = device->ops.get_port_immutable(device, port,
494 &pdata->immutable);
495 if (ret)
496 return ret;
497
498 if (verify_immutable(device, port))
499 return -EINVAL;
500 }

--- 176 unchanged lines hidden (view full) ---

677 down_read(&clients_rwsem);
678 list_for_each_entry_reverse(client, &client_list, list)
679 remove_client_context(device, client->client_id);
680 up_read(&clients_rwsem);
681
682 /* Pairs with refcount_set in enable_device */
683 ib_device_put(device);
684 wait_for_completion(&device->unreg_completion);
520 ret = device->ops.get_port_immutable(device, port,
521 &pdata->immutable);
522 if (ret)
523 return ret;
524
525 if (verify_immutable(device, port))
526 return -EINVAL;
527 }

--- 176 unchanged lines hidden (view full) ---

704 down_read(&clients_rwsem);
705 list_for_each_entry_reverse(client, &client_list, list)
706 remove_client_context(device, client->client_id);
707 up_read(&clients_rwsem);
708
709 /* Pairs with refcount_set in enable_device */
710 ib_device_put(device);
711 wait_for_completion(&device->unreg_completion);
712
713 /* Expedite removing unregistered pointers from the hash table */
714 free_netdevs(device);
685}
686
687/*
688 * An enabled device is visible to all clients and to all the public facing
689 * APIs that return a device pointer.
690 */
691static int enable_device(struct ib_device *device)
692{

--- 315 unchanged lines hidden (view full) ---

1008 return err;
1009
1010 port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
1011 return 0;
1012}
1013EXPORT_SYMBOL(ib_query_port);
1014
1015/**
715}
716
717/*
718 * An enabled device is visible to all clients and to all the public facing
719 * APIs that return a device pointer.
720 */
721static int enable_device(struct ib_device *device)
722{

--- 315 unchanged lines hidden (view full) ---

1038 return err;
1039
1040 port_attr->subnet_prefix = be64_to_cpu(gid.global.subnet_prefix);
1041 return 0;
1042}
1043EXPORT_SYMBOL(ib_query_port);
1044
1045/**
1046 * ib_device_set_netdev - Associate the ib_dev with an underlying net_device
1047 * @ib_dev: Device to modify
1048 * @ndev: net_device to affiliate, may be NULL
1049 * @port: IB port the net_device is connected to
1050 *
1051 * Drivers should use this to link the ib_device to a netdev so the netdev
1052 * shows up in interfaces like ib_enum_roce_netdev. Only one netdev may be
1053 * affiliated with any port.
1054 *
1055 * The caller must ensure that the given ndev is not unregistered or
1056 * unregistering, and that either the ib_device is unregistered or
1057 * ib_device_set_netdev() is called with NULL when the ndev sends a
1058 * NETDEV_UNREGISTER event.
1059 */
1060int ib_device_set_netdev(struct ib_device *ib_dev, struct net_device *ndev,
1061 unsigned int port)
1062{
1063 struct net_device *old_ndev;
1064 struct ib_port_data *pdata;
1065 unsigned long flags;
1066 int ret;
1067
1068 /*
1069 * Drivers wish to call this before ib_register_driver, so we have to
1070 * setup the port data early.
1071 */
1072 ret = alloc_port_data(ib_dev);
1073 if (ret)
1074 return ret;
1075
1076 if (!rdma_is_port_valid(ib_dev, port))
1077 return -EINVAL;
1078
1079 pdata = &ib_dev->port_data[port];
1080 spin_lock_irqsave(&pdata->netdev_lock, flags);
1081 if (pdata->netdev == ndev) {
1082 spin_unlock_irqrestore(&pdata->netdev_lock, flags);
1083 return 0;
1084 }
1085 old_ndev = pdata->netdev;
1086
1087 if (ndev)
1088 dev_hold(ndev);
1089 pdata->netdev = ndev;
1090 spin_unlock_irqrestore(&pdata->netdev_lock, flags);
1091
1092 if (old_ndev)
1093 dev_put(old_ndev);
1094
1095 return 0;
1096}
1097EXPORT_SYMBOL(ib_device_set_netdev);
1098
1099static void free_netdevs(struct ib_device *ib_dev)
1100{
1101 unsigned long flags;
1102 unsigned int port;
1103
1104 rdma_for_each_port (ib_dev, port) {
1105 struct ib_port_data *pdata = &ib_dev->port_data[port];
1106
1107 spin_lock_irqsave(&pdata->netdev_lock, flags);
1108 if (pdata->netdev) {
1109 dev_put(pdata->netdev);
1110 pdata->netdev = NULL;
1111 }
1112 spin_unlock_irqrestore(&pdata->netdev_lock, flags);
1113 }
1114}
1115
1116struct net_device *ib_device_get_netdev(struct ib_device *ib_dev,
1117 unsigned int port)
1118{
1119 struct ib_port_data *pdata;
1120 struct net_device *res;
1121
1122 if (!rdma_is_port_valid(ib_dev, port))
1123 return NULL;
1124
1125 pdata = &ib_dev->port_data[port];
1126
1127 /*
1128 * New drivers should use ib_device_set_netdev() not the legacy
1129 * get_netdev().
1130 */
1131 if (ib_dev->ops.get_netdev)
1132 res = ib_dev->ops.get_netdev(ib_dev, port);
1133 else {
1134 spin_lock(&pdata->netdev_lock);
1135 res = pdata->netdev;
1136 if (res)
1137 dev_hold(res);
1138 spin_unlock(&pdata->netdev_lock);
1139 }
1140
1141 /*
1142 * If we are starting to unregister expedite things by preventing
1143 * propagation of an unregistering netdev.
1144 */
1145 if (res && res->reg_state != NETREG_REGISTERED) {
1146 dev_put(res);
1147 return NULL;
1148 }
1149
1150 return res;
1151}
1152
1153/**
1016 * ib_enum_roce_netdev - enumerate all RoCE ports
1017 * @ib_dev : IB device we want to query
1018 * @filter: Should we call the callback?
1019 * @filter_cookie: Cookie passed to filter
1020 * @cb: Callback to call for each found RoCE ports
1021 * @cookie: Cookie passed back to the callback
1022 *
1023 * Enumerates all of the physical RoCE ports of ib_dev

--- 5 unchanged lines hidden (view full) ---

1029 void *filter_cookie,
1030 roce_netdev_callback cb,
1031 void *cookie)
1032{
1033 unsigned int port;
1034
1035 rdma_for_each_port (ib_dev, port)
1036 if (rdma_protocol_roce(ib_dev, port)) {
1154 * ib_enum_roce_netdev - enumerate all RoCE ports
1155 * @ib_dev : IB device we want to query
1156 * @filter: Should we call the callback?
1157 * @filter_cookie: Cookie passed to filter
1158 * @cb: Callback to call for each found RoCE ports
1159 * @cookie: Cookie passed back to the callback
1160 *
1161 * Enumerates all of the physical RoCE ports of ib_dev

--- 5 unchanged lines hidden (view full) ---

1167 void *filter_cookie,
1168 roce_netdev_callback cb,
1169 void *cookie)
1170{
1171 unsigned int port;
1172
1173 rdma_for_each_port (ib_dev, port)
1174 if (rdma_protocol_roce(ib_dev, port)) {
1037 struct net_device *idev = NULL;
1175 struct net_device *idev =
1176 ib_device_get_netdev(ib_dev, port);
1038
1177
1039 if (ib_dev->ops.get_netdev)
1040 idev = ib_dev->ops.get_netdev(ib_dev, port);
1041
1042 if (idev &&
1043 idev->reg_state >= NETREG_UNREGISTERED) {
1044 dev_put(idev);
1045 idev = NULL;
1046 }
1047
1048 if (filter(ib_dev, port, idev, filter_cookie))
1049 cb(ib_dev, port, idev, cookie);
1050
1051 if (idev)
1052 dev_put(idev);
1053 }
1054}
1055

--- 474 unchanged lines hidden ---
1178 if (filter(ib_dev, port, idev, filter_cookie))
1179 cb(ib_dev, port, idev, cookie);
1180
1181 if (idev)
1182 dev_put(idev);
1183 }
1184}
1185

--- 474 unchanged lines hidden ---