📄 user_mad.c
字号:
agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num, ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI, ureq.mgmt_class ? &req : NULL, ureq.rmpp_version, send_handler, recv_handler, file); if (IS_ERR(agent)) { ret = PTR_ERR(agent); goto out; } if (put_user(agent_id, (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) { ret = -EFAULT; ib_unregister_mad_agent(agent); goto out; } file->agent[agent_id] = agent; ret = 0;out: up_write(&file->port->mutex); return ret;}static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg){ struct ib_mad_agent *agent = NULL; u32 id; int ret = 0; if (get_user(id, (u32 __user *) arg)) return -EFAULT; down_write(&file->port->mutex); if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) { ret = -EINVAL; goto out; } agent = file->agent[id]; file->agent[id] = NULL;out: up_write(&file->port->mutex); if (agent) ib_unregister_mad_agent(agent); return ret;}static long ib_umad_ioctl(struct file *filp, unsigned int cmd, unsigned long arg){ switch (cmd) { case IB_USER_MAD_REGISTER_AGENT: return ib_umad_reg_agent(filp->private_data, arg); case IB_USER_MAD_UNREGISTER_AGENT: return ib_umad_unreg_agent(filp->private_data, arg); default: return -ENOIOCTLCMD; }}static int ib_umad_open(struct inode *inode, struct file *filp){ struct ib_umad_port *port; struct ib_umad_file *file; int ret = 0; spin_lock(&port_lock); port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE]; if (port) kref_get(&port->umad_dev->ref); spin_unlock(&port_lock); if (!port) return -ENXIO; down_write(&port->mutex); if (!port->ib_dev) { ret = -ENXIO; goto out; } file = kzalloc(sizeof *file, GFP_KERNEL); if (!file) { kref_put(&port->umad_dev->ref, ib_umad_release_dev); ret = -ENOMEM; goto out; } spin_lock_init(&file->recv_lock); INIT_LIST_HEAD(&file->recv_list); init_waitqueue_head(&file->recv_wait); file->port = port; filp->private_data = file; list_add_tail(&file->port_list, &port->file_list);out: up_write(&port->mutex); return ret;}static int ib_umad_close(struct inode *inode, struct file *filp){ struct ib_umad_file *file = filp->private_data; struct ib_umad_device *dev = file->port->umad_dev; struct ib_umad_packet *packet, *tmp; int already_dead; int i; down_write(&file->port->mutex); already_dead = file->agents_dead; file->agents_dead = 1; list_for_each_entry_safe(packet, tmp, &file->recv_list, list) kfree(packet); list_del(&file->port_list); downgrade_write(&file->port->mutex); if (!already_dead) for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i) if (file->agent[i]) ib_unregister_mad_agent(file->agent[i]); up_read(&file->port->mutex); kfree(file); kref_put(&dev->ref, ib_umad_release_dev); return 0;}static struct file_operations umad_fops = { .owner = THIS_MODULE, .read = ib_umad_read, .write = ib_umad_write, .poll = ib_umad_poll, .unlocked_ioctl = ib_umad_ioctl, .compat_ioctl = ib_umad_ioctl, .open = ib_umad_open, .release = ib_umad_close};static int ib_umad_sm_open(struct inode *inode, struct file *filp){ struct ib_umad_port *port; struct ib_port_modify props = { .set_port_cap_mask = IB_PORT_SM }; int ret; spin_lock(&port_lock); port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS]; if (port) kref_get(&port->umad_dev->ref); spin_unlock(&port_lock); if (!port) return -ENXIO; if (filp->f_flags & O_NONBLOCK) { if (down_trylock(&port->sm_sem)) { ret = -EAGAIN; goto fail; } } else { if (down_interruptible(&port->sm_sem)) { ret = -ERESTARTSYS; goto fail; } } ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); if (ret) { up(&port->sm_sem); goto fail; } filp->private_data = port; return 0;fail: kref_put(&port->umad_dev->ref, ib_umad_release_dev); return ret;}static int ib_umad_sm_close(struct inode *inode, struct file *filp){ struct ib_umad_port *port = filp->private_data; struct ib_port_modify props = { .clr_port_cap_mask = IB_PORT_SM }; int ret = 0; down_write(&port->mutex); if (port->ib_dev) ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props); up_write(&port->mutex); up(&port->sm_sem); kref_put(&port->umad_dev->ref, ib_umad_release_dev); return ret;}static struct file_operations umad_sm_fops = { .owner = THIS_MODULE, .open = ib_umad_sm_open, .release = ib_umad_sm_close};static struct ib_client umad_client = { .name = "umad", .add = ib_umad_add_one, .remove = ib_umad_remove_one};static ssize_t show_ibdev(struct class_device *class_dev, char *buf){ struct ib_umad_port *port = class_get_devdata(class_dev); if (!port) return -ENODEV; return sprintf(buf, "%s\n", port->ib_dev->name);}static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);static ssize_t show_port(struct class_device *class_dev, char *buf){ struct ib_umad_port *port = class_get_devdata(class_dev); if (!port) return -ENODEV; return sprintf(buf, "%d\n", port->port_num);}static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);static ssize_t show_abi_version(struct class *class, char *buf){ return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);}static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);static int ib_umad_init_port(struct ib_device *device, int port_num, struct ib_umad_port *port){ spin_lock(&port_lock); port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS); if (port->dev_num >= IB_UMAD_MAX_PORTS) { spin_unlock(&port_lock); return -1; } set_bit(port->dev_num, dev_map); spin_unlock(&port_lock); port->ib_dev = device; port->port_num = port_num; init_MUTEX(&port->sm_sem); init_rwsem(&port->mutex); INIT_LIST_HEAD(&port->file_list); port->dev = cdev_alloc(); if (!port->dev) return -1; port->dev->owner = THIS_MODULE; port->dev->ops = &umad_fops; kobject_set_name(&port->dev->kobj, "umad%d", port->dev_num); if (cdev_add(port->dev, base_dev + port->dev_num, 1)) goto err_cdev; port->class_dev = class_device_create(umad_class, NULL, port->dev->dev, device->dma_device, "umad%d", port->dev_num); if (IS_ERR(port->class_dev)) goto err_cdev; if (class_device_create_file(port->class_dev, &class_device_attr_ibdev)) goto err_class; if (class_device_create_file(port->class_dev, &class_device_attr_port)) goto err_class; port->sm_dev = cdev_alloc(); if (!port->sm_dev) goto err_class; port->sm_dev->owner = THIS_MODULE; port->sm_dev->ops = &umad_sm_fops; kobject_set_name(&port->sm_dev->kobj, "issm%d", port->dev_num); if (cdev_add(port->sm_dev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1)) goto err_sm_cdev; port->sm_class_dev = class_device_create(umad_class, NULL, port->sm_dev->dev, device->dma_device, "issm%d", port->dev_num); if (IS_ERR(port->sm_class_dev)) goto err_sm_cdev; class_set_devdata(port->class_dev, port); class_set_devdata(port->sm_class_dev, port); if (class_device_create_file(port->sm_class_dev, &class_device_attr_ibdev)) goto err_sm_class; if (class_device_create_file(port->sm_class_dev, &class_device_attr_port)) goto err_sm_class; spin_lock(&port_lock); umad_port[port->dev_num] = port; spin_unlock(&port_lock); return 0;err_sm_class: class_device_destroy(umad_class, port->sm_dev->dev);err_sm_cdev: cdev_del(port->sm_dev);err_class: class_device_destroy(umad_class, port->dev->dev);err_cdev: cdev_del(port->dev); clear_bit(port->dev_num, dev_map); return -1;}static void ib_umad_kill_port(struct ib_umad_port *port){ struct ib_umad_file *file; int id; class_set_devdata(port->class_dev, NULL); class_set_devdata(port->sm_class_dev, NULL); class_device_destroy(umad_class, port->dev->dev); class_device_destroy(umad_class, port->sm_dev->dev); cdev_del(port->dev); cdev_del(port->sm_dev); spin_lock(&port_lock); umad_port[port->dev_num] = NULL; spin_unlock(&port_lock); down_write(&port->mutex); port->ib_dev = NULL; /* * Now go through the list of files attached to this port and * unregister all of their MAD agents. We need to hold * port->mutex while doing this to avoid racing with * ib_umad_close(), but we can't hold the mutex for writing * while calling ib_unregister_mad_agent(), since that might * deadlock by calling back into queue_packet(). So we * downgrade our lock to a read lock, and then drop and * reacquire the write lock for the next iteration. * * We do list_del_init() on the file's list_head so that the * list_del in ib_umad_close() is still OK, even after the * file is removed from the list. */ while (!list_empty(&port->file_list)) { file = list_entry(port->file_list.next, struct ib_umad_file, port_list); file->agents_dead = 1; list_del_init(&file->port_list); downgrade_write(&port->mutex); for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id) if (file->agent[id]) ib_unregister_mad_agent(file->agent[id]); up_read(&port->mutex); down_write(&port->mutex); } up_write(&port->mutex); clear_bit(port->dev_num, dev_map);}static void ib_umad_add_one(struct ib_device *device){ struct ib_umad_device *umad_dev; int s, e, i; if (device->node_type == IB_NODE_SWITCH) s = e = 0; else { s = 1; e = device->phys_port_cnt; } umad_dev = kzalloc(sizeof *umad_dev + (e - s + 1) * sizeof (struct ib_umad_port), GFP_KERNEL); if (!umad_dev) return; kref_init(&umad_dev->ref); umad_dev->start_port = s; umad_dev->end_port = e; for (i = s; i <= e; ++i) { umad_dev->port[i - s].umad_dev = umad_dev; if (ib_umad_init_port(device, i, &umad_dev->port[i - s])) goto err; } ib_set_client_data(device, &umad_client, umad_dev); return;err: while (--i >= s) ib_umad_kill_port(&umad_dev->port[i - s]); kref_put(&umad_dev->ref, ib_umad_release_dev);}static void ib_umad_remove_one(struct ib_device *device){ struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client); int i; if (!umad_dev) return; for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i) ib_umad_kill_port(&umad_dev->port[i]); kref_put(&umad_dev->ref, ib_umad_release_dev);}static int __init ib_umad_init(void){ int ret; ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2, "infiniband_mad"); if (ret) { printk(KERN_ERR "user_mad: couldn't register device number\n"); goto out; } umad_class = class_create(THIS_MODULE, "infiniband_mad"); if (IS_ERR(umad_class)) { ret = PTR_ERR(umad_class); printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n"); goto out_chrdev; } ret = class_create_file(umad_class, &class_attr_abi_version); if (ret) { printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n"); goto out_class; } ret = ib_register_client(&umad_client); if (ret) { printk(KERN_ERR "user_mad: couldn't register ib_umad client\n"); goto out_class; } return 0;out_class: class_destroy(umad_class);out_chrdev: unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);out: return ret;}static void __exit ib_umad_cleanup(void){ ib_unregister_client(&umad_client); class_destroy(umad_class); unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);}module_init(ib_umad_init);module_exit(ib_umad_cleanup);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -