]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (c) 2004 Topspin Communications. All rights reserved. | |
2a1d9b7f | 3 | * Copyright (c) 2005 Voltaire, Inc. All rights reserved. |
cb183a06 | 4 | * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. |
1da177e4 LT |
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 | * | |
cb183a06 | 34 | * $Id: user_mad.c 2814 2005-07-06 19:14:09Z halr $ |
1da177e4 LT |
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 | ||
a4d61e84 RD |
52 | #include <rdma/ib_mad.h> |
53 | #include <rdma/ib_user_mad.h> | |
1da177e4 LT |
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 | ||
a74968f8 RD |
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 | */ | |
88 | ||
1da177e4 | 89 | struct ib_umad_port { |
a74968f8 RD |
90 | struct cdev *dev; |
91 | struct class_device *class_dev; | |
1da177e4 | 92 | |
a74968f8 RD |
93 | struct cdev *sm_dev; |
94 | struct class_device *sm_class_dev; | |
1da177e4 LT |
95 | struct semaphore sm_sem; |
96 | ||
0c99cb6d RD |
97 | struct rw_semaphore mutex; |
98 | struct list_head file_list; | |
99 | ||
1da177e4 LT |
100 | struct ib_device *ib_dev; |
101 | struct ib_umad_device *umad_dev; | |
a74968f8 | 102 | int dev_num; |
1da177e4 LT |
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 { | |
113 | struct ib_umad_port *port; | |
1da177e4 | 114 | struct list_head recv_list; |
0c99cb6d RD |
115 | struct list_head port_list; |
116 | spinlock_t recv_lock; | |
1da177e4 | 117 | wait_queue_head_t recv_wait; |
1da177e4 LT |
118 | struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS]; |
119 | struct ib_mr *mr[IB_UMAD_MAX_AGENTS]; | |
120 | }; | |
121 | ||
122 | struct ib_umad_packet { | |
cb183a06 | 123 | struct ib_mad_send_buf *msg; |
1da177e4 | 124 | struct list_head list; |
cb183a06 | 125 | int length; |
cb183a06 | 126 | struct ib_user_mad mad; |
1da177e4 LT |
127 | }; |
128 | ||
a74968f8 RD |
129 | static struct class *umad_class; |
130 | ||
1da177e4 | 131 | static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE); |
a74968f8 RD |
132 | |
133 | static DEFINE_SPINLOCK(port_lock); | |
134 | static struct ib_umad_port *umad_port[IB_UMAD_MAX_PORTS]; | |
1da177e4 LT |
135 | static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS * 2); |
136 | ||
137 | static void ib_umad_add_one(struct ib_device *device); | |
138 | static void ib_umad_remove_one(struct ib_device *device); | |
139 | ||
a74968f8 RD |
140 | static void ib_umad_release_dev(struct kref *ref) |
141 | { | |
142 | struct ib_umad_device *dev = | |
143 | container_of(ref, struct ib_umad_device, ref); | |
144 | ||
145 | kfree(dev); | |
146 | } | |
147 | ||
1da177e4 LT |
148 | static int queue_packet(struct ib_umad_file *file, |
149 | struct ib_mad_agent *agent, | |
150 | struct ib_umad_packet *packet) | |
151 | { | |
152 | int ret = 1; | |
153 | ||
0c99cb6d | 154 | down_read(&file->port->mutex); |
cb183a06 HR |
155 | for (packet->mad.hdr.id = 0; |
156 | packet->mad.hdr.id < IB_UMAD_MAX_AGENTS; | |
157 | packet->mad.hdr.id++) | |
158 | if (agent == file->agent[packet->mad.hdr.id]) { | |
1da177e4 LT |
159 | spin_lock_irq(&file->recv_lock); |
160 | list_add_tail(&packet->list, &file->recv_list); | |
161 | spin_unlock_irq(&file->recv_lock); | |
162 | wake_up_interruptible(&file->recv_wait); | |
163 | ret = 0; | |
164 | break; | |
165 | } | |
166 | ||
0c99cb6d | 167 | up_read(&file->port->mutex); |
1da177e4 LT |
168 | |
169 | return ret; | |
170 | } | |
171 | ||
172 | static void send_handler(struct ib_mad_agent *agent, | |
173 | struct ib_mad_send_wc *send_wc) | |
174 | { | |
175 | struct ib_umad_file *file = agent->context; | |
34816ad9 SH |
176 | struct ib_umad_packet *timeout; |
177 | struct ib_umad_packet *packet = send_wc->send_buf->context[0]; | |
1da177e4 | 178 | |
34816ad9 | 179 | ib_destroy_ah(packet->msg->ah); |
cb183a06 | 180 | ib_free_send_mad(packet->msg); |
1da177e4 LT |
181 | |
182 | if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) { | |
cb0f0910 | 183 | timeout = kzalloc(sizeof *timeout + IB_MGMT_MAD_HDR, GFP_KERNEL); |
cb183a06 HR |
184 | if (!timeout) |
185 | goto out; | |
1da177e4 | 186 | |
cb0f0910 SH |
187 | timeout->length = IB_MGMT_MAD_HDR; |
188 | timeout->mad.hdr.id = packet->mad.hdr.id; | |
cb183a06 HR |
189 | timeout->mad.hdr.status = ETIMEDOUT; |
190 | memcpy(timeout->mad.data, packet->mad.data, | |
191 | sizeof (struct ib_mad_hdr)); | |
192 | ||
193 | if (!queue_packet(file, agent, timeout)) | |
194 | return; | |
195 | } | |
196 | out: | |
1da177e4 LT |
197 | kfree(packet); |
198 | } | |
199 | ||
200 | static void recv_handler(struct ib_mad_agent *agent, | |
201 | struct ib_mad_recv_wc *mad_recv_wc) | |
202 | { | |
203 | struct ib_umad_file *file = agent->context; | |
204 | struct ib_umad_packet *packet; | |
cb183a06 | 205 | int length; |
1da177e4 LT |
206 | |
207 | if (mad_recv_wc->wc->status != IB_WC_SUCCESS) | |
208 | goto out; | |
209 | ||
cb183a06 | 210 | length = mad_recv_wc->mad_len; |
cb0f0910 | 211 | packet = kzalloc(sizeof *packet + length, GFP_KERNEL); |
1da177e4 LT |
212 | if (!packet) |
213 | goto out; | |
214 | ||
cb183a06 HR |
215 | packet->length = length; |
216 | ||
217 | ib_coalesce_recv_mad(mad_recv_wc, packet->mad.data); | |
1da177e4 | 218 | |
cb183a06 HR |
219 | packet->mad.hdr.status = 0; |
220 | packet->mad.hdr.length = length + sizeof (struct ib_user_mad); | |
221 | packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp); | |
222 | packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid); | |
223 | packet->mad.hdr.sl = mad_recv_wc->wc->sl; | |
224 | packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits; | |
225 | packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH); | |
226 | if (packet->mad.hdr.grh_present) { | |
1da177e4 | 227 | /* XXX parse GRH */ |
cb183a06 HR |
228 | packet->mad.hdr.gid_index = 0; |
229 | packet->mad.hdr.hop_limit = 0; | |
230 | packet->mad.hdr.traffic_class = 0; | |
231 | memset(packet->mad.hdr.gid, 0, 16); | |
232 | packet->mad.hdr.flow_label = 0; | |
1da177e4 LT |
233 | } |
234 | ||
235 | if (queue_packet(file, agent, packet)) | |
236 | kfree(packet); | |
237 | ||
238 | out: | |
239 | ib_free_recv_mad(mad_recv_wc); | |
240 | } | |
241 | ||
242 | static ssize_t ib_umad_read(struct file *filp, char __user *buf, | |
243 | size_t count, loff_t *pos) | |
244 | { | |
245 | struct ib_umad_file *file = filp->private_data; | |
246 | struct ib_umad_packet *packet; | |
247 | ssize_t ret; | |
248 | ||
cb183a06 | 249 | if (count < sizeof (struct ib_user_mad) + sizeof (struct ib_mad)) |
1da177e4 LT |
250 | return -EINVAL; |
251 | ||
252 | spin_lock_irq(&file->recv_lock); | |
253 | ||
254 | while (list_empty(&file->recv_list)) { | |
255 | spin_unlock_irq(&file->recv_lock); | |
256 | ||
257 | if (filp->f_flags & O_NONBLOCK) | |
258 | return -EAGAIN; | |
259 | ||
260 | if (wait_event_interruptible(file->recv_wait, | |
261 | !list_empty(&file->recv_list))) | |
262 | return -ERESTARTSYS; | |
263 | ||
264 | spin_lock_irq(&file->recv_lock); | |
265 | } | |
266 | ||
267 | packet = list_entry(file->recv_list.next, struct ib_umad_packet, list); | |
268 | list_del(&packet->list); | |
269 | ||
270 | spin_unlock_irq(&file->recv_lock); | |
271 | ||
cb183a06 HR |
272 | if (count < packet->length + sizeof (struct ib_user_mad)) { |
273 | /* Return length needed (and first RMPP segment) if too small */ | |
274 | if (copy_to_user(buf, &packet->mad, | |
275 | sizeof (struct ib_user_mad) + sizeof (struct ib_mad))) | |
276 | ret = -EFAULT; | |
277 | else | |
278 | ret = -ENOSPC; | |
279 | } else if (copy_to_user(buf, &packet->mad, | |
cb0f0910 | 280 | packet->length + sizeof (struct ib_user_mad))) |
1da177e4 LT |
281 | ret = -EFAULT; |
282 | else | |
cb183a06 HR |
283 | ret = packet->length + sizeof (struct ib_user_mad); |
284 | if (ret < 0) { | |
285 | /* Requeue packet */ | |
286 | spin_lock_irq(&file->recv_lock); | |
287 | list_add(&packet->list, &file->recv_list); | |
288 | spin_unlock_irq(&file->recv_lock); | |
289 | } else | |
290 | kfree(packet); | |
1da177e4 LT |
291 | return ret; |
292 | } | |
293 | ||
294 | static ssize_t ib_umad_write(struct file *filp, const char __user *buf, | |
295 | size_t count, loff_t *pos) | |
296 | { | |
297 | struct ib_umad_file *file = filp->private_data; | |
298 | struct ib_umad_packet *packet; | |
299 | struct ib_mad_agent *agent; | |
300 | struct ib_ah_attr ah_attr; | |
34816ad9 | 301 | struct ib_ah *ah; |
cb183a06 | 302 | struct ib_rmpp_mad *rmpp_mad; |
1da177e4 | 303 | u8 method; |
97f52eb4 | 304 | __be64 *tid; |
cb0f0910 | 305 | int ret, length, hdr_len, copy_offset; |
cb183a06 | 306 | int rmpp_active = 0; |
1da177e4 LT |
307 | |
308 | if (count < sizeof (struct ib_user_mad)) | |
309 | return -EINVAL; | |
310 | ||
cb183a06 | 311 | length = count - sizeof (struct ib_user_mad); |
cb0f0910 | 312 | packet = kmalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL); |
1da177e4 LT |
313 | if (!packet) |
314 | return -ENOMEM; | |
315 | ||
cb183a06 | 316 | if (copy_from_user(&packet->mad, buf, |
cb0f0910 | 317 | sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)) { |
cb183a06 HR |
318 | ret = -EFAULT; |
319 | goto err; | |
1da177e4 LT |
320 | } |
321 | ||
cb183a06 HR |
322 | if (packet->mad.hdr.id < 0 || |
323 | packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) { | |
1da177e4 LT |
324 | ret = -EINVAL; |
325 | goto err; | |
326 | } | |
327 | ||
0c99cb6d | 328 | down_read(&file->port->mutex); |
1da177e4 | 329 | |
cb183a06 | 330 | agent = file->agent[packet->mad.hdr.id]; |
1da177e4 LT |
331 | if (!agent) { |
332 | ret = -EINVAL; | |
333 | goto err_up; | |
334 | } | |
335 | ||
1da177e4 | 336 | memset(&ah_attr, 0, sizeof ah_attr); |
cb183a06 HR |
337 | ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid); |
338 | ah_attr.sl = packet->mad.hdr.sl; | |
339 | ah_attr.src_path_bits = packet->mad.hdr.path_bits; | |
1da177e4 | 340 | ah_attr.port_num = file->port->port_num; |
cb183a06 | 341 | if (packet->mad.hdr.grh_present) { |
1da177e4 | 342 | ah_attr.ah_flags = IB_AH_GRH; |
cb183a06 | 343 | memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16); |
97f52eb4 | 344 | ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label); |
cb183a06 HR |
345 | ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit; |
346 | ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class; | |
1da177e4 LT |
347 | } |
348 | ||
34816ad9 SH |
349 | ah = ib_create_ah(agent->qp->pd, &ah_attr); |
350 | if (IS_ERR(ah)) { | |
351 | ret = PTR_ERR(ah); | |
1da177e4 LT |
352 | goto err_up; |
353 | } | |
354 | ||
cb183a06 HR |
355 | rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data; |
356 | if (ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_ACTIVE) { | |
357 | /* RMPP active */ | |
358 | if (!agent->rmpp_version) { | |
359 | ret = -EINVAL; | |
360 | goto err_ah; | |
361 | } | |
eff4c654 HR |
362 | |
363 | /* Validate that the management class can support RMPP */ | |
cb183a06 | 364 | if (rmpp_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_ADM) { |
34816ad9 | 365 | hdr_len = IB_MGMT_SA_HDR; |
cb183a06 HR |
366 | } else if ((rmpp_mad->mad_hdr.mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) && |
367 | (rmpp_mad->mad_hdr.mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)) { | |
34816ad9 | 368 | hdr_len = IB_MGMT_VENDOR_HDR; |
cb183a06 HR |
369 | } else { |
370 | ret = -EINVAL; | |
371 | goto err_ah; | |
372 | } | |
373 | rmpp_active = 1; | |
cb0f0910 | 374 | copy_offset = IB_MGMT_RMPP_HDR; |
cb183a06 | 375 | } else { |
34816ad9 | 376 | hdr_len = IB_MGMT_MAD_HDR; |
cb0f0910 | 377 | copy_offset = IB_MGMT_MAD_HDR; |
cb183a06 HR |
378 | } |
379 | ||
380 | packet->msg = ib_create_send_mad(agent, | |
381 | be32_to_cpu(packet->mad.hdr.qpn), | |
34816ad9 SH |
382 | 0, rmpp_active, |
383 | hdr_len, length - hdr_len, | |
cb183a06 HR |
384 | GFP_KERNEL); |
385 | if (IS_ERR(packet->msg)) { | |
386 | ret = PTR_ERR(packet->msg); | |
387 | goto err_ah; | |
388 | } | |
1da177e4 | 389 | |
34816ad9 SH |
390 | packet->msg->ah = ah; |
391 | packet->msg->timeout_ms = packet->mad.hdr.timeout_ms; | |
392 | packet->msg->retries = packet->mad.hdr.retries; | |
393 | packet->msg->context[0] = packet; | |
1da177e4 | 394 | |
cb0f0910 SH |
395 | /* Copy MAD headers (RMPP header in place) */ |
396 | memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR); | |
397 | /* Now, copy rest of message from user into send buffer */ | |
398 | if (copy_from_user(packet->msg->mad + copy_offset, | |
399 | buf + sizeof (struct ib_user_mad) + copy_offset, | |
400 | length - copy_offset)) { | |
401 | ret = -EFAULT; | |
402 | goto err_msg; | |
cb183a06 HR |
403 | } |
404 | ||
405 | /* | |
406 | * If userspace is generating a request that will generate a | |
407 | * response, we need to make sure the high-order part of the | |
408 | * transaction ID matches the agent being used to send the | |
409 | * MAD. | |
410 | */ | |
089a1bed | 411 | method = ((struct ib_mad_hdr *) packet->msg->mad)->method; |
cb183a06 HR |
412 | |
413 | if (!(method & IB_MGMT_METHOD_RESP) && | |
414 | method != IB_MGMT_METHOD_TRAP_REPRESS && | |
415 | method != IB_MGMT_METHOD_SEND) { | |
089a1bed | 416 | tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid; |
cb183a06 HR |
417 | *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 | |
418 | (be64_to_cpup(tid) & 0xffffffff)); | |
1da177e4 LT |
419 | } |
420 | ||
34816ad9 | 421 | ret = ib_post_send_mad(packet->msg, NULL); |
cb183a06 HR |
422 | if (ret) |
423 | goto err_msg; | |
424 | ||
0c99cb6d | 425 | up_read(&file->port->mutex); |
1da177e4 | 426 | |
cb0f0910 | 427 | return count; |
cb183a06 HR |
428 | |
429 | err_msg: | |
430 | ib_free_send_mad(packet->msg); | |
431 | ||
432 | err_ah: | |
34816ad9 | 433 | ib_destroy_ah(ah); |
1da177e4 LT |
434 | |
435 | err_up: | |
0c99cb6d | 436 | up_read(&file->port->mutex); |
1da177e4 LT |
437 | |
438 | err: | |
439 | kfree(packet); | |
440 | return ret; | |
441 | } | |
442 | ||
443 | static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait) | |
444 | { | |
445 | struct ib_umad_file *file = filp->private_data; | |
446 | ||
447 | /* we will always be able to post a MAD send */ | |
448 | unsigned int mask = POLLOUT | POLLWRNORM; | |
449 | ||
450 | poll_wait(filp, &file->recv_wait, wait); | |
451 | ||
452 | if (!list_empty(&file->recv_list)) | |
453 | mask |= POLLIN | POLLRDNORM; | |
454 | ||
455 | return mask; | |
456 | } | |
457 | ||
458 | static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg) | |
459 | { | |
460 | struct ib_user_mad_reg_req ureq; | |
461 | struct ib_mad_reg_req req; | |
462 | struct ib_mad_agent *agent; | |
463 | int agent_id; | |
464 | int ret; | |
465 | ||
0c99cb6d RD |
466 | down_write(&file->port->mutex); |
467 | ||
468 | if (!file->port->ib_dev) { | |
469 | ret = -EPIPE; | |
470 | goto out; | |
471 | } | |
1da177e4 LT |
472 | |
473 | if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) { | |
474 | ret = -EFAULT; | |
475 | goto out; | |
476 | } | |
477 | ||
478 | if (ureq.qpn != 0 && ureq.qpn != 1) { | |
479 | ret = -EINVAL; | |
480 | goto out; | |
481 | } | |
482 | ||
483 | for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id) | |
484 | if (!file->agent[agent_id]) | |
485 | goto found; | |
486 | ||
487 | ret = -ENOMEM; | |
488 | goto out; | |
489 | ||
490 | found: | |
0df3bb13 RD |
491 | if (ureq.mgmt_class) { |
492 | req.mgmt_class = ureq.mgmt_class; | |
493 | req.mgmt_class_version = ureq.mgmt_class_version; | |
494 | memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask); | |
495 | memcpy(req.oui, ureq.oui, sizeof req.oui); | |
496 | } | |
1da177e4 LT |
497 | |
498 | agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num, | |
499 | ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI, | |
0df3bb13 | 500 | ureq.mgmt_class ? &req : NULL, |
cb183a06 HR |
501 | ureq.rmpp_version, |
502 | send_handler, recv_handler, file); | |
1da177e4 LT |
503 | if (IS_ERR(agent)) { |
504 | ret = PTR_ERR(agent); | |
505 | goto out; | |
506 | } | |
507 | ||
508 | file->agent[agent_id] = agent; | |
509 | ||
510 | file->mr[agent_id] = ib_get_dma_mr(agent->qp->pd, IB_ACCESS_LOCAL_WRITE); | |
511 | if (IS_ERR(file->mr[agent_id])) { | |
512 | ret = -ENOMEM; | |
513 | goto err; | |
514 | } | |
515 | ||
516 | if (put_user(agent_id, | |
517 | (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) { | |
518 | ret = -EFAULT; | |
519 | goto err_mr; | |
520 | } | |
521 | ||
522 | ret = 0; | |
523 | goto out; | |
524 | ||
525 | err_mr: | |
526 | ib_dereg_mr(file->mr[agent_id]); | |
527 | ||
528 | err: | |
529 | file->agent[agent_id] = NULL; | |
530 | ib_unregister_mad_agent(agent); | |
531 | ||
532 | out: | |
0c99cb6d | 533 | up_write(&file->port->mutex); |
1da177e4 LT |
534 | return ret; |
535 | } | |
536 | ||
537 | static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg) | |
538 | { | |
539 | u32 id; | |
540 | int ret = 0; | |
541 | ||
0c99cb6d | 542 | down_write(&file->port->mutex); |
1da177e4 LT |
543 | |
544 | if (get_user(id, (u32 __user *) arg)) { | |
545 | ret = -EFAULT; | |
546 | goto out; | |
547 | } | |
548 | ||
549 | if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !file->agent[id]) { | |
550 | ret = -EINVAL; | |
551 | goto out; | |
552 | } | |
553 | ||
554 | ib_dereg_mr(file->mr[id]); | |
555 | ib_unregister_mad_agent(file->agent[id]); | |
556 | file->agent[id] = NULL; | |
557 | ||
558 | out: | |
0c99cb6d | 559 | up_write(&file->port->mutex); |
1da177e4 LT |
560 | return ret; |
561 | } | |
562 | ||
cb183a06 HR |
563 | static long ib_umad_ioctl(struct file *filp, unsigned int cmd, |
564 | unsigned long arg) | |
1da177e4 LT |
565 | { |
566 | switch (cmd) { | |
567 | case IB_USER_MAD_REGISTER_AGENT: | |
568 | return ib_umad_reg_agent(filp->private_data, arg); | |
569 | case IB_USER_MAD_UNREGISTER_AGENT: | |
570 | return ib_umad_unreg_agent(filp->private_data, arg); | |
571 | default: | |
572 | return -ENOIOCTLCMD; | |
573 | } | |
574 | } | |
575 | ||
576 | static int ib_umad_open(struct inode *inode, struct file *filp) | |
577 | { | |
a74968f8 | 578 | struct ib_umad_port *port; |
1da177e4 | 579 | struct ib_umad_file *file; |
0c99cb6d | 580 | int ret = 0; |
1da177e4 | 581 | |
a74968f8 RD |
582 | spin_lock(&port_lock); |
583 | port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE]; | |
584 | if (port) | |
585 | kref_get(&port->umad_dev->ref); | |
586 | spin_unlock(&port_lock); | |
587 | ||
588 | if (!port) | |
589 | return -ENXIO; | |
590 | ||
0c99cb6d RD |
591 | down_write(&port->mutex); |
592 | ||
593 | if (!port->ib_dev) { | |
594 | ret = -ENXIO; | |
595 | goto out; | |
596 | } | |
597 | ||
cb0f0910 | 598 | file = kzalloc(sizeof *file, GFP_KERNEL); |
a74968f8 RD |
599 | if (!file) { |
600 | kref_put(&port->umad_dev->ref, ib_umad_release_dev); | |
0c99cb6d RD |
601 | ret = -ENOMEM; |
602 | goto out; | |
a74968f8 | 603 | } |
1da177e4 | 604 | |
1da177e4 | 605 | spin_lock_init(&file->recv_lock); |
1da177e4 LT |
606 | INIT_LIST_HEAD(&file->recv_list); |
607 | init_waitqueue_head(&file->recv_wait); | |
608 | ||
609 | file->port = port; | |
610 | filp->private_data = file; | |
611 | ||
0c99cb6d RD |
612 | list_add_tail(&file->port_list, &port->file_list); |
613 | ||
614 | out: | |
615 | up_write(&port->mutex); | |
616 | return ret; | |
1da177e4 LT |
617 | } |
618 | ||
619 | static int ib_umad_close(struct inode *inode, struct file *filp) | |
620 | { | |
621 | struct ib_umad_file *file = filp->private_data; | |
a74968f8 | 622 | struct ib_umad_device *dev = file->port->umad_dev; |
561e148e | 623 | struct ib_umad_packet *packet, *tmp; |
1da177e4 LT |
624 | int i; |
625 | ||
0c99cb6d | 626 | down_write(&file->port->mutex); |
1da177e4 LT |
627 | for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i) |
628 | if (file->agent[i]) { | |
629 | ib_dereg_mr(file->mr[i]); | |
630 | ib_unregister_mad_agent(file->agent[i]); | |
631 | } | |
632 | ||
561e148e RD |
633 | list_for_each_entry_safe(packet, tmp, &file->recv_list, list) |
634 | kfree(packet); | |
635 | ||
0c99cb6d RD |
636 | list_del(&file->port_list); |
637 | up_write(&file->port->mutex); | |
638 | ||
1da177e4 LT |
639 | kfree(file); |
640 | ||
a74968f8 RD |
641 | kref_put(&dev->ref, ib_umad_release_dev); |
642 | ||
1da177e4 LT |
643 | return 0; |
644 | } | |
645 | ||
646 | static struct file_operations umad_fops = { | |
cb183a06 HR |
647 | .owner = THIS_MODULE, |
648 | .read = ib_umad_read, | |
649 | .write = ib_umad_write, | |
650 | .poll = ib_umad_poll, | |
1da177e4 | 651 | .unlocked_ioctl = ib_umad_ioctl, |
cb183a06 HR |
652 | .compat_ioctl = ib_umad_ioctl, |
653 | .open = ib_umad_open, | |
654 | .release = ib_umad_close | |
1da177e4 LT |
655 | }; |
656 | ||
657 | static int ib_umad_sm_open(struct inode *inode, struct file *filp) | |
658 | { | |
a74968f8 | 659 | struct ib_umad_port *port; |
1da177e4 LT |
660 | struct ib_port_modify props = { |
661 | .set_port_cap_mask = IB_PORT_SM | |
662 | }; | |
663 | int ret; | |
664 | ||
a74968f8 RD |
665 | spin_lock(&port_lock); |
666 | port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS]; | |
667 | if (port) | |
668 | kref_get(&port->umad_dev->ref); | |
669 | spin_unlock(&port_lock); | |
670 | ||
671 | if (!port) | |
672 | return -ENXIO; | |
673 | ||
1da177e4 | 674 | if (filp->f_flags & O_NONBLOCK) { |
a74968f8 RD |
675 | if (down_trylock(&port->sm_sem)) { |
676 | ret = -EAGAIN; | |
677 | goto fail; | |
678 | } | |
1da177e4 | 679 | } else { |
a74968f8 RD |
680 | if (down_interruptible(&port->sm_sem)) { |
681 | ret = -ERESTARTSYS; | |
682 | goto fail; | |
683 | } | |
1da177e4 LT |
684 | } |
685 | ||
686 | ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); | |
687 | if (ret) { | |
688 | up(&port->sm_sem); | |
a74968f8 | 689 | goto fail; |
1da177e4 LT |
690 | } |
691 | ||
692 | filp->private_data = port; | |
693 | ||
694 | return 0; | |
a74968f8 RD |
695 | |
696 | fail: | |
697 | kref_put(&port->umad_dev->ref, ib_umad_release_dev); | |
698 | return ret; | |
1da177e4 LT |
699 | } |
700 | ||
701 | static int ib_umad_sm_close(struct inode *inode, struct file *filp) | |
702 | { | |
703 | struct ib_umad_port *port = filp->private_data; | |
704 | struct ib_port_modify props = { | |
705 | .clr_port_cap_mask = IB_PORT_SM | |
706 | }; | |
0c99cb6d RD |
707 | int ret = 0; |
708 | ||
709 | down_write(&port->mutex); | |
710 | if (port->ib_dev) | |
711 | ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); | |
712 | up_write(&port->mutex); | |
1da177e4 | 713 | |
1da177e4 LT |
714 | up(&port->sm_sem); |
715 | ||
a74968f8 RD |
716 | kref_put(&port->umad_dev->ref, ib_umad_release_dev); |
717 | ||
1da177e4 LT |
718 | return ret; |
719 | } | |
720 | ||
721 | static struct file_operations umad_sm_fops = { | |
722 | .owner = THIS_MODULE, | |
723 | .open = ib_umad_sm_open, | |
724 | .release = ib_umad_sm_close | |
725 | }; | |
726 | ||
727 | static struct ib_client umad_client = { | |
728 | .name = "umad", | |
729 | .add = ib_umad_add_one, | |
730 | .remove = ib_umad_remove_one | |
731 | }; | |
732 | ||
1da177e4 LT |
733 | static ssize_t show_ibdev(struct class_device *class_dev, char *buf) |
734 | { | |
735 | struct ib_umad_port *port = class_get_devdata(class_dev); | |
736 | ||
a74968f8 RD |
737 | if (!port) |
738 | return -ENODEV; | |
739 | ||
1da177e4 LT |
740 | return sprintf(buf, "%s\n", port->ib_dev->name); |
741 | } | |
742 | static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); | |
743 | ||
744 | static ssize_t show_port(struct class_device *class_dev, char *buf) | |
745 | { | |
746 | struct ib_umad_port *port = class_get_devdata(class_dev); | |
747 | ||
a74968f8 RD |
748 | if (!port) |
749 | return -ENODEV; | |
750 | ||
1da177e4 LT |
751 | return sprintf(buf, "%d\n", port->port_num); |
752 | } | |
753 | static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL); | |
754 | ||
1da177e4 LT |
755 | static ssize_t show_abi_version(struct class *class, char *buf) |
756 | { | |
757 | return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION); | |
758 | } | |
759 | static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL); | |
760 | ||
761 | static int ib_umad_init_port(struct ib_device *device, int port_num, | |
762 | struct ib_umad_port *port) | |
763 | { | |
a74968f8 RD |
764 | spin_lock(&port_lock); |
765 | port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS); | |
766 | if (port->dev_num >= IB_UMAD_MAX_PORTS) { | |
767 | spin_unlock(&port_lock); | |
1da177e4 LT |
768 | return -1; |
769 | } | |
a74968f8 RD |
770 | set_bit(port->dev_num, dev_map); |
771 | spin_unlock(&port_lock); | |
1da177e4 LT |
772 | |
773 | port->ib_dev = device; | |
774 | port->port_num = port_num; | |
775 | init_MUTEX(&port->sm_sem); | |
0c99cb6d RD |
776 | init_rwsem(&port->mutex); |
777 | INIT_LIST_HEAD(&port->file_list); | |
1da177e4 | 778 | |
a74968f8 RD |
779 | port->dev = cdev_alloc(); |
780 | if (!port->dev) | |
1da177e4 | 781 | return -1; |
a74968f8 RD |
782 | port->dev->owner = THIS_MODULE; |
783 | port->dev->ops = &umad_fops; | |
784 | kobject_set_name(&port->dev->kobj, "umad%d", port->dev_num); | |
785 | if (cdev_add(port->dev, base_dev + port->dev_num, 1)) | |
1da177e4 LT |
786 | goto err_cdev; |
787 | ||
4cce3390 | 788 | port->class_dev = class_device_create(umad_class, NULL, port->dev->dev, |
a74968f8 RD |
789 | device->dma_device, |
790 | "umad%d", port->dev_num); | |
791 | if (IS_ERR(port->class_dev)) | |
792 | goto err_cdev; | |
1da177e4 | 793 | |
a74968f8 | 794 | if (class_device_create_file(port->class_dev, &class_device_attr_ibdev)) |
1da177e4 | 795 | goto err_class; |
a74968f8 | 796 | if (class_device_create_file(port->class_dev, &class_device_attr_port)) |
1da177e4 LT |
797 | goto err_class; |
798 | ||
a74968f8 RD |
799 | port->sm_dev = cdev_alloc(); |
800 | if (!port->sm_dev) | |
801 | goto err_class; | |
802 | port->sm_dev->owner = THIS_MODULE; | |
803 | port->sm_dev->ops = &umad_sm_fops; | |
8b37b947 | 804 | kobject_set_name(&port->sm_dev->kobj, "issm%d", port->dev_num); |
a74968f8 RD |
805 | if (cdev_add(port->sm_dev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1)) |
806 | goto err_sm_cdev; | |
1da177e4 | 807 | |
4cce3390 | 808 | port->sm_class_dev = class_device_create(umad_class, NULL, port->sm_dev->dev, |
a74968f8 RD |
809 | device->dma_device, |
810 | "issm%d", port->dev_num); | |
811 | if (IS_ERR(port->sm_class_dev)) | |
1da177e4 LT |
812 | goto err_sm_cdev; |
813 | ||
a74968f8 RD |
814 | class_set_devdata(port->class_dev, port); |
815 | class_set_devdata(port->sm_class_dev, port); | |
1da177e4 | 816 | |
a74968f8 | 817 | if (class_device_create_file(port->sm_class_dev, &class_device_attr_ibdev)) |
1da177e4 | 818 | goto err_sm_class; |
a74968f8 | 819 | if (class_device_create_file(port->sm_class_dev, &class_device_attr_port)) |
1da177e4 LT |
820 | goto err_sm_class; |
821 | ||
a74968f8 RD |
822 | spin_lock(&port_lock); |
823 | umad_port[port->dev_num] = port; | |
824 | spin_unlock(&port_lock); | |
825 | ||
1da177e4 LT |
826 | return 0; |
827 | ||
828 | err_sm_class: | |
a74968f8 | 829 | class_device_destroy(umad_class, port->sm_dev->dev); |
1da177e4 LT |
830 | |
831 | err_sm_cdev: | |
a74968f8 | 832 | cdev_del(port->sm_dev); |
1da177e4 LT |
833 | |
834 | err_class: | |
a74968f8 | 835 | class_device_destroy(umad_class, port->dev->dev); |
1da177e4 LT |
836 | |
837 | err_cdev: | |
a74968f8 RD |
838 | cdev_del(port->dev); |
839 | clear_bit(port->dev_num, dev_map); | |
1da177e4 LT |
840 | |
841 | return -1; | |
842 | } | |
843 | ||
a74968f8 RD |
844 | static void ib_umad_kill_port(struct ib_umad_port *port) |
845 | { | |
0c99cb6d RD |
846 | struct ib_umad_file *file; |
847 | int id; | |
848 | ||
a74968f8 RD |
849 | class_set_devdata(port->class_dev, NULL); |
850 | class_set_devdata(port->sm_class_dev, NULL); | |
851 | ||
852 | class_device_destroy(umad_class, port->dev->dev); | |
853 | class_device_destroy(umad_class, port->sm_dev->dev); | |
854 | ||
855 | cdev_del(port->dev); | |
856 | cdev_del(port->sm_dev); | |
857 | ||
858 | spin_lock(&port_lock); | |
859 | umad_port[port->dev_num] = NULL; | |
860 | spin_unlock(&port_lock); | |
861 | ||
0c99cb6d RD |
862 | down_write(&port->mutex); |
863 | ||
864 | port->ib_dev = NULL; | |
865 | ||
866 | list_for_each_entry(file, &port->file_list, port_list) | |
867 | for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id) { | |
868 | if (!file->agent[id]) | |
869 | continue; | |
870 | ib_dereg_mr(file->mr[id]); | |
871 | ib_unregister_mad_agent(file->agent[id]); | |
872 | file->agent[id] = NULL; | |
873 | } | |
874 | ||
875 | up_write(&port->mutex); | |
876 | ||
a74968f8 RD |
877 | clear_bit(port->dev_num, dev_map); |
878 | } | |
879 | ||
1da177e4 LT |
880 | static void ib_umad_add_one(struct ib_device *device) |
881 | { | |
882 | struct ib_umad_device *umad_dev; | |
883 | int s, e, i; | |
884 | ||
885 | if (device->node_type == IB_NODE_SWITCH) | |
886 | s = e = 0; | |
887 | else { | |
888 | s = 1; | |
889 | e = device->phys_port_cnt; | |
890 | } | |
891 | ||
cb0f0910 | 892 | umad_dev = kzalloc(sizeof *umad_dev + |
1da177e4 LT |
893 | (e - s + 1) * sizeof (struct ib_umad_port), |
894 | GFP_KERNEL); | |
895 | if (!umad_dev) | |
896 | return; | |
897 | ||
1da177e4 LT |
898 | kref_init(&umad_dev->ref); |
899 | ||
900 | umad_dev->start_port = s; | |
901 | umad_dev->end_port = e; | |
902 | ||
903 | for (i = s; i <= e; ++i) { | |
904 | umad_dev->port[i - s].umad_dev = umad_dev; | |
905 | ||
906 | if (ib_umad_init_port(device, i, &umad_dev->port[i - s])) | |
907 | goto err; | |
908 | } | |
909 | ||
910 | ib_set_client_data(device, &umad_client, umad_dev); | |
911 | ||
912 | return; | |
913 | ||
914 | err: | |
a74968f8 | 915 | while (--i >= s) |
8b37b947 | 916 | ib_umad_kill_port(&umad_dev->port[i - s]); |
1da177e4 LT |
917 | |
918 | kref_put(&umad_dev->ref, ib_umad_release_dev); | |
919 | } | |
920 | ||
921 | static void ib_umad_remove_one(struct ib_device *device) | |
922 | { | |
923 | struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client); | |
924 | int i; | |
925 | ||
926 | if (!umad_dev) | |
927 | return; | |
928 | ||
a74968f8 RD |
929 | for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i) |
930 | ib_umad_kill_port(&umad_dev->port[i]); | |
1da177e4 LT |
931 | |
932 | kref_put(&umad_dev->ref, ib_umad_release_dev); | |
933 | } | |
934 | ||
935 | static int __init ib_umad_init(void) | |
936 | { | |
937 | int ret; | |
938 | ||
1da177e4 LT |
939 | ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2, |
940 | "infiniband_mad"); | |
941 | if (ret) { | |
942 | printk(KERN_ERR "user_mad: couldn't register device number\n"); | |
943 | goto out; | |
944 | } | |
945 | ||
a74968f8 RD |
946 | umad_class = class_create(THIS_MODULE, "infiniband_mad"); |
947 | if (IS_ERR(umad_class)) { | |
948 | ret = PTR_ERR(umad_class); | |
1da177e4 LT |
949 | printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n"); |
950 | goto out_chrdev; | |
951 | } | |
952 | ||
a74968f8 | 953 | ret = class_create_file(umad_class, &class_attr_abi_version); |
1da177e4 LT |
954 | if (ret) { |
955 | printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n"); | |
956 | goto out_class; | |
957 | } | |
958 | ||
959 | ret = ib_register_client(&umad_client); | |
960 | if (ret) { | |
961 | printk(KERN_ERR "user_mad: couldn't register ib_umad client\n"); | |
962 | goto out_class; | |
963 | } | |
964 | ||
965 | return 0; | |
966 | ||
967 | out_class: | |
a74968f8 | 968 | class_destroy(umad_class); |
1da177e4 LT |
969 | |
970 | out_chrdev: | |
971 | unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2); | |
972 | ||
973 | out: | |
974 | return ret; | |
975 | } | |
976 | ||
977 | static void __exit ib_umad_cleanup(void) | |
978 | { | |
979 | ib_unregister_client(&umad_client); | |
a74968f8 | 980 | class_destroy(umad_class); |
1da177e4 LT |
981 | unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2); |
982 | } | |
983 | ||
984 | module_init(ib_umad_init); | |
985 | module_exit(ib_umad_cleanup); |