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