| .. | .. |
|---|
| 55 | 55 | |
|---|
| 56 | 56 | #include <rdma/ib_mad.h> |
|---|
| 57 | 57 | #include <rdma/ib_user_mad.h> |
|---|
| 58 | +#include <rdma/rdma_netlink.h> |
|---|
| 58 | 59 | |
|---|
| 59 | 60 | #include "core_priv.h" |
|---|
| 60 | 61 | |
|---|
| .. | .. |
|---|
| 89 | 90 | |
|---|
| 90 | 91 | struct ib_umad_port { |
|---|
| 91 | 92 | struct cdev cdev; |
|---|
| 92 | | - struct device *dev; |
|---|
| 93 | | - |
|---|
| 93 | + struct device dev; |
|---|
| 94 | 94 | struct cdev sm_cdev; |
|---|
| 95 | | - struct device *sm_dev; |
|---|
| 95 | + struct device sm_dev; |
|---|
| 96 | 96 | struct semaphore sm_sem; |
|---|
| 97 | 97 | |
|---|
| 98 | 98 | struct mutex file_mutex; |
|---|
| .. | .. |
|---|
| 105 | 105 | }; |
|---|
| 106 | 106 | |
|---|
| 107 | 107 | struct ib_umad_device { |
|---|
| 108 | | - struct kobject kobj; |
|---|
| 109 | | - struct ib_umad_port port[0]; |
|---|
| 108 | + struct kref kref; |
|---|
| 109 | + struct ib_umad_port ports[]; |
|---|
| 110 | 110 | }; |
|---|
| 111 | 111 | |
|---|
| 112 | 112 | struct ib_umad_file { |
|---|
| .. | .. |
|---|
| 131 | 131 | struct ib_user_mad mad; |
|---|
| 132 | 132 | }; |
|---|
| 133 | 133 | |
|---|
| 134 | | -static struct class *umad_class; |
|---|
| 134 | +#define CREATE_TRACE_POINTS |
|---|
| 135 | +#include <trace/events/ib_umad.h> |
|---|
| 135 | 136 | |
|---|
| 136 | 137 | static const dev_t base_umad_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE); |
|---|
| 137 | 138 | static const dev_t base_issm_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE) + |
|---|
| .. | .. |
|---|
| 139 | 140 | static dev_t dynamic_umad_dev; |
|---|
| 140 | 141 | static dev_t dynamic_issm_dev; |
|---|
| 141 | 142 | |
|---|
| 142 | | -static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS); |
|---|
| 143 | +static DEFINE_IDA(umad_ida); |
|---|
| 143 | 144 | |
|---|
| 144 | | -static void ib_umad_add_one(struct ib_device *device); |
|---|
| 145 | +static int ib_umad_add_one(struct ib_device *device); |
|---|
| 145 | 146 | static void ib_umad_remove_one(struct ib_device *device, void *client_data); |
|---|
| 146 | 147 | |
|---|
| 147 | | -static void ib_umad_release_dev(struct kobject *kobj) |
|---|
| 148 | +static void ib_umad_dev_free(struct kref *kref) |
|---|
| 148 | 149 | { |
|---|
| 149 | 150 | struct ib_umad_device *dev = |
|---|
| 150 | | - container_of(kobj, struct ib_umad_device, kobj); |
|---|
| 151 | + container_of(kref, struct ib_umad_device, kref); |
|---|
| 151 | 152 | |
|---|
| 152 | 153 | kfree(dev); |
|---|
| 153 | 154 | } |
|---|
| 154 | 155 | |
|---|
| 155 | | -static struct kobj_type ib_umad_dev_ktype = { |
|---|
| 156 | | - .release = ib_umad_release_dev, |
|---|
| 157 | | -}; |
|---|
| 156 | +static void ib_umad_dev_get(struct ib_umad_device *dev) |
|---|
| 157 | +{ |
|---|
| 158 | + kref_get(&dev->kref); |
|---|
| 159 | +} |
|---|
| 160 | + |
|---|
| 161 | +static void ib_umad_dev_put(struct ib_umad_device *dev) |
|---|
| 162 | +{ |
|---|
| 163 | + kref_put(&dev->kref, ib_umad_dev_free); |
|---|
| 164 | +} |
|---|
| 158 | 165 | |
|---|
| 159 | 166 | static int hdr_size(struct ib_umad_file *file) |
|---|
| 160 | 167 | { |
|---|
| .. | .. |
|---|
| 206 | 213 | struct ib_umad_packet *packet = send_wc->send_buf->context[0]; |
|---|
| 207 | 214 | |
|---|
| 208 | 215 | dequeue_send(file, packet); |
|---|
| 209 | | - rdma_destroy_ah(packet->msg->ah); |
|---|
| 216 | + rdma_destroy_ah(packet->msg->ah, RDMA_DESTROY_AH_SLEEPABLE); |
|---|
| 210 | 217 | ib_free_send_mad(packet->msg); |
|---|
| 211 | 218 | |
|---|
| 212 | 219 | if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) { |
|---|
| .. | .. |
|---|
| 332 | 339 | return -EFAULT; |
|---|
| 333 | 340 | } |
|---|
| 334 | 341 | } |
|---|
| 342 | + |
|---|
| 343 | + trace_ib_umad_read_recv(file, &packet->mad.hdr, &recv_buf->mad->mad_hdr); |
|---|
| 344 | + |
|---|
| 335 | 345 | return hdr_size(file) + packet->length; |
|---|
| 336 | 346 | } |
|---|
| 337 | 347 | |
|---|
| .. | .. |
|---|
| 350 | 360 | |
|---|
| 351 | 361 | if (copy_to_user(buf, packet->mad.data, packet->length)) |
|---|
| 352 | 362 | return -EFAULT; |
|---|
| 363 | + |
|---|
| 364 | + trace_ib_umad_read_send(file, &packet->mad.hdr, |
|---|
| 365 | + (struct ib_mad_hdr *)&packet->mad.data); |
|---|
| 353 | 366 | |
|---|
| 354 | 367 | return size; |
|---|
| 355 | 368 | } |
|---|
| .. | .. |
|---|
| 516 | 529 | |
|---|
| 517 | 530 | mutex_lock(&file->mutex); |
|---|
| 518 | 531 | |
|---|
| 532 | + trace_ib_umad_write(file, &packet->mad.hdr, |
|---|
| 533 | + (struct ib_mad_hdr *)&packet->mad.data); |
|---|
| 534 | + |
|---|
| 519 | 535 | agent = __get_agent(file, packet->mad.hdr.id); |
|---|
| 520 | 536 | if (!agent) { |
|---|
| 521 | 537 | ret = -EIO; |
|---|
| .. | .. |
|---|
| 632 | 648 | err_msg: |
|---|
| 633 | 649 | ib_free_send_mad(packet->msg); |
|---|
| 634 | 650 | err_ah: |
|---|
| 635 | | - rdma_destroy_ah(ah); |
|---|
| 651 | + rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE); |
|---|
| 636 | 652 | err_up: |
|---|
| 637 | 653 | mutex_unlock(&file->mutex); |
|---|
| 638 | 654 | err: |
|---|
| .. | .. |
|---|
| 672 | 688 | mutex_lock(&file->mutex); |
|---|
| 673 | 689 | |
|---|
| 674 | 690 | if (!file->port->ib_dev) { |
|---|
| 675 | | - dev_notice(file->port->dev, |
|---|
| 691 | + dev_notice(&file->port->dev, |
|---|
| 676 | 692 | "ib_umad_reg_agent: invalid device\n"); |
|---|
| 677 | 693 | ret = -EPIPE; |
|---|
| 678 | 694 | goto out; |
|---|
| .. | .. |
|---|
| 684 | 700 | } |
|---|
| 685 | 701 | |
|---|
| 686 | 702 | if (ureq.qpn != 0 && ureq.qpn != 1) { |
|---|
| 687 | | - dev_notice(file->port->dev, |
|---|
| 703 | + dev_notice(&file->port->dev, |
|---|
| 688 | 704 | "ib_umad_reg_agent: invalid QPN %d specified\n", |
|---|
| 689 | 705 | ureq.qpn); |
|---|
| 690 | 706 | ret = -EINVAL; |
|---|
| .. | .. |
|---|
| 695 | 711 | if (!__get_agent(file, agent_id)) |
|---|
| 696 | 712 | goto found; |
|---|
| 697 | 713 | |
|---|
| 698 | | - dev_notice(file->port->dev, |
|---|
| 714 | + dev_notice(&file->port->dev, |
|---|
| 699 | 715 | "ib_umad_reg_agent: Max Agents (%u) reached\n", |
|---|
| 700 | 716 | IB_UMAD_MAX_AGENTS); |
|---|
| 701 | 717 | ret = -ENOMEM; |
|---|
| .. | .. |
|---|
| 740 | 756 | if (!file->already_used) { |
|---|
| 741 | 757 | file->already_used = 1; |
|---|
| 742 | 758 | if (!file->use_pkey_index) { |
|---|
| 743 | | - dev_warn(file->port->dev, |
|---|
| 759 | + dev_warn(&file->port->dev, |
|---|
| 744 | 760 | "process %s did not enable P_Key index support.\n", |
|---|
| 745 | 761 | current->comm); |
|---|
| 746 | | - dev_warn(file->port->dev, |
|---|
| 747 | | - " Documentation/infiniband/user_mad.txt has info on the new ABI.\n"); |
|---|
| 762 | + dev_warn(&file->port->dev, |
|---|
| 763 | + " Documentation/infiniband/user_mad.rst has info on the new ABI.\n"); |
|---|
| 748 | 764 | } |
|---|
| 749 | 765 | } |
|---|
| 750 | 766 | |
|---|
| .. | .. |
|---|
| 774 | 790 | mutex_lock(&file->mutex); |
|---|
| 775 | 791 | |
|---|
| 776 | 792 | if (!file->port->ib_dev) { |
|---|
| 777 | | - dev_notice(file->port->dev, |
|---|
| 793 | + dev_notice(&file->port->dev, |
|---|
| 778 | 794 | "ib_umad_reg_agent2: invalid device\n"); |
|---|
| 779 | 795 | ret = -EPIPE; |
|---|
| 780 | 796 | goto out; |
|---|
| .. | .. |
|---|
| 786 | 802 | } |
|---|
| 787 | 803 | |
|---|
| 788 | 804 | if (ureq.qpn != 0 && ureq.qpn != 1) { |
|---|
| 789 | | - dev_notice(file->port->dev, |
|---|
| 805 | + dev_notice(&file->port->dev, |
|---|
| 790 | 806 | "ib_umad_reg_agent2: invalid QPN %d specified\n", |
|---|
| 791 | 807 | ureq.qpn); |
|---|
| 792 | 808 | ret = -EINVAL; |
|---|
| .. | .. |
|---|
| 794 | 810 | } |
|---|
| 795 | 811 | |
|---|
| 796 | 812 | if (ureq.flags & ~IB_USER_MAD_REG_FLAGS_CAP) { |
|---|
| 797 | | - dev_notice(file->port->dev, |
|---|
| 813 | + dev_notice(&file->port->dev, |
|---|
| 798 | 814 | "ib_umad_reg_agent2 failed: invalid registration flags specified 0x%x; supported 0x%x\n", |
|---|
| 799 | 815 | ureq.flags, IB_USER_MAD_REG_FLAGS_CAP); |
|---|
| 800 | 816 | ret = -EINVAL; |
|---|
| .. | .. |
|---|
| 811 | 827 | if (!__get_agent(file, agent_id)) |
|---|
| 812 | 828 | goto found; |
|---|
| 813 | 829 | |
|---|
| 814 | | - dev_notice(file->port->dev, |
|---|
| 830 | + dev_notice(&file->port->dev, |
|---|
| 815 | 831 | "ib_umad_reg_agent2: Max Agents (%u) reached\n", |
|---|
| 816 | 832 | IB_UMAD_MAX_AGENTS); |
|---|
| 817 | 833 | ret = -ENOMEM; |
|---|
| .. | .. |
|---|
| 823 | 839 | req.mgmt_class = ureq.mgmt_class; |
|---|
| 824 | 840 | req.mgmt_class_version = ureq.mgmt_class_version; |
|---|
| 825 | 841 | if (ureq.oui & 0xff000000) { |
|---|
| 826 | | - dev_notice(file->port->dev, |
|---|
| 842 | + dev_notice(&file->port->dev, |
|---|
| 827 | 843 | "ib_umad_reg_agent2 failed: oui invalid 0x%08x\n", |
|---|
| 828 | 844 | ureq.oui); |
|---|
| 829 | 845 | ret = -EINVAL; |
|---|
| .. | .. |
|---|
| 972 | 988 | { |
|---|
| 973 | 989 | struct ib_umad_port *port; |
|---|
| 974 | 990 | struct ib_umad_file *file; |
|---|
| 975 | | - int ret = -ENXIO; |
|---|
| 991 | + int ret = 0; |
|---|
| 976 | 992 | |
|---|
| 977 | 993 | port = container_of(inode->i_cdev, struct ib_umad_port, cdev); |
|---|
| 978 | 994 | |
|---|
| 979 | 995 | mutex_lock(&port->file_mutex); |
|---|
| 980 | 996 | |
|---|
| 981 | | - if (!port->ib_dev) |
|---|
| 997 | + if (!port->ib_dev) { |
|---|
| 998 | + ret = -ENXIO; |
|---|
| 982 | 999 | goto out; |
|---|
| 1000 | + } |
|---|
| 983 | 1001 | |
|---|
| 984 | | - ret = -ENOMEM; |
|---|
| 985 | | - file = kzalloc(sizeof *file, GFP_KERNEL); |
|---|
| 986 | | - if (!file) |
|---|
| 1002 | + if (!rdma_dev_access_netns(port->ib_dev, current->nsproxy->net_ns)) { |
|---|
| 1003 | + ret = -EPERM; |
|---|
| 987 | 1004 | goto out; |
|---|
| 1005 | + } |
|---|
| 1006 | + |
|---|
| 1007 | + file = kzalloc(sizeof(*file), GFP_KERNEL); |
|---|
| 1008 | + if (!file) { |
|---|
| 1009 | + ret = -ENOMEM; |
|---|
| 1010 | + goto out; |
|---|
| 1011 | + } |
|---|
| 988 | 1012 | |
|---|
| 989 | 1013 | mutex_init(&file->mutex); |
|---|
| 990 | 1014 | spin_lock_init(&file->send_lock); |
|---|
| .. | .. |
|---|
| 997 | 1021 | |
|---|
| 998 | 1022 | list_add_tail(&file->port_list, &port->file_list); |
|---|
| 999 | 1023 | |
|---|
| 1000 | | - ret = nonseekable_open(inode, filp); |
|---|
| 1001 | | - if (ret) { |
|---|
| 1002 | | - list_del(&file->port_list); |
|---|
| 1003 | | - kfree(file); |
|---|
| 1004 | | - goto out; |
|---|
| 1005 | | - } |
|---|
| 1006 | | - |
|---|
| 1007 | | - kobject_get(&port->umad_dev->kobj); |
|---|
| 1008 | | - |
|---|
| 1024 | + stream_open(inode, filp); |
|---|
| 1009 | 1025 | out: |
|---|
| 1010 | 1026 | mutex_unlock(&port->file_mutex); |
|---|
| 1011 | 1027 | return ret; |
|---|
| .. | .. |
|---|
| 1014 | 1030 | static int ib_umad_close(struct inode *inode, struct file *filp) |
|---|
| 1015 | 1031 | { |
|---|
| 1016 | 1032 | struct ib_umad_file *file = filp->private_data; |
|---|
| 1017 | | - struct ib_umad_device *dev = file->port->umad_dev; |
|---|
| 1018 | 1033 | struct ib_umad_packet *packet, *tmp; |
|---|
| 1019 | 1034 | int already_dead; |
|---|
| 1020 | 1035 | int i; |
|---|
| .. | .. |
|---|
| 1041 | 1056 | ib_unregister_mad_agent(file->agent[i]); |
|---|
| 1042 | 1057 | |
|---|
| 1043 | 1058 | mutex_unlock(&file->port->file_mutex); |
|---|
| 1044 | | - |
|---|
| 1059 | + mutex_destroy(&file->mutex); |
|---|
| 1045 | 1060 | kfree(file); |
|---|
| 1046 | | - kobject_put(&dev->kobj); |
|---|
| 1047 | | - |
|---|
| 1048 | 1061 | return 0; |
|---|
| 1049 | 1062 | } |
|---|
| 1050 | 1063 | |
|---|
| .. | .. |
|---|
| 1084 | 1097 | } |
|---|
| 1085 | 1098 | } |
|---|
| 1086 | 1099 | |
|---|
| 1100 | + if (!rdma_dev_access_netns(port->ib_dev, current->nsproxy->net_ns)) { |
|---|
| 1101 | + ret = -EPERM; |
|---|
| 1102 | + goto err_up_sem; |
|---|
| 1103 | + } |
|---|
| 1104 | + |
|---|
| 1087 | 1105 | ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); |
|---|
| 1088 | 1106 | if (ret) |
|---|
| 1089 | 1107 | goto err_up_sem; |
|---|
| 1090 | 1108 | |
|---|
| 1091 | 1109 | filp->private_data = port; |
|---|
| 1092 | 1110 | |
|---|
| 1093 | | - ret = nonseekable_open(inode, filp); |
|---|
| 1094 | | - if (ret) |
|---|
| 1095 | | - goto err_clr_sm_cap; |
|---|
| 1096 | | - |
|---|
| 1097 | | - kobject_get(&port->umad_dev->kobj); |
|---|
| 1098 | | - |
|---|
| 1111 | + nonseekable_open(inode, filp); |
|---|
| 1099 | 1112 | return 0; |
|---|
| 1100 | | - |
|---|
| 1101 | | -err_clr_sm_cap: |
|---|
| 1102 | | - swap(props.set_port_cap_mask, props.clr_port_cap_mask); |
|---|
| 1103 | | - ib_modify_port(port->ib_dev, port->port_num, 0, &props); |
|---|
| 1104 | 1113 | |
|---|
| 1105 | 1114 | err_up_sem: |
|---|
| 1106 | 1115 | up(&port->sm_sem); |
|---|
| .. | .. |
|---|
| 1124 | 1133 | |
|---|
| 1125 | 1134 | up(&port->sm_sem); |
|---|
| 1126 | 1135 | |
|---|
| 1127 | | - kobject_put(&port->umad_dev->kobj); |
|---|
| 1128 | | - |
|---|
| 1129 | 1136 | return ret; |
|---|
| 1130 | 1137 | } |
|---|
| 1131 | 1138 | |
|---|
| .. | .. |
|---|
| 1136 | 1143 | .llseek = no_llseek, |
|---|
| 1137 | 1144 | }; |
|---|
| 1138 | 1145 | |
|---|
| 1146 | +static struct ib_umad_port *get_port(struct ib_device *ibdev, |
|---|
| 1147 | + struct ib_umad_device *umad_dev, |
|---|
| 1148 | + unsigned int port) |
|---|
| 1149 | +{ |
|---|
| 1150 | + if (!umad_dev) |
|---|
| 1151 | + return ERR_PTR(-EOPNOTSUPP); |
|---|
| 1152 | + if (!rdma_is_port_valid(ibdev, port)) |
|---|
| 1153 | + return ERR_PTR(-EINVAL); |
|---|
| 1154 | + if (!rdma_cap_ib_mad(ibdev, port)) |
|---|
| 1155 | + return ERR_PTR(-EOPNOTSUPP); |
|---|
| 1156 | + |
|---|
| 1157 | + return &umad_dev->ports[port - rdma_start_port(ibdev)]; |
|---|
| 1158 | +} |
|---|
| 1159 | + |
|---|
| 1160 | +static int ib_umad_get_nl_info(struct ib_device *ibdev, void *client_data, |
|---|
| 1161 | + struct ib_client_nl_info *res) |
|---|
| 1162 | +{ |
|---|
| 1163 | + struct ib_umad_port *port = get_port(ibdev, client_data, res->port); |
|---|
| 1164 | + |
|---|
| 1165 | + if (IS_ERR(port)) |
|---|
| 1166 | + return PTR_ERR(port); |
|---|
| 1167 | + |
|---|
| 1168 | + res->abi = IB_USER_MAD_ABI_VERSION; |
|---|
| 1169 | + res->cdev = &port->dev; |
|---|
| 1170 | + return 0; |
|---|
| 1171 | +} |
|---|
| 1172 | + |
|---|
| 1139 | 1173 | static struct ib_client umad_client = { |
|---|
| 1140 | 1174 | .name = "umad", |
|---|
| 1141 | 1175 | .add = ib_umad_add_one, |
|---|
| 1142 | | - .remove = ib_umad_remove_one |
|---|
| 1176 | + .remove = ib_umad_remove_one, |
|---|
| 1177 | + .get_nl_info = ib_umad_get_nl_info, |
|---|
| 1143 | 1178 | }; |
|---|
| 1179 | +MODULE_ALIAS_RDMA_CLIENT("umad"); |
|---|
| 1144 | 1180 | |
|---|
| 1145 | | -static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr, |
|---|
| 1181 | +static int ib_issm_get_nl_info(struct ib_device *ibdev, void *client_data, |
|---|
| 1182 | + struct ib_client_nl_info *res) |
|---|
| 1183 | +{ |
|---|
| 1184 | + struct ib_umad_port *port = get_port(ibdev, client_data, res->port); |
|---|
| 1185 | + |
|---|
| 1186 | + if (IS_ERR(port)) |
|---|
| 1187 | + return PTR_ERR(port); |
|---|
| 1188 | + |
|---|
| 1189 | + res->abi = IB_USER_MAD_ABI_VERSION; |
|---|
| 1190 | + res->cdev = &port->sm_dev; |
|---|
| 1191 | + return 0; |
|---|
| 1192 | +} |
|---|
| 1193 | + |
|---|
| 1194 | +static struct ib_client issm_client = { |
|---|
| 1195 | + .name = "issm", |
|---|
| 1196 | + .get_nl_info = ib_issm_get_nl_info, |
|---|
| 1197 | +}; |
|---|
| 1198 | +MODULE_ALIAS_RDMA_CLIENT("issm"); |
|---|
| 1199 | + |
|---|
| 1200 | +static ssize_t ibdev_show(struct device *dev, struct device_attribute *attr, |
|---|
| 1146 | 1201 | char *buf) |
|---|
| 1147 | 1202 | { |
|---|
| 1148 | 1203 | struct ib_umad_port *port = dev_get_drvdata(dev); |
|---|
| .. | .. |
|---|
| 1150 | 1205 | if (!port) |
|---|
| 1151 | 1206 | return -ENODEV; |
|---|
| 1152 | 1207 | |
|---|
| 1153 | | - return sprintf(buf, "%s\n", port->ib_dev->name); |
|---|
| 1208 | + return sprintf(buf, "%s\n", dev_name(&port->ib_dev->dev)); |
|---|
| 1154 | 1209 | } |
|---|
| 1155 | | -static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); |
|---|
| 1210 | +static DEVICE_ATTR_RO(ibdev); |
|---|
| 1156 | 1211 | |
|---|
| 1157 | | -static ssize_t show_port(struct device *dev, struct device_attribute *attr, |
|---|
| 1212 | +static ssize_t port_show(struct device *dev, struct device_attribute *attr, |
|---|
| 1158 | 1213 | char *buf) |
|---|
| 1159 | 1214 | { |
|---|
| 1160 | 1215 | struct ib_umad_port *port = dev_get_drvdata(dev); |
|---|
| .. | .. |
|---|
| 1164 | 1219 | |
|---|
| 1165 | 1220 | return sprintf(buf, "%d\n", port->port_num); |
|---|
| 1166 | 1221 | } |
|---|
| 1167 | | -static DEVICE_ATTR(port, S_IRUGO, show_port, NULL); |
|---|
| 1222 | +static DEVICE_ATTR_RO(port); |
|---|
| 1168 | 1223 | |
|---|
| 1169 | | -static CLASS_ATTR_STRING(abi_version, S_IRUGO, |
|---|
| 1170 | | - __stringify(IB_USER_MAD_ABI_VERSION)); |
|---|
| 1224 | +static struct attribute *umad_class_dev_attrs[] = { |
|---|
| 1225 | + &dev_attr_ibdev.attr, |
|---|
| 1226 | + &dev_attr_port.attr, |
|---|
| 1227 | + NULL, |
|---|
| 1228 | +}; |
|---|
| 1229 | +ATTRIBUTE_GROUPS(umad_class_dev); |
|---|
| 1230 | + |
|---|
| 1231 | +static char *umad_devnode(struct device *dev, umode_t *mode) |
|---|
| 1232 | +{ |
|---|
| 1233 | + return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev)); |
|---|
| 1234 | +} |
|---|
| 1235 | + |
|---|
| 1236 | +static ssize_t abi_version_show(struct class *class, |
|---|
| 1237 | + struct class_attribute *attr, char *buf) |
|---|
| 1238 | +{ |
|---|
| 1239 | + return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION); |
|---|
| 1240 | +} |
|---|
| 1241 | +static CLASS_ATTR_RO(abi_version); |
|---|
| 1242 | + |
|---|
| 1243 | +static struct attribute *umad_class_attrs[] = { |
|---|
| 1244 | + &class_attr_abi_version.attr, |
|---|
| 1245 | + NULL, |
|---|
| 1246 | +}; |
|---|
| 1247 | +ATTRIBUTE_GROUPS(umad_class); |
|---|
| 1248 | + |
|---|
| 1249 | +static struct class umad_class = { |
|---|
| 1250 | + .name = "infiniband_mad", |
|---|
| 1251 | + .devnode = umad_devnode, |
|---|
| 1252 | + .class_groups = umad_class_groups, |
|---|
| 1253 | + .dev_groups = umad_class_dev_groups, |
|---|
| 1254 | +}; |
|---|
| 1255 | + |
|---|
| 1256 | +static void ib_umad_release_port(struct device *device) |
|---|
| 1257 | +{ |
|---|
| 1258 | + struct ib_umad_port *port = dev_get_drvdata(device); |
|---|
| 1259 | + struct ib_umad_device *umad_dev = port->umad_dev; |
|---|
| 1260 | + |
|---|
| 1261 | + ib_umad_dev_put(umad_dev); |
|---|
| 1262 | +} |
|---|
| 1263 | + |
|---|
| 1264 | +static void ib_umad_init_port_dev(struct device *dev, |
|---|
| 1265 | + struct ib_umad_port *port, |
|---|
| 1266 | + const struct ib_device *device) |
|---|
| 1267 | +{ |
|---|
| 1268 | + device_initialize(dev); |
|---|
| 1269 | + ib_umad_dev_get(port->umad_dev); |
|---|
| 1270 | + dev->class = &umad_class; |
|---|
| 1271 | + dev->parent = device->dev.parent; |
|---|
| 1272 | + dev_set_drvdata(dev, port); |
|---|
| 1273 | + dev->release = ib_umad_release_port; |
|---|
| 1274 | +} |
|---|
| 1171 | 1275 | |
|---|
| 1172 | 1276 | static int ib_umad_init_port(struct ib_device *device, int port_num, |
|---|
| 1173 | 1277 | struct ib_umad_device *umad_dev, |
|---|
| .. | .. |
|---|
| 1176 | 1280 | int devnum; |
|---|
| 1177 | 1281 | dev_t base_umad; |
|---|
| 1178 | 1282 | dev_t base_issm; |
|---|
| 1283 | + int ret; |
|---|
| 1179 | 1284 | |
|---|
| 1180 | | - devnum = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS); |
|---|
| 1181 | | - if (devnum >= IB_UMAD_MAX_PORTS) |
|---|
| 1285 | + devnum = ida_alloc_max(&umad_ida, IB_UMAD_MAX_PORTS - 1, GFP_KERNEL); |
|---|
| 1286 | + if (devnum < 0) |
|---|
| 1182 | 1287 | return -1; |
|---|
| 1183 | 1288 | port->dev_num = devnum; |
|---|
| 1184 | | - set_bit(devnum, dev_map); |
|---|
| 1185 | 1289 | if (devnum >= IB_UMAD_NUM_FIXED_MINOR) { |
|---|
| 1186 | 1290 | base_umad = dynamic_umad_dev + devnum - IB_UMAD_NUM_FIXED_MINOR; |
|---|
| 1187 | 1291 | base_issm = dynamic_issm_dev + devnum - IB_UMAD_NUM_FIXED_MINOR; |
|---|
| .. | .. |
|---|
| 1191 | 1295 | } |
|---|
| 1192 | 1296 | |
|---|
| 1193 | 1297 | port->ib_dev = device; |
|---|
| 1298 | + port->umad_dev = umad_dev; |
|---|
| 1194 | 1299 | port->port_num = port_num; |
|---|
| 1195 | 1300 | sema_init(&port->sm_sem, 1); |
|---|
| 1196 | 1301 | mutex_init(&port->file_mutex); |
|---|
| 1197 | 1302 | INIT_LIST_HEAD(&port->file_list); |
|---|
| 1198 | 1303 | |
|---|
| 1304 | + ib_umad_init_port_dev(&port->dev, port, device); |
|---|
| 1305 | + port->dev.devt = base_umad; |
|---|
| 1306 | + dev_set_name(&port->dev, "umad%d", port->dev_num); |
|---|
| 1199 | 1307 | cdev_init(&port->cdev, &umad_fops); |
|---|
| 1200 | 1308 | port->cdev.owner = THIS_MODULE; |
|---|
| 1201 | | - cdev_set_parent(&port->cdev, &umad_dev->kobj); |
|---|
| 1202 | | - kobject_set_name(&port->cdev.kobj, "umad%d", port->dev_num); |
|---|
| 1203 | | - if (cdev_add(&port->cdev, base_umad, 1)) |
|---|
| 1309 | + |
|---|
| 1310 | + ret = cdev_device_add(&port->cdev, &port->dev); |
|---|
| 1311 | + if (ret) |
|---|
| 1204 | 1312 | goto err_cdev; |
|---|
| 1205 | 1313 | |
|---|
| 1206 | | - port->dev = device_create(umad_class, device->dev.parent, |
|---|
| 1207 | | - port->cdev.dev, port, |
|---|
| 1208 | | - "umad%d", port->dev_num); |
|---|
| 1209 | | - if (IS_ERR(port->dev)) |
|---|
| 1210 | | - goto err_cdev; |
|---|
| 1211 | | - |
|---|
| 1212 | | - if (device_create_file(port->dev, &dev_attr_ibdev)) |
|---|
| 1213 | | - goto err_dev; |
|---|
| 1214 | | - if (device_create_file(port->dev, &dev_attr_port)) |
|---|
| 1215 | | - goto err_dev; |
|---|
| 1216 | | - |
|---|
| 1314 | + ib_umad_init_port_dev(&port->sm_dev, port, device); |
|---|
| 1315 | + port->sm_dev.devt = base_issm; |
|---|
| 1316 | + dev_set_name(&port->sm_dev, "issm%d", port->dev_num); |
|---|
| 1217 | 1317 | cdev_init(&port->sm_cdev, &umad_sm_fops); |
|---|
| 1218 | 1318 | port->sm_cdev.owner = THIS_MODULE; |
|---|
| 1219 | | - cdev_set_parent(&port->sm_cdev, &umad_dev->kobj); |
|---|
| 1220 | | - kobject_set_name(&port->sm_cdev.kobj, "issm%d", port->dev_num); |
|---|
| 1221 | | - if (cdev_add(&port->sm_cdev, base_issm, 1)) |
|---|
| 1222 | | - goto err_sm_cdev; |
|---|
| 1223 | 1319 | |
|---|
| 1224 | | - port->sm_dev = device_create(umad_class, device->dev.parent, |
|---|
| 1225 | | - port->sm_cdev.dev, port, |
|---|
| 1226 | | - "issm%d", port->dev_num); |
|---|
| 1227 | | - if (IS_ERR(port->sm_dev)) |
|---|
| 1228 | | - goto err_sm_cdev; |
|---|
| 1229 | | - |
|---|
| 1230 | | - if (device_create_file(port->sm_dev, &dev_attr_ibdev)) |
|---|
| 1231 | | - goto err_sm_dev; |
|---|
| 1232 | | - if (device_create_file(port->sm_dev, &dev_attr_port)) |
|---|
| 1233 | | - goto err_sm_dev; |
|---|
| 1320 | + ret = cdev_device_add(&port->sm_cdev, &port->sm_dev); |
|---|
| 1321 | + if (ret) |
|---|
| 1322 | + goto err_dev; |
|---|
| 1234 | 1323 | |
|---|
| 1235 | 1324 | return 0; |
|---|
| 1236 | 1325 | |
|---|
| 1237 | | -err_sm_dev: |
|---|
| 1238 | | - device_destroy(umad_class, port->sm_cdev.dev); |
|---|
| 1239 | | - |
|---|
| 1240 | | -err_sm_cdev: |
|---|
| 1241 | | - cdev_del(&port->sm_cdev); |
|---|
| 1242 | | - |
|---|
| 1243 | 1326 | err_dev: |
|---|
| 1244 | | - device_destroy(umad_class, port->cdev.dev); |
|---|
| 1245 | | - |
|---|
| 1327 | + put_device(&port->sm_dev); |
|---|
| 1328 | + cdev_device_del(&port->cdev, &port->dev); |
|---|
| 1246 | 1329 | err_cdev: |
|---|
| 1247 | | - cdev_del(&port->cdev); |
|---|
| 1248 | | - clear_bit(devnum, dev_map); |
|---|
| 1249 | | - |
|---|
| 1250 | | - return -1; |
|---|
| 1330 | + put_device(&port->dev); |
|---|
| 1331 | + ida_free(&umad_ida, devnum); |
|---|
| 1332 | + return ret; |
|---|
| 1251 | 1333 | } |
|---|
| 1252 | 1334 | |
|---|
| 1253 | 1335 | static void ib_umad_kill_port(struct ib_umad_port *port) |
|---|
| .. | .. |
|---|
| 1255 | 1337 | struct ib_umad_file *file; |
|---|
| 1256 | 1338 | int id; |
|---|
| 1257 | 1339 | |
|---|
| 1258 | | - dev_set_drvdata(port->dev, NULL); |
|---|
| 1259 | | - dev_set_drvdata(port->sm_dev, NULL); |
|---|
| 1260 | | - |
|---|
| 1261 | | - device_destroy(umad_class, port->cdev.dev); |
|---|
| 1262 | | - device_destroy(umad_class, port->sm_cdev.dev); |
|---|
| 1263 | | - |
|---|
| 1264 | | - cdev_del(&port->cdev); |
|---|
| 1265 | | - cdev_del(&port->sm_cdev); |
|---|
| 1340 | + cdev_device_del(&port->sm_cdev, &port->sm_dev); |
|---|
| 1341 | + cdev_device_del(&port->cdev, &port->dev); |
|---|
| 1266 | 1342 | |
|---|
| 1267 | 1343 | mutex_lock(&port->file_mutex); |
|---|
| 1268 | 1344 | |
|---|
| 1345 | + /* Mark ib_dev NULL and block ioctl or other file ops to progress |
|---|
| 1346 | + * further. |
|---|
| 1347 | + */ |
|---|
| 1269 | 1348 | port->ib_dev = NULL; |
|---|
| 1270 | 1349 | |
|---|
| 1271 | 1350 | list_for_each_entry(file, &port->file_list, port_list) { |
|---|
| .. | .. |
|---|
| 1280 | 1359 | } |
|---|
| 1281 | 1360 | |
|---|
| 1282 | 1361 | mutex_unlock(&port->file_mutex); |
|---|
| 1283 | | - clear_bit(port->dev_num, dev_map); |
|---|
| 1362 | + |
|---|
| 1363 | + ida_free(&umad_ida, port->dev_num); |
|---|
| 1364 | + |
|---|
| 1365 | + /* balances device_initialize() */ |
|---|
| 1366 | + put_device(&port->sm_dev); |
|---|
| 1367 | + put_device(&port->dev); |
|---|
| 1284 | 1368 | } |
|---|
| 1285 | 1369 | |
|---|
| 1286 | | -static void ib_umad_add_one(struct ib_device *device) |
|---|
| 1370 | +static int ib_umad_add_one(struct ib_device *device) |
|---|
| 1287 | 1371 | { |
|---|
| 1288 | 1372 | struct ib_umad_device *umad_dev; |
|---|
| 1289 | 1373 | int s, e, i; |
|---|
| 1290 | 1374 | int count = 0; |
|---|
| 1375 | + int ret; |
|---|
| 1291 | 1376 | |
|---|
| 1292 | 1377 | s = rdma_start_port(device); |
|---|
| 1293 | 1378 | e = rdma_end_port(device); |
|---|
| 1294 | 1379 | |
|---|
| 1295 | | - umad_dev = kzalloc(sizeof *umad_dev + |
|---|
| 1296 | | - (e - s + 1) * sizeof (struct ib_umad_port), |
|---|
| 1297 | | - GFP_KERNEL); |
|---|
| 1380 | + umad_dev = kzalloc(struct_size(umad_dev, ports, e - s + 1), GFP_KERNEL); |
|---|
| 1298 | 1381 | if (!umad_dev) |
|---|
| 1299 | | - return; |
|---|
| 1382 | + return -ENOMEM; |
|---|
| 1300 | 1383 | |
|---|
| 1301 | | - kobject_init(&umad_dev->kobj, &ib_umad_dev_ktype); |
|---|
| 1302 | | - |
|---|
| 1384 | + kref_init(&umad_dev->kref); |
|---|
| 1303 | 1385 | for (i = s; i <= e; ++i) { |
|---|
| 1304 | 1386 | if (!rdma_cap_ib_mad(device, i)) |
|---|
| 1305 | 1387 | continue; |
|---|
| 1306 | 1388 | |
|---|
| 1307 | | - umad_dev->port[i - s].umad_dev = umad_dev; |
|---|
| 1308 | | - |
|---|
| 1309 | | - if (ib_umad_init_port(device, i, umad_dev, |
|---|
| 1310 | | - &umad_dev->port[i - s])) |
|---|
| 1389 | + ret = ib_umad_init_port(device, i, umad_dev, |
|---|
| 1390 | + &umad_dev->ports[i - s]); |
|---|
| 1391 | + if (ret) |
|---|
| 1311 | 1392 | goto err; |
|---|
| 1312 | 1393 | |
|---|
| 1313 | 1394 | count++; |
|---|
| 1314 | 1395 | } |
|---|
| 1315 | 1396 | |
|---|
| 1316 | | - if (!count) |
|---|
| 1397 | + if (!count) { |
|---|
| 1398 | + ret = -EOPNOTSUPP; |
|---|
| 1317 | 1399 | goto free; |
|---|
| 1400 | + } |
|---|
| 1318 | 1401 | |
|---|
| 1319 | 1402 | ib_set_client_data(device, &umad_client, umad_dev); |
|---|
| 1320 | 1403 | |
|---|
| 1321 | | - return; |
|---|
| 1404 | + return 0; |
|---|
| 1322 | 1405 | |
|---|
| 1323 | 1406 | err: |
|---|
| 1324 | 1407 | while (--i >= s) { |
|---|
| 1325 | 1408 | if (!rdma_cap_ib_mad(device, i)) |
|---|
| 1326 | 1409 | continue; |
|---|
| 1327 | 1410 | |
|---|
| 1328 | | - ib_umad_kill_port(&umad_dev->port[i - s]); |
|---|
| 1411 | + ib_umad_kill_port(&umad_dev->ports[i - s]); |
|---|
| 1329 | 1412 | } |
|---|
| 1330 | 1413 | free: |
|---|
| 1331 | | - kobject_put(&umad_dev->kobj); |
|---|
| 1414 | + /* balances kref_init */ |
|---|
| 1415 | + ib_umad_dev_put(umad_dev); |
|---|
| 1416 | + return ret; |
|---|
| 1332 | 1417 | } |
|---|
| 1333 | 1418 | |
|---|
| 1334 | 1419 | static void ib_umad_remove_one(struct ib_device *device, void *client_data) |
|---|
| 1335 | 1420 | { |
|---|
| 1336 | 1421 | struct ib_umad_device *umad_dev = client_data; |
|---|
| 1337 | | - int i; |
|---|
| 1422 | + unsigned int i; |
|---|
| 1338 | 1423 | |
|---|
| 1339 | | - if (!umad_dev) |
|---|
| 1340 | | - return; |
|---|
| 1341 | | - |
|---|
| 1342 | | - for (i = 0; i <= rdma_end_port(device) - rdma_start_port(device); ++i) { |
|---|
| 1343 | | - if (rdma_cap_ib_mad(device, i + rdma_start_port(device))) |
|---|
| 1344 | | - ib_umad_kill_port(&umad_dev->port[i]); |
|---|
| 1424 | + rdma_for_each_port (device, i) { |
|---|
| 1425 | + if (rdma_cap_ib_mad(device, i)) |
|---|
| 1426 | + ib_umad_kill_port( |
|---|
| 1427 | + &umad_dev->ports[i - rdma_start_port(device)]); |
|---|
| 1345 | 1428 | } |
|---|
| 1346 | | - |
|---|
| 1347 | | - kobject_put(&umad_dev->kobj); |
|---|
| 1348 | | -} |
|---|
| 1349 | | - |
|---|
| 1350 | | -static char *umad_devnode(struct device *dev, umode_t *mode) |
|---|
| 1351 | | -{ |
|---|
| 1352 | | - return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev)); |
|---|
| 1429 | + /* balances kref_init() */ |
|---|
| 1430 | + ib_umad_dev_put(umad_dev); |
|---|
| 1353 | 1431 | } |
|---|
| 1354 | 1432 | |
|---|
| 1355 | 1433 | static int __init ib_umad_init(void) |
|---|
| .. | .. |
|---|
| 1358 | 1436 | |
|---|
| 1359 | 1437 | ret = register_chrdev_region(base_umad_dev, |
|---|
| 1360 | 1438 | IB_UMAD_NUM_FIXED_MINOR * 2, |
|---|
| 1361 | | - "infiniband_mad"); |
|---|
| 1439 | + umad_class.name); |
|---|
| 1362 | 1440 | if (ret) { |
|---|
| 1363 | 1441 | pr_err("couldn't register device number\n"); |
|---|
| 1364 | 1442 | goto out; |
|---|
| .. | .. |
|---|
| 1366 | 1444 | |
|---|
| 1367 | 1445 | ret = alloc_chrdev_region(&dynamic_umad_dev, 0, |
|---|
| 1368 | 1446 | IB_UMAD_NUM_DYNAMIC_MINOR * 2, |
|---|
| 1369 | | - "infiniband_mad"); |
|---|
| 1447 | + umad_class.name); |
|---|
| 1370 | 1448 | if (ret) { |
|---|
| 1371 | 1449 | pr_err("couldn't register dynamic device number\n"); |
|---|
| 1372 | 1450 | goto out_alloc; |
|---|
| 1373 | 1451 | } |
|---|
| 1374 | 1452 | dynamic_issm_dev = dynamic_umad_dev + IB_UMAD_NUM_DYNAMIC_MINOR; |
|---|
| 1375 | 1453 | |
|---|
| 1376 | | - umad_class = class_create(THIS_MODULE, "infiniband_mad"); |
|---|
| 1377 | | - if (IS_ERR(umad_class)) { |
|---|
| 1378 | | - ret = PTR_ERR(umad_class); |
|---|
| 1454 | + ret = class_register(&umad_class); |
|---|
| 1455 | + if (ret) { |
|---|
| 1379 | 1456 | pr_err("couldn't create class infiniband_mad\n"); |
|---|
| 1380 | 1457 | goto out_chrdev; |
|---|
| 1381 | 1458 | } |
|---|
| 1382 | 1459 | |
|---|
| 1383 | | - umad_class->devnode = umad_devnode; |
|---|
| 1384 | | - |
|---|
| 1385 | | - ret = class_create_file(umad_class, &class_attr_abi_version.attr); |
|---|
| 1386 | | - if (ret) { |
|---|
| 1387 | | - pr_err("couldn't create abi_version attribute\n"); |
|---|
| 1388 | | - goto out_class; |
|---|
| 1389 | | - } |
|---|
| 1390 | | - |
|---|
| 1391 | 1460 | ret = ib_register_client(&umad_client); |
|---|
| 1392 | | - if (ret) { |
|---|
| 1393 | | - pr_err("couldn't register ib_umad client\n"); |
|---|
| 1461 | + if (ret) |
|---|
| 1394 | 1462 | goto out_class; |
|---|
| 1395 | | - } |
|---|
| 1463 | + |
|---|
| 1464 | + ret = ib_register_client(&issm_client); |
|---|
| 1465 | + if (ret) |
|---|
| 1466 | + goto out_client; |
|---|
| 1396 | 1467 | |
|---|
| 1397 | 1468 | return 0; |
|---|
| 1398 | 1469 | |
|---|
| 1470 | +out_client: |
|---|
| 1471 | + ib_unregister_client(&umad_client); |
|---|
| 1399 | 1472 | out_class: |
|---|
| 1400 | | - class_destroy(umad_class); |
|---|
| 1473 | + class_unregister(&umad_class); |
|---|
| 1401 | 1474 | |
|---|
| 1402 | 1475 | out_chrdev: |
|---|
| 1403 | 1476 | unregister_chrdev_region(dynamic_umad_dev, |
|---|
| .. | .. |
|---|
| 1413 | 1486 | |
|---|
| 1414 | 1487 | static void __exit ib_umad_cleanup(void) |
|---|
| 1415 | 1488 | { |
|---|
| 1489 | + ib_unregister_client(&issm_client); |
|---|
| 1416 | 1490 | ib_unregister_client(&umad_client); |
|---|
| 1417 | | - class_destroy(umad_class); |
|---|
| 1491 | + class_unregister(&umad_class); |
|---|
| 1418 | 1492 | unregister_chrdev_region(base_umad_dev, |
|---|
| 1419 | 1493 | IB_UMAD_NUM_FIXED_MINOR * 2); |
|---|
| 1420 | 1494 | unregister_chrdev_region(dynamic_umad_dev, |
|---|