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