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