Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2004 Topspin Communications. All rights reserved. |
Roland Dreier | 3cd9656 | 2006-09-22 15:22:46 -0700 | [diff] [blame] | 3 | * Copyright (c) 2005 Voltaire, Inc. All rights reserved. |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 4 | * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This software is available to you under a choice of one of two |
| 7 | * licenses. You may choose to be licensed under the terms of the GNU |
| 8 | * General Public License (GPL) Version 2, available from the file |
| 9 | * COPYING in the main directory of this source tree, or the |
| 10 | * OpenIB.org BSD license below: |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or |
| 13 | * without modification, are permitted provided that the following |
| 14 | * conditions are met: |
| 15 | * |
| 16 | * - Redistributions of source code must retain the above |
| 17 | * copyright notice, this list of conditions and the following |
| 18 | * disclaimer. |
| 19 | * |
| 20 | * - Redistributions in binary form must reproduce the above |
| 21 | * copyright notice, this list of conditions and the following |
| 22 | * disclaimer in the documentation and/or other materials |
| 23 | * provided with the distribution. |
| 24 | * |
| 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 26 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 28 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 29 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 30 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 31 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 32 | * SOFTWARE. |
| 33 | * |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 34 | * $Id: user_mad.c 5596 2006-03-03 01:00:07Z sean.hefty $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | #include <linux/module.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/device.h> |
| 40 | #include <linux/err.h> |
| 41 | #include <linux/fs.h> |
| 42 | #include <linux/cdev.h> |
| 43 | #include <linux/pci.h> |
| 44 | #include <linux/dma-mapping.h> |
| 45 | #include <linux/poll.h> |
| 46 | #include <linux/rwsem.h> |
| 47 | #include <linux/kref.h> |
| 48 | |
| 49 | #include <asm/uaccess.h> |
| 50 | #include <asm/semaphore.h> |
| 51 | |
Roland Dreier | a4d61e8 | 2005-08-25 13:40:04 -0700 | [diff] [blame] | 52 | #include <rdma/ib_mad.h> |
| 53 | #include <rdma/ib_user_mad.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | MODULE_AUTHOR("Roland Dreier"); |
| 56 | MODULE_DESCRIPTION("InfiniBand userspace MAD packet access"); |
| 57 | MODULE_LICENSE("Dual BSD/GPL"); |
| 58 | |
| 59 | enum { |
| 60 | IB_UMAD_MAX_PORTS = 64, |
| 61 | IB_UMAD_MAX_AGENTS = 32, |
| 62 | |
| 63 | IB_UMAD_MAJOR = 231, |
| 64 | IB_UMAD_MINOR_BASE = 0 |
| 65 | }; |
| 66 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 67 | /* |
| 68 | * Our lifetime rules for these structs are the following: each time a |
| 69 | * device special file is opened, we look up the corresponding struct |
| 70 | * ib_umad_port by minor in the umad_port[] table while holding the |
| 71 | * port_lock. If this lookup succeeds, we take a reference on the |
| 72 | * ib_umad_port's struct ib_umad_device while still holding the |
| 73 | * port_lock; if the lookup fails, we fail the open(). We drop these |
| 74 | * references in the corresponding close(). |
| 75 | * |
| 76 | * In addition to references coming from open character devices, there |
| 77 | * is one more reference to each ib_umad_device representing the |
| 78 | * module's reference taken when allocating the ib_umad_device in |
| 79 | * ib_umad_add_one(). |
| 80 | * |
| 81 | * When destroying an ib_umad_device, we clear all of its |
| 82 | * ib_umad_ports from umad_port[] while holding port_lock before |
| 83 | * dropping the module's reference to the ib_umad_device. This is |
| 84 | * always safe because any open() calls will either succeed and obtain |
| 85 | * a reference before we clear the umad_port[] entries, or fail after |
| 86 | * we clear the umad_port[] entries. |
| 87 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 89 | struct ib_umad_port { |
| 90 | struct cdev *dev; |
| 91 | struct class_device *class_dev; |
| 92 | |
| 93 | struct cdev *sm_dev; |
| 94 | struct class_device *sm_class_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | struct semaphore sm_sem; |
| 96 | |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 97 | struct rw_semaphore mutex; |
| 98 | struct list_head file_list; |
| 99 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | struct ib_device *ib_dev; |
| 101 | struct ib_umad_device *umad_dev; |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 102 | int dev_num; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | u8 port_num; |
| 104 | }; |
| 105 | |
| 106 | struct ib_umad_device { |
| 107 | int start_port, end_port; |
| 108 | struct kref ref; |
| 109 | struct ib_umad_port port[0]; |
| 110 | }; |
| 111 | |
| 112 | struct ib_umad_file { |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 113 | struct ib_umad_port *port; |
| 114 | struct list_head recv_list; |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 115 | struct list_head send_list; |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 116 | struct list_head port_list; |
| 117 | spinlock_t recv_lock; |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 118 | spinlock_t send_lock; |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 119 | wait_queue_head_t recv_wait; |
| 120 | struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS]; |
| 121 | int agents_dead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | struct ib_umad_packet { |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 125 | struct ib_mad_send_buf *msg; |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 126 | struct ib_mad_recv_wc *recv_wc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | struct list_head list; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 128 | int length; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 129 | struct ib_user_mad mad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 132 | static struct class *umad_class; |
| 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE); |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 135 | |
| 136 | static DEFINE_SPINLOCK(port_lock); |
| 137 | static struct ib_umad_port *umad_port[IB_UMAD_MAX_PORTS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS * 2); |
| 139 | |
| 140 | static void ib_umad_add_one(struct ib_device *device); |
| 141 | static void ib_umad_remove_one(struct ib_device *device); |
| 142 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 143 | static void ib_umad_release_dev(struct kref *ref) |
| 144 | { |
| 145 | struct ib_umad_device *dev = |
| 146 | container_of(ref, struct ib_umad_device, ref); |
| 147 | |
| 148 | kfree(dev); |
| 149 | } |
| 150 | |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 151 | /* caller must hold port->mutex at least for reading */ |
| 152 | static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id) |
| 153 | { |
| 154 | return file->agents_dead ? NULL : file->agent[id]; |
| 155 | } |
| 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | static int queue_packet(struct ib_umad_file *file, |
| 158 | struct ib_mad_agent *agent, |
| 159 | struct ib_umad_packet *packet) |
| 160 | { |
| 161 | int ret = 1; |
| 162 | |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 163 | down_read(&file->port->mutex); |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 164 | |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 165 | for (packet->mad.hdr.id = 0; |
| 166 | packet->mad.hdr.id < IB_UMAD_MAX_AGENTS; |
| 167 | packet->mad.hdr.id++) |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 168 | if (agent == __get_agent(file, packet->mad.hdr.id)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | spin_lock_irq(&file->recv_lock); |
| 170 | list_add_tail(&packet->list, &file->recv_list); |
| 171 | spin_unlock_irq(&file->recv_lock); |
| 172 | wake_up_interruptible(&file->recv_wait); |
| 173 | ret = 0; |
| 174 | break; |
| 175 | } |
| 176 | |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 177 | up_read(&file->port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 182 | static void dequeue_send(struct ib_umad_file *file, |
| 183 | struct ib_umad_packet *packet) |
| 184 | { |
| 185 | spin_lock_irq(&file->send_lock); |
| 186 | list_del(&packet->list); |
| 187 | spin_unlock_irq(&file->send_lock); |
| 188 | } |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | static void send_handler(struct ib_mad_agent *agent, |
| 191 | struct ib_mad_send_wc *send_wc) |
| 192 | { |
| 193 | struct ib_umad_file *file = agent->context; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 194 | struct ib_umad_packet *packet = send_wc->send_buf->context[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 196 | dequeue_send(file, packet); |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 197 | ib_destroy_ah(packet->msg->ah); |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 198 | ib_free_send_mad(packet->msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) { |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 201 | packet->length = IB_MGMT_MAD_HDR; |
| 202 | packet->mad.hdr.status = ETIMEDOUT; |
| 203 | if (!queue_packet(file, agent, packet)) |
| 204 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | kfree(packet); |
| 207 | } |
| 208 | |
| 209 | static void recv_handler(struct ib_mad_agent *agent, |
| 210 | struct ib_mad_recv_wc *mad_recv_wc) |
| 211 | { |
| 212 | struct ib_umad_file *file = agent->context; |
| 213 | struct ib_umad_packet *packet; |
| 214 | |
| 215 | if (mad_recv_wc->wc->status != IB_WC_SUCCESS) |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 216 | goto err1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 218 | packet = kzalloc(sizeof *packet, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | if (!packet) |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 220 | goto err1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 222 | packet->length = mad_recv_wc->mad_len; |
| 223 | packet->recv_wc = mad_recv_wc; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 224 | |
| 225 | packet->mad.hdr.status = 0; |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 226 | packet->mad.hdr.length = sizeof (struct ib_user_mad) + |
| 227 | mad_recv_wc->mad_len; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 228 | packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp); |
| 229 | packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid); |
| 230 | packet->mad.hdr.sl = mad_recv_wc->wc->sl; |
| 231 | packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits; |
| 232 | packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH); |
| 233 | if (packet->mad.hdr.grh_present) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | /* XXX parse GRH */ |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 235 | packet->mad.hdr.gid_index = 0; |
| 236 | packet->mad.hdr.hop_limit = 0; |
| 237 | packet->mad.hdr.traffic_class = 0; |
| 238 | memset(packet->mad.hdr.gid, 0, 16); |
| 239 | packet->mad.hdr.flow_label = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | if (queue_packet(file, agent, packet)) |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 243 | goto err2; |
| 244 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 246 | err2: |
| 247 | kfree(packet); |
| 248 | err1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | ib_free_recv_mad(mad_recv_wc); |
| 250 | } |
| 251 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 252 | static ssize_t copy_recv_mad(char __user *buf, struct ib_umad_packet *packet, |
| 253 | size_t count) |
| 254 | { |
| 255 | struct ib_mad_recv_buf *recv_buf; |
| 256 | int left, seg_payload, offset, max_seg_payload; |
| 257 | |
| 258 | /* We need enough room to copy the first (or only) MAD segment. */ |
| 259 | recv_buf = &packet->recv_wc->recv_buf; |
| 260 | if ((packet->length <= sizeof (*recv_buf->mad) && |
| 261 | count < sizeof (packet->mad) + packet->length) || |
| 262 | (packet->length > sizeof (*recv_buf->mad) && |
| 263 | count < sizeof (packet->mad) + sizeof (*recv_buf->mad))) |
| 264 | return -EINVAL; |
| 265 | |
| 266 | if (copy_to_user(buf, &packet->mad, sizeof (packet->mad))) |
| 267 | return -EFAULT; |
| 268 | |
| 269 | buf += sizeof (packet->mad); |
| 270 | seg_payload = min_t(int, packet->length, sizeof (*recv_buf->mad)); |
| 271 | if (copy_to_user(buf, recv_buf->mad, seg_payload)) |
| 272 | return -EFAULT; |
| 273 | |
| 274 | if (seg_payload < packet->length) { |
| 275 | /* |
| 276 | * Multipacket RMPP MAD message. Copy remainder of message. |
| 277 | * Note that last segment may have a shorter payload. |
| 278 | */ |
| 279 | if (count < sizeof (packet->mad) + packet->length) { |
| 280 | /* |
| 281 | * The buffer is too small, return the first RMPP segment, |
| 282 | * which includes the RMPP message length. |
| 283 | */ |
| 284 | return -ENOSPC; |
| 285 | } |
Hal Rosenstock | 618a3c0 | 2006-03-28 16:40:04 -0800 | [diff] [blame] | 286 | offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class); |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 287 | max_seg_payload = sizeof (struct ib_mad) - offset; |
| 288 | |
| 289 | for (left = packet->length - seg_payload, buf += seg_payload; |
| 290 | left; left -= seg_payload, buf += seg_payload) { |
| 291 | recv_buf = container_of(recv_buf->list.next, |
| 292 | struct ib_mad_recv_buf, list); |
| 293 | seg_payload = min(left, max_seg_payload); |
| 294 | if (copy_to_user(buf, ((void *) recv_buf->mad) + offset, |
| 295 | seg_payload)) |
| 296 | return -EFAULT; |
| 297 | } |
| 298 | } |
| 299 | return sizeof (packet->mad) + packet->length; |
| 300 | } |
| 301 | |
| 302 | static ssize_t copy_send_mad(char __user *buf, struct ib_umad_packet *packet, |
| 303 | size_t count) |
| 304 | { |
| 305 | ssize_t size = sizeof (packet->mad) + packet->length; |
| 306 | |
| 307 | if (count < size) |
| 308 | return -EINVAL; |
| 309 | |
| 310 | if (copy_to_user(buf, &packet->mad, size)) |
| 311 | return -EFAULT; |
| 312 | |
| 313 | return size; |
| 314 | } |
| 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | static ssize_t ib_umad_read(struct file *filp, char __user *buf, |
| 317 | size_t count, loff_t *pos) |
| 318 | { |
| 319 | struct ib_umad_file *file = filp->private_data; |
| 320 | struct ib_umad_packet *packet; |
| 321 | ssize_t ret; |
| 322 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 323 | if (count < sizeof (struct ib_user_mad)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | return -EINVAL; |
| 325 | |
| 326 | spin_lock_irq(&file->recv_lock); |
| 327 | |
| 328 | while (list_empty(&file->recv_list)) { |
| 329 | spin_unlock_irq(&file->recv_lock); |
| 330 | |
| 331 | if (filp->f_flags & O_NONBLOCK) |
| 332 | return -EAGAIN; |
| 333 | |
| 334 | if (wait_event_interruptible(file->recv_wait, |
| 335 | !list_empty(&file->recv_list))) |
| 336 | return -ERESTARTSYS; |
| 337 | |
| 338 | spin_lock_irq(&file->recv_lock); |
| 339 | } |
| 340 | |
| 341 | packet = list_entry(file->recv_list.next, struct ib_umad_packet, list); |
| 342 | list_del(&packet->list); |
| 343 | |
| 344 | spin_unlock_irq(&file->recv_lock); |
| 345 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 346 | if (packet->recv_wc) |
| 347 | ret = copy_recv_mad(buf, packet, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | else |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 349 | ret = copy_send_mad(buf, packet, count); |
| 350 | |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 351 | if (ret < 0) { |
| 352 | /* Requeue packet */ |
| 353 | spin_lock_irq(&file->recv_lock); |
| 354 | list_add(&packet->list, &file->recv_list); |
| 355 | spin_unlock_irq(&file->recv_lock); |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 356 | } else { |
| 357 | if (packet->recv_wc) |
| 358 | ib_free_recv_mad(packet->recv_wc); |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 359 | kfree(packet); |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | return ret; |
| 362 | } |
| 363 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 364 | static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf) |
| 365 | { |
| 366 | int left, seg; |
| 367 | |
| 368 | /* Copy class specific header */ |
| 369 | if ((msg->hdr_len > IB_MGMT_RMPP_HDR) && |
| 370 | copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR, |
| 371 | msg->hdr_len - IB_MGMT_RMPP_HDR)) |
| 372 | return -EFAULT; |
| 373 | |
| 374 | /* All headers are in place. Copy data segments. */ |
| 375 | for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0; |
| 376 | seg++, left -= msg->seg_size, buf += msg->seg_size) { |
| 377 | if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf, |
| 378 | min(left, msg->seg_size))) |
| 379 | return -EFAULT; |
| 380 | } |
| 381 | return 0; |
| 382 | } |
| 383 | |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 384 | static int same_destination(struct ib_user_mad_hdr *hdr1, |
| 385 | struct ib_user_mad_hdr *hdr2) |
| 386 | { |
| 387 | if (!hdr1->grh_present && !hdr2->grh_present) |
| 388 | return (hdr1->lid == hdr2->lid); |
| 389 | |
| 390 | if (hdr1->grh_present && hdr2->grh_present) |
| 391 | return !memcmp(hdr1->gid, hdr2->gid, 16); |
| 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | static int is_duplicate(struct ib_umad_file *file, |
| 397 | struct ib_umad_packet *packet) |
| 398 | { |
| 399 | struct ib_umad_packet *sent_packet; |
| 400 | struct ib_mad_hdr *sent_hdr, *hdr; |
| 401 | |
| 402 | hdr = (struct ib_mad_hdr *) packet->mad.data; |
| 403 | list_for_each_entry(sent_packet, &file->send_list, list) { |
| 404 | sent_hdr = (struct ib_mad_hdr *) sent_packet->mad.data; |
| 405 | |
| 406 | if ((hdr->tid != sent_hdr->tid) || |
| 407 | (hdr->mgmt_class != sent_hdr->mgmt_class)) |
| 408 | continue; |
| 409 | |
| 410 | /* |
| 411 | * No need to be overly clever here. If two new operations have |
| 412 | * the same TID, reject the second as a duplicate. This is more |
| 413 | * restrictive than required by the spec. |
| 414 | */ |
| 415 | if (!ib_response_mad((struct ib_mad *) hdr)) { |
| 416 | if (!ib_response_mad((struct ib_mad *) sent_hdr)) |
| 417 | return 1; |
| 418 | continue; |
| 419 | } else if (!ib_response_mad((struct ib_mad *) sent_hdr)) |
| 420 | continue; |
| 421 | |
| 422 | if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr)) |
| 423 | return 1; |
| 424 | } |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | static ssize_t ib_umad_write(struct file *filp, const char __user *buf, |
| 430 | size_t count, loff_t *pos) |
| 431 | { |
| 432 | struct ib_umad_file *file = filp->private_data; |
| 433 | struct ib_umad_packet *packet; |
| 434 | struct ib_mad_agent *agent; |
| 435 | struct ib_ah_attr ah_attr; |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 436 | struct ib_ah *ah; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 437 | struct ib_rmpp_mad *rmpp_mad; |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 438 | __be64 *tid; |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 439 | int ret, data_len, hdr_len, copy_offset, rmpp_active; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
Roland Dreier | eabc779 | 2005-11-18 14:18:26 -0800 | [diff] [blame] | 441 | if (count < sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | return -EINVAL; |
| 443 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 444 | packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | if (!packet) |
| 446 | return -ENOMEM; |
| 447 | |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 448 | if (copy_from_user(&packet->mad, buf, |
Sean Hefty | cb0f091 | 2005-10-27 20:48:11 -0700 | [diff] [blame] | 449 | sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)) { |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 450 | ret = -EFAULT; |
| 451 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 454 | if (packet->mad.hdr.id < 0 || |
| 455 | packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | ret = -EINVAL; |
| 457 | goto err; |
| 458 | } |
| 459 | |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 460 | down_read(&file->port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 462 | agent = __get_agent(file, packet->mad.hdr.id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | if (!agent) { |
| 464 | ret = -EINVAL; |
| 465 | goto err_up; |
| 466 | } |
| 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | memset(&ah_attr, 0, sizeof ah_attr); |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 469 | ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid); |
| 470 | ah_attr.sl = packet->mad.hdr.sl; |
| 471 | ah_attr.src_path_bits = packet->mad.hdr.path_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | ah_attr.port_num = file->port->port_num; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 473 | if (packet->mad.hdr.grh_present) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | ah_attr.ah_flags = IB_AH_GRH; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 475 | memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16); |
Sean Hefty | 97f52eb | 2005-08-13 21:05:57 -0700 | [diff] [blame] | 476 | ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label); |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 477 | ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit; |
| 478 | ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 481 | ah = ib_create_ah(agent->qp->pd, &ah_attr); |
| 482 | if (IS_ERR(ah)) { |
| 483 | ret = PTR_ERR(ah); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | goto err_up; |
| 485 | } |
| 486 | |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 487 | rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data; |
Hal Rosenstock | 618a3c0 | 2006-03-28 16:40:04 -0800 | [diff] [blame] | 488 | hdr_len = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class); |
| 489 | if (!ib_is_mad_class_rmpp(rmpp_mad->mad_hdr.mgmt_class)) { |
Sean Hefty | cb0f091 | 2005-10-27 20:48:11 -0700 | [diff] [blame] | 490 | copy_offset = IB_MGMT_MAD_HDR; |
Michael S. Tsirkin | bf6d9e2 | 2005-11-28 13:07:20 -0800 | [diff] [blame] | 491 | rmpp_active = 0; |
Hal Rosenstock | 618a3c0 | 2006-03-28 16:40:04 -0800 | [diff] [blame] | 492 | } else { |
| 493 | copy_offset = IB_MGMT_RMPP_HDR; |
| 494 | rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & |
| 495 | IB_MGMT_RMPP_FLAG_ACTIVE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 498 | data_len = count - sizeof (struct ib_user_mad) - hdr_len; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 499 | packet->msg = ib_create_send_mad(agent, |
| 500 | be32_to_cpu(packet->mad.hdr.qpn), |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 501 | 0, rmpp_active, hdr_len, |
| 502 | data_len, GFP_KERNEL); |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 503 | if (IS_ERR(packet->msg)) { |
| 504 | ret = PTR_ERR(packet->msg); |
| 505 | goto err_ah; |
| 506 | } |
| 507 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 508 | packet->msg->ah = ah; |
| 509 | packet->msg->timeout_ms = packet->mad.hdr.timeout_ms; |
| 510 | packet->msg->retries = packet->mad.hdr.retries; |
| 511 | packet->msg->context[0] = packet; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 512 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 513 | /* Copy MAD header. Any RMPP header is already in place. */ |
Sean Hefty | cb0f091 | 2005-10-27 20:48:11 -0700 | [diff] [blame] | 514 | memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR); |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 515 | buf += sizeof (struct ib_user_mad); |
| 516 | |
| 517 | if (!rmpp_active) { |
| 518 | if (copy_from_user(packet->msg->mad + copy_offset, |
| 519 | buf + copy_offset, |
| 520 | hdr_len + data_len - copy_offset)) { |
| 521 | ret = -EFAULT; |
| 522 | goto err_msg; |
| 523 | } |
| 524 | } else { |
| 525 | ret = copy_rmpp_mad(packet->msg, buf); |
| 526 | if (ret) |
| 527 | goto err_msg; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 531 | * Set the high-order part of the transaction ID to make MADs from |
| 532 | * different agents unique, and allow routing responses back to the |
| 533 | * original requestor. |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 534 | */ |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 535 | if (!ib_response_mad(packet->msg->mad)) { |
Roland Dreier | 089a1be | 2005-10-27 20:33:43 -0700 | [diff] [blame] | 536 | tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 537 | *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 | |
| 538 | (be64_to_cpup(tid) & 0xffffffff)); |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 539 | rmpp_mad->mad_hdr.tid = *tid; |
| 540 | } |
| 541 | |
| 542 | spin_lock_irq(&file->send_lock); |
| 543 | ret = is_duplicate(file, packet); |
| 544 | if (!ret) |
| 545 | list_add_tail(&packet->list, &file->send_list); |
| 546 | spin_unlock_irq(&file->send_lock); |
| 547 | if (ret) { |
| 548 | ret = -EINVAL; |
| 549 | goto err_msg; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 552 | ret = ib_post_send_mad(packet->msg, NULL); |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 553 | if (ret) |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 554 | goto err_send; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 555 | |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 556 | up_read(&file->port->mutex); |
Sean Hefty | cb0f091 | 2005-10-27 20:48:11 -0700 | [diff] [blame] | 557 | return count; |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 558 | |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 559 | err_send: |
| 560 | dequeue_send(file, packet); |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 561 | err_msg: |
| 562 | ib_free_send_mad(packet->msg); |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 563 | err_ah: |
Sean Hefty | 34816ad | 2005-10-25 10:51:39 -0700 | [diff] [blame] | 564 | ib_destroy_ah(ah); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | err_up: |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 566 | up_read(&file->port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | err: |
| 568 | kfree(packet); |
| 569 | return ret; |
| 570 | } |
| 571 | |
| 572 | static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait) |
| 573 | { |
| 574 | struct ib_umad_file *file = filp->private_data; |
| 575 | |
| 576 | /* we will always be able to post a MAD send */ |
| 577 | unsigned int mask = POLLOUT | POLLWRNORM; |
| 578 | |
| 579 | poll_wait(filp, &file->recv_wait, wait); |
| 580 | |
| 581 | if (!list_empty(&file->recv_list)) |
| 582 | mask |= POLLIN | POLLRDNORM; |
| 583 | |
| 584 | return mask; |
| 585 | } |
| 586 | |
| 587 | static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg) |
| 588 | { |
| 589 | struct ib_user_mad_reg_req ureq; |
| 590 | struct ib_mad_reg_req req; |
| 591 | struct ib_mad_agent *agent; |
| 592 | int agent_id; |
| 593 | int ret; |
| 594 | |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 595 | down_write(&file->port->mutex); |
| 596 | |
| 597 | if (!file->port->ib_dev) { |
| 598 | ret = -EPIPE; |
| 599 | goto out; |
| 600 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
| 602 | if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) { |
| 603 | ret = -EFAULT; |
| 604 | goto out; |
| 605 | } |
| 606 | |
| 607 | if (ureq.qpn != 0 && ureq.qpn != 1) { |
| 608 | ret = -EINVAL; |
| 609 | goto out; |
| 610 | } |
| 611 | |
| 612 | for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id) |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 613 | if (!__get_agent(file, agent_id)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | goto found; |
| 615 | |
| 616 | ret = -ENOMEM; |
| 617 | goto out; |
| 618 | |
| 619 | found: |
Roland Dreier | 0df3bb1 | 2005-04-16 15:26:11 -0700 | [diff] [blame] | 620 | if (ureq.mgmt_class) { |
| 621 | req.mgmt_class = ureq.mgmt_class; |
| 622 | req.mgmt_class_version = ureq.mgmt_class_version; |
| 623 | memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask); |
| 624 | memcpy(req.oui, ureq.oui, sizeof req.oui); |
| 625 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
| 627 | agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num, |
| 628 | ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI, |
Roland Dreier | 0df3bb1 | 2005-04-16 15:26:11 -0700 | [diff] [blame] | 629 | ureq.mgmt_class ? &req : NULL, |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 630 | ureq.rmpp_version, |
| 631 | send_handler, recv_handler, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | if (IS_ERR(agent)) { |
| 633 | ret = PTR_ERR(agent); |
| 634 | goto out; |
| 635 | } |
| 636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | if (put_user(agent_id, |
| 638 | (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) { |
| 639 | ret = -EFAULT; |
Roland Dreier | ec914c5 | 2005-11-09 09:58:10 -0800 | [diff] [blame] | 640 | ib_unregister_mad_agent(agent); |
| 641 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Roland Dreier | 2f76e82 | 2005-11-07 10:41:29 -0800 | [diff] [blame] | 644 | file->agent[agent_id] = agent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | ret = 0; |
Roland Dreier | 2f76e82 | 2005-11-07 10:41:29 -0800 | [diff] [blame] | 646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | out: |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 648 | up_write(&file->port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | return ret; |
| 650 | } |
| 651 | |
| 652 | static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg) |
| 653 | { |
Roland Dreier | 2f76e82 | 2005-11-07 10:41:29 -0800 | [diff] [blame] | 654 | struct ib_mad_agent *agent = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | u32 id; |
| 656 | int ret = 0; |
| 657 | |
Roland Dreier | 2f76e82 | 2005-11-07 10:41:29 -0800 | [diff] [blame] | 658 | if (get_user(id, (u32 __user *) arg)) |
| 659 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Roland Dreier | 2f76e82 | 2005-11-07 10:41:29 -0800 | [diff] [blame] | 661 | down_write(&file->port->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 663 | if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | ret = -EINVAL; |
| 665 | goto out; |
| 666 | } |
| 667 | |
Roland Dreier | 2f76e82 | 2005-11-07 10:41:29 -0800 | [diff] [blame] | 668 | agent = file->agent[id]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | file->agent[id] = NULL; |
| 670 | |
| 671 | out: |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 672 | up_write(&file->port->mutex); |
Roland Dreier | 2f76e82 | 2005-11-07 10:41:29 -0800 | [diff] [blame] | 673 | |
Roland Dreier | ec914c5 | 2005-11-09 09:58:10 -0800 | [diff] [blame] | 674 | if (agent) |
Roland Dreier | 2f76e82 | 2005-11-07 10:41:29 -0800 | [diff] [blame] | 675 | ib_unregister_mad_agent(agent); |
Roland Dreier | 2f76e82 | 2005-11-07 10:41:29 -0800 | [diff] [blame] | 676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | return ret; |
| 678 | } |
| 679 | |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 680 | static long ib_umad_ioctl(struct file *filp, unsigned int cmd, |
| 681 | unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | { |
| 683 | switch (cmd) { |
| 684 | case IB_USER_MAD_REGISTER_AGENT: |
| 685 | return ib_umad_reg_agent(filp->private_data, arg); |
| 686 | case IB_USER_MAD_UNREGISTER_AGENT: |
| 687 | return ib_umad_unreg_agent(filp->private_data, arg); |
| 688 | default: |
| 689 | return -ENOIOCTLCMD; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | static int ib_umad_open(struct inode *inode, struct file *filp) |
| 694 | { |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 695 | struct ib_umad_port *port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | struct ib_umad_file *file; |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 697 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 699 | spin_lock(&port_lock); |
| 700 | port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE]; |
| 701 | if (port) |
| 702 | kref_get(&port->umad_dev->ref); |
| 703 | spin_unlock(&port_lock); |
| 704 | |
| 705 | if (!port) |
| 706 | return -ENXIO; |
| 707 | |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 708 | down_write(&port->mutex); |
| 709 | |
| 710 | if (!port->ib_dev) { |
| 711 | ret = -ENXIO; |
| 712 | goto out; |
| 713 | } |
| 714 | |
Sean Hefty | cb0f091 | 2005-10-27 20:48:11 -0700 | [diff] [blame] | 715 | file = kzalloc(sizeof *file, GFP_KERNEL); |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 716 | if (!file) { |
| 717 | kref_put(&port->umad_dev->ref, ib_umad_release_dev); |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 718 | ret = -ENOMEM; |
| 719 | goto out; |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 720 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | spin_lock_init(&file->recv_lock); |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 723 | spin_lock_init(&file->send_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | INIT_LIST_HEAD(&file->recv_list); |
Sean Hefty | 2527e68 | 2006-07-20 11:25:50 +0300 | [diff] [blame] | 725 | INIT_LIST_HEAD(&file->send_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | init_waitqueue_head(&file->recv_wait); |
| 727 | |
| 728 | file->port = port; |
| 729 | filp->private_data = file; |
| 730 | |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 731 | list_add_tail(&file->port_list, &port->file_list); |
| 732 | |
| 733 | out: |
| 734 | up_write(&port->mutex); |
| 735 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | static int ib_umad_close(struct inode *inode, struct file *filp) |
| 739 | { |
| 740 | struct ib_umad_file *file = filp->private_data; |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 741 | struct ib_umad_device *dev = file->port->umad_dev; |
Roland Dreier | 561e148 | 2005-05-25 12:31:30 -0700 | [diff] [blame] | 742 | struct ib_umad_packet *packet, *tmp; |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 743 | int already_dead; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | int i; |
| 745 | |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 746 | down_write(&file->port->mutex); |
| 747 | |
| 748 | already_dead = file->agents_dead; |
| 749 | file->agents_dead = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 751 | list_for_each_entry_safe(packet, tmp, &file->recv_list, list) { |
| 752 | if (packet->recv_wc) |
| 753 | ib_free_recv_mad(packet->recv_wc); |
Roland Dreier | 561e148 | 2005-05-25 12:31:30 -0700 | [diff] [blame] | 754 | kfree(packet); |
Jack Morgenstein | f36e179 | 2006-03-03 21:54:13 -0800 | [diff] [blame] | 755 | } |
Roland Dreier | 561e148 | 2005-05-25 12:31:30 -0700 | [diff] [blame] | 756 | |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 757 | list_del(&file->port_list); |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 758 | |
| 759 | downgrade_write(&file->port->mutex); |
| 760 | |
| 761 | if (!already_dead) |
| 762 | for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i) |
| 763 | if (file->agent[i]) |
| 764 | ib_unregister_mad_agent(file->agent[i]); |
| 765 | |
| 766 | up_read(&file->port->mutex); |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | kfree(file); |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 769 | kref_put(&dev->ref, ib_umad_release_dev); |
| 770 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static struct file_operations umad_fops = { |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 775 | .owner = THIS_MODULE, |
| 776 | .read = ib_umad_read, |
| 777 | .write = ib_umad_write, |
| 778 | .poll = ib_umad_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | .unlocked_ioctl = ib_umad_ioctl, |
Hal Rosenstock | cb183a0 | 2005-07-27 11:45:42 -0700 | [diff] [blame] | 780 | .compat_ioctl = ib_umad_ioctl, |
| 781 | .open = ib_umad_open, |
| 782 | .release = ib_umad_close |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | }; |
| 784 | |
| 785 | static int ib_umad_sm_open(struct inode *inode, struct file *filp) |
| 786 | { |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 787 | struct ib_umad_port *port; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | struct ib_port_modify props = { |
| 789 | .set_port_cap_mask = IB_PORT_SM |
| 790 | }; |
| 791 | int ret; |
| 792 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 793 | spin_lock(&port_lock); |
| 794 | port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS]; |
| 795 | if (port) |
| 796 | kref_get(&port->umad_dev->ref); |
| 797 | spin_unlock(&port_lock); |
| 798 | |
| 799 | if (!port) |
| 800 | return -ENXIO; |
| 801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | if (filp->f_flags & O_NONBLOCK) { |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 803 | if (down_trylock(&port->sm_sem)) { |
| 804 | ret = -EAGAIN; |
| 805 | goto fail; |
| 806 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | } else { |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 808 | if (down_interruptible(&port->sm_sem)) { |
| 809 | ret = -ERESTARTSYS; |
| 810 | goto fail; |
| 811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); |
| 815 | if (ret) { |
| 816 | up(&port->sm_sem); |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 817 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | filp->private_data = port; |
| 821 | |
| 822 | return 0; |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 823 | |
| 824 | fail: |
| 825 | kref_put(&port->umad_dev->ref, ib_umad_release_dev); |
| 826 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | static int ib_umad_sm_close(struct inode *inode, struct file *filp) |
| 830 | { |
| 831 | struct ib_umad_port *port = filp->private_data; |
| 832 | struct ib_port_modify props = { |
| 833 | .clr_port_cap_mask = IB_PORT_SM |
| 834 | }; |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 835 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 837 | down_write(&port->mutex); |
| 838 | if (port->ib_dev) |
| 839 | ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); |
| 840 | up_write(&port->mutex); |
| 841 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | up(&port->sm_sem); |
| 843 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 844 | kref_put(&port->umad_dev->ref, ib_umad_release_dev); |
| 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | return ret; |
| 847 | } |
| 848 | |
| 849 | static struct file_operations umad_sm_fops = { |
| 850 | .owner = THIS_MODULE, |
| 851 | .open = ib_umad_sm_open, |
| 852 | .release = ib_umad_sm_close |
| 853 | }; |
| 854 | |
| 855 | static struct ib_client umad_client = { |
| 856 | .name = "umad", |
| 857 | .add = ib_umad_add_one, |
| 858 | .remove = ib_umad_remove_one |
| 859 | }; |
| 860 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | static ssize_t show_ibdev(struct class_device *class_dev, char *buf) |
| 862 | { |
| 863 | struct ib_umad_port *port = class_get_devdata(class_dev); |
| 864 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 865 | if (!port) |
| 866 | return -ENODEV; |
| 867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | return sprintf(buf, "%s\n", port->ib_dev->name); |
| 869 | } |
| 870 | static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); |
| 871 | |
| 872 | static ssize_t show_port(struct class_device *class_dev, char *buf) |
| 873 | { |
| 874 | struct ib_umad_port *port = class_get_devdata(class_dev); |
| 875 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 876 | if (!port) |
| 877 | return -ENODEV; |
| 878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | return sprintf(buf, "%d\n", port->port_num); |
| 880 | } |
| 881 | static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL); |
| 882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | static ssize_t show_abi_version(struct class *class, char *buf) |
| 884 | { |
| 885 | return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION); |
| 886 | } |
| 887 | static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL); |
| 888 | |
| 889 | static int ib_umad_init_port(struct ib_device *device, int port_num, |
| 890 | struct ib_umad_port *port) |
| 891 | { |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 892 | spin_lock(&port_lock); |
| 893 | port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS); |
| 894 | if (port->dev_num >= IB_UMAD_MAX_PORTS) { |
| 895 | spin_unlock(&port_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | return -1; |
| 897 | } |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 898 | set_bit(port->dev_num, dev_map); |
| 899 | spin_unlock(&port_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | |
| 901 | port->ib_dev = device; |
| 902 | port->port_num = port_num; |
| 903 | init_MUTEX(&port->sm_sem); |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 904 | init_rwsem(&port->mutex); |
| 905 | INIT_LIST_HEAD(&port->file_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 907 | port->dev = cdev_alloc(); |
| 908 | if (!port->dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | return -1; |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 910 | port->dev->owner = THIS_MODULE; |
| 911 | port->dev->ops = &umad_fops; |
| 912 | kobject_set_name(&port->dev->kobj, "umad%d", port->dev_num); |
| 913 | if (cdev_add(port->dev, base_dev + port->dev_num, 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | goto err_cdev; |
| 915 | |
Roland Dreier | 4cce339 | 2005-10-28 16:38:15 -0700 | [diff] [blame] | 916 | port->class_dev = class_device_create(umad_class, NULL, port->dev->dev, |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 917 | device->dma_device, |
| 918 | "umad%d", port->dev_num); |
| 919 | if (IS_ERR(port->class_dev)) |
| 920 | goto err_cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 922 | if (class_device_create_file(port->class_dev, &class_device_attr_ibdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | goto err_class; |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 924 | if (class_device_create_file(port->class_dev, &class_device_attr_port)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | goto err_class; |
| 926 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 927 | port->sm_dev = cdev_alloc(); |
| 928 | if (!port->sm_dev) |
| 929 | goto err_class; |
| 930 | port->sm_dev->owner = THIS_MODULE; |
| 931 | port->sm_dev->ops = &umad_sm_fops; |
Michael S. Tsirkin | 8b37b94 | 2005-11-06 15:47:02 -0800 | [diff] [blame] | 932 | kobject_set_name(&port->sm_dev->kobj, "issm%d", port->dev_num); |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 933 | if (cdev_add(port->sm_dev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | goto err_sm_cdev; |
| 935 | |
Roland Dreier | 4cce339 | 2005-10-28 16:38:15 -0700 | [diff] [blame] | 936 | port->sm_class_dev = class_device_create(umad_class, NULL, port->sm_dev->dev, |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 937 | device->dma_device, |
| 938 | "issm%d", port->dev_num); |
| 939 | if (IS_ERR(port->sm_class_dev)) |
| 940 | goto err_sm_cdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 942 | class_set_devdata(port->class_dev, port); |
| 943 | class_set_devdata(port->sm_class_dev, port); |
| 944 | |
| 945 | if (class_device_create_file(port->sm_class_dev, &class_device_attr_ibdev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | goto err_sm_class; |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 947 | if (class_device_create_file(port->sm_class_dev, &class_device_attr_port)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | goto err_sm_class; |
| 949 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 950 | spin_lock(&port_lock); |
| 951 | umad_port[port->dev_num] = port; |
| 952 | spin_unlock(&port_lock); |
| 953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | return 0; |
| 955 | |
| 956 | err_sm_class: |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 957 | class_device_destroy(umad_class, port->sm_dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
| 959 | err_sm_cdev: |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 960 | cdev_del(port->sm_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
| 962 | err_class: |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 963 | class_device_destroy(umad_class, port->dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
| 965 | err_cdev: |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 966 | cdev_del(port->dev); |
| 967 | clear_bit(port->dev_num, dev_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
| 969 | return -1; |
| 970 | } |
| 971 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 972 | static void ib_umad_kill_port(struct ib_umad_port *port) |
| 973 | { |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 974 | struct ib_umad_file *file; |
| 975 | int id; |
| 976 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 977 | class_set_devdata(port->class_dev, NULL); |
| 978 | class_set_devdata(port->sm_class_dev, NULL); |
| 979 | |
| 980 | class_device_destroy(umad_class, port->dev->dev); |
| 981 | class_device_destroy(umad_class, port->sm_dev->dev); |
| 982 | |
| 983 | cdev_del(port->dev); |
| 984 | cdev_del(port->sm_dev); |
| 985 | |
| 986 | spin_lock(&port_lock); |
| 987 | umad_port[port->dev_num] = NULL; |
| 988 | spin_unlock(&port_lock); |
| 989 | |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 990 | down_write(&port->mutex); |
| 991 | |
| 992 | port->ib_dev = NULL; |
| 993 | |
Roland Dreier | 94382f3 | 2005-11-10 10:18:23 -0800 | [diff] [blame] | 994 | /* |
| 995 | * Now go through the list of files attached to this port and |
| 996 | * unregister all of their MAD agents. We need to hold |
| 997 | * port->mutex while doing this to avoid racing with |
| 998 | * ib_umad_close(), but we can't hold the mutex for writing |
| 999 | * while calling ib_unregister_mad_agent(), since that might |
| 1000 | * deadlock by calling back into queue_packet(). So we |
| 1001 | * downgrade our lock to a read lock, and then drop and |
| 1002 | * reacquire the write lock for the next iteration. |
| 1003 | * |
| 1004 | * We do list_del_init() on the file's list_head so that the |
| 1005 | * list_del in ib_umad_close() is still OK, even after the |
| 1006 | * file is removed from the list. |
| 1007 | */ |
| 1008 | while (!list_empty(&port->file_list)) { |
| 1009 | file = list_entry(port->file_list.next, struct ib_umad_file, |
| 1010 | port_list); |
| 1011 | |
| 1012 | file->agents_dead = 1; |
| 1013 | list_del_init(&file->port_list); |
| 1014 | |
| 1015 | downgrade_write(&port->mutex); |
| 1016 | |
| 1017 | for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id) |
| 1018 | if (file->agent[id]) |
| 1019 | ib_unregister_mad_agent(file->agent[id]); |
| 1020 | |
| 1021 | up_read(&port->mutex); |
| 1022 | down_write(&port->mutex); |
| 1023 | } |
Roland Dreier | 0c99cb6 | 2005-11-03 12:01:18 -0800 | [diff] [blame] | 1024 | |
| 1025 | up_write(&port->mutex); |
| 1026 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 1027 | clear_bit(port->dev_num, dev_map); |
| 1028 | } |
| 1029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | static void ib_umad_add_one(struct ib_device *device) |
| 1031 | { |
| 1032 | struct ib_umad_device *umad_dev; |
| 1033 | int s, e, i; |
| 1034 | |
Tom Tucker | 07ebafb | 2006-08-03 16:02:42 -0500 | [diff] [blame] | 1035 | if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB) |
| 1036 | return; |
| 1037 | |
| 1038 | if (device->node_type == RDMA_NODE_IB_SWITCH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | s = e = 0; |
| 1040 | else { |
| 1041 | s = 1; |
| 1042 | e = device->phys_port_cnt; |
| 1043 | } |
| 1044 | |
Sean Hefty | cb0f091 | 2005-10-27 20:48:11 -0700 | [diff] [blame] | 1045 | umad_dev = kzalloc(sizeof *umad_dev + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | (e - s + 1) * sizeof (struct ib_umad_port), |
| 1047 | GFP_KERNEL); |
| 1048 | if (!umad_dev) |
| 1049 | return; |
| 1050 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | kref_init(&umad_dev->ref); |
| 1052 | |
| 1053 | umad_dev->start_port = s; |
| 1054 | umad_dev->end_port = e; |
| 1055 | |
| 1056 | for (i = s; i <= e; ++i) { |
| 1057 | umad_dev->port[i - s].umad_dev = umad_dev; |
| 1058 | |
| 1059 | if (ib_umad_init_port(device, i, &umad_dev->port[i - s])) |
| 1060 | goto err; |
| 1061 | } |
| 1062 | |
| 1063 | ib_set_client_data(device, &umad_client, umad_dev); |
| 1064 | |
| 1065 | return; |
| 1066 | |
| 1067 | err: |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 1068 | while (--i >= s) |
Michael S. Tsirkin | 8b37b94 | 2005-11-06 15:47:02 -0800 | [diff] [blame] | 1069 | ib_umad_kill_port(&umad_dev->port[i - s]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
| 1071 | kref_put(&umad_dev->ref, ib_umad_release_dev); |
| 1072 | } |
| 1073 | |
| 1074 | static void ib_umad_remove_one(struct ib_device *device) |
| 1075 | { |
| 1076 | struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client); |
| 1077 | int i; |
| 1078 | |
| 1079 | if (!umad_dev) |
| 1080 | return; |
| 1081 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 1082 | for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i) |
| 1083 | ib_umad_kill_port(&umad_dev->port[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
| 1085 | kref_put(&umad_dev->ref, ib_umad_release_dev); |
| 1086 | } |
| 1087 | |
| 1088 | static int __init ib_umad_init(void) |
| 1089 | { |
| 1090 | int ret; |
| 1091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2, |
| 1093 | "infiniband_mad"); |
| 1094 | if (ret) { |
| 1095 | printk(KERN_ERR "user_mad: couldn't register device number\n"); |
| 1096 | goto out; |
| 1097 | } |
| 1098 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 1099 | umad_class = class_create(THIS_MODULE, "infiniband_mad"); |
| 1100 | if (IS_ERR(umad_class)) { |
| 1101 | ret = PTR_ERR(umad_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n"); |
| 1103 | goto out_chrdev; |
| 1104 | } |
| 1105 | |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 1106 | ret = class_create_file(umad_class, &class_attr_abi_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | if (ret) { |
| 1108 | printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n"); |
| 1109 | goto out_class; |
| 1110 | } |
| 1111 | |
| 1112 | ret = ib_register_client(&umad_client); |
| 1113 | if (ret) { |
| 1114 | printk(KERN_ERR "user_mad: couldn't register ib_umad client\n"); |
| 1115 | goto out_class; |
| 1116 | } |
| 1117 | |
| 1118 | return 0; |
| 1119 | |
| 1120 | out_class: |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 1121 | class_destroy(umad_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | |
| 1123 | out_chrdev: |
| 1124 | unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2); |
| 1125 | |
| 1126 | out: |
| 1127 | return ret; |
| 1128 | } |
| 1129 | |
| 1130 | static void __exit ib_umad_cleanup(void) |
| 1131 | { |
| 1132 | ib_unregister_client(&umad_client); |
Roland Dreier | a74968f | 2005-10-28 15:37:23 -0700 | [diff] [blame] | 1133 | class_destroy(umad_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2); |
| 1135 | } |
| 1136 | |
| 1137 | module_init(ib_umad_init); |
| 1138 | module_exit(ib_umad_cleanup); |