Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

IB/core: Add helpers for uncached GID and P_Key searches

Add ib_find_gid() and ib_find_pkey() functions that use uncached device
queries. The calls might block but the returns are always up-to-date.
Cache P_Key and GID table lengths in core to avoid extra port info queries.

Signed-off-by: Yosef Etigin <yosefe@voltaire.com>
Acked-by: Michael S. Tsirkin <mst@dev.mellanox.co.il>
Signed-off-by: Roland Dreier <rolandd@cisco.com>

authored by

Yosef Etigin and committed by
Roland Dreier
5eb620c8 8b8c8bca

+133
+125
drivers/infiniband/core/device.c
··· 149 149 return 0; 150 150 } 151 151 152 + static int start_port(struct ib_device *device) 153 + { 154 + return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1; 155 + } 156 + 157 + 158 + static int end_port(struct ib_device *device) 159 + { 160 + return (device->node_type == RDMA_NODE_IB_SWITCH) ? 161 + 0 : device->phys_port_cnt; 162 + } 163 + 152 164 /** 153 165 * ib_alloc_device - allocate an IB device struct 154 166 * @size:size of structure to allocate ··· 220 208 return 0; 221 209 } 222 210 211 + static int read_port_table_lengths(struct ib_device *device) 212 + { 213 + struct ib_port_attr *tprops = NULL; 214 + int num_ports, ret = -ENOMEM; 215 + u8 port_index; 216 + 217 + tprops = kmalloc(sizeof *tprops, GFP_KERNEL); 218 + if (!tprops) 219 + goto out; 220 + 221 + num_ports = end_port(device) - start_port(device) + 1; 222 + 223 + device->pkey_tbl_len = kmalloc(sizeof *device->pkey_tbl_len * num_ports, 224 + GFP_KERNEL); 225 + device->gid_tbl_len = kmalloc(sizeof *device->gid_tbl_len * num_ports, 226 + GFP_KERNEL); 227 + if (!device->pkey_tbl_len || !device->gid_tbl_len) 228 + goto err; 229 + 230 + for (port_index = 0; port_index < num_ports; ++port_index) { 231 + ret = ib_query_port(device, port_index + start_port(device), 232 + tprops); 233 + if (ret) 234 + goto err; 235 + device->pkey_tbl_len[port_index] = tprops->pkey_tbl_len; 236 + device->gid_tbl_len[port_index] = tprops->gid_tbl_len; 237 + } 238 + 239 + ret = 0; 240 + goto out; 241 + 242 + err: 243 + kfree(device->gid_tbl_len); 244 + kfree(device->pkey_tbl_len); 245 + out: 246 + kfree(tprops); 247 + return ret; 248 + } 249 + 223 250 /** 224 251 * ib_register_device - Register an IB device with IB core 225 252 * @device:Device to register ··· 290 239 spin_lock_init(&device->event_handler_lock); 291 240 spin_lock_init(&device->client_data_lock); 292 241 242 + ret = read_port_table_lengths(device); 243 + if (ret) { 244 + printk(KERN_WARNING "Couldn't create table lengths cache for device %s\n", 245 + device->name); 246 + goto out; 247 + } 248 + 293 249 ret = ib_device_register_sysfs(device); 294 250 if (ret) { 295 251 printk(KERN_WARNING "Couldn't register device %s with driver model\n", 296 252 device->name); 253 + kfree(device->gid_tbl_len); 254 + kfree(device->pkey_tbl_len); 297 255 goto out; 298 256 } 299 257 ··· 343 283 client->remove(device); 344 284 345 285 list_del(&device->core_list); 286 + 287 + kfree(device->gid_tbl_len); 288 + kfree(device->pkey_tbl_len); 346 289 347 290 mutex_unlock(&device_mutex); 348 291 ··· 654 591 port_modify); 655 592 } 656 593 EXPORT_SYMBOL(ib_modify_port); 594 + 595 + /** 596 + * ib_find_gid - Returns the port number and GID table index where 597 + * a specified GID value occurs. 598 + * @device: The device to query. 599 + * @gid: The GID value to search for. 600 + * @port_num: The port number of the device where the GID value was found. 601 + * @index: The index into the GID table where the GID was found. This 602 + * parameter may be NULL. 603 + */ 604 + int ib_find_gid(struct ib_device *device, union ib_gid *gid, 605 + u8 *port_num, u16 *index) 606 + { 607 + union ib_gid tmp_gid; 608 + int ret, port, i; 609 + 610 + for (port = start_port(device); port <= end_port(device); ++port) { 611 + for (i = 0; i < device->gid_tbl_len[port - start_port(device)]; ++i) { 612 + ret = ib_query_gid(device, port, i, &tmp_gid); 613 + if (ret) 614 + return ret; 615 + if (!memcmp(&tmp_gid, gid, sizeof *gid)) { 616 + *port_num = port; 617 + if (index) 618 + *index = i; 619 + return 0; 620 + } 621 + } 622 + } 623 + 624 + return -ENOENT; 625 + } 626 + EXPORT_SYMBOL(ib_find_gid); 627 + 628 + /** 629 + * ib_find_pkey - Returns the PKey table index where a specified 630 + * PKey value occurs. 631 + * @device: The device to query. 632 + * @port_num: The port number of the device to search for the PKey. 633 + * @pkey: The PKey value to search for. 634 + * @index: The index into the PKey table where the PKey was found. 635 + */ 636 + int ib_find_pkey(struct ib_device *device, 637 + u8 port_num, u16 pkey, u16 *index) 638 + { 639 + int ret, i; 640 + u16 tmp_pkey; 641 + 642 + for (i = 0; i < device->pkey_tbl_len[port_num - start_port(device)]; ++i) { 643 + ret = ib_query_pkey(device, port_num, i, &tmp_pkey); 644 + if (ret) 645 + return ret; 646 + 647 + if (pkey == tmp_pkey) { 648 + *index = i; 649 + return 0; 650 + } 651 + } 652 + 653 + return -ENOENT; 654 + } 655 + EXPORT_SYMBOL(ib_find_pkey); 657 656 658 657 static int __init ib_core_init(void) 659 658 {
+8
include/rdma/ib_verbs.h
··· 890 890 spinlock_t client_data_lock; 891 891 892 892 struct ib_cache cache; 893 + int *pkey_tbl_len; 894 + int *gid_tbl_len; 893 895 894 896 u32 flags; 895 897 ··· 1119 1117 int ib_modify_port(struct ib_device *device, 1120 1118 u8 port_num, int port_modify_mask, 1121 1119 struct ib_port_modify *port_modify); 1120 + 1121 + int ib_find_gid(struct ib_device *device, union ib_gid *gid, 1122 + u8 *port_num, u16 *index); 1123 + 1124 + int ib_find_pkey(struct ib_device *device, 1125 + u8 port_num, u16 pkey, u16 *index); 1122 1126 1123 1127 /** 1124 1128 * ib_alloc_pd - Allocates an unused protection domain.