user_mad.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. * $Id: user_mad.c 1389 2004-12-27 22:56:47Z roland $
  33. */
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/device.h>
  37. #include <linux/err.h>
  38. #include <linux/fs.h>
  39. #include <linux/cdev.h>
  40. #include <linux/pci.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/poll.h>
  43. #include <linux/rwsem.h>
  44. #include <linux/kref.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/semaphore.h>
  47. #include <ib_mad.h>
  48. #include <ib_user_mad.h>
  49. MODULE_AUTHOR("Roland Dreier");
  50. MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
  51. MODULE_LICENSE("Dual BSD/GPL");
  52. enum {
  53. IB_UMAD_MAX_PORTS = 64,
  54. IB_UMAD_MAX_AGENTS = 32,
  55. IB_UMAD_MAJOR = 231,
  56. IB_UMAD_MINOR_BASE = 0
  57. };
  58. struct ib_umad_port {
  59. int devnum;
  60. struct cdev dev;
  61. struct class_device class_dev;
  62. int sm_devnum;
  63. struct cdev sm_dev;
  64. struct class_device sm_class_dev;
  65. struct semaphore sm_sem;
  66. struct ib_device *ib_dev;
  67. struct ib_umad_device *umad_dev;
  68. u8 port_num;
  69. };
  70. struct ib_umad_device {
  71. int start_port, end_port;
  72. struct kref ref;
  73. struct ib_umad_port port[0];
  74. };
  75. struct ib_umad_file {
  76. struct ib_umad_port *port;
  77. spinlock_t recv_lock;
  78. struct list_head recv_list;
  79. wait_queue_head_t recv_wait;
  80. struct rw_semaphore agent_mutex;
  81. struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];
  82. struct ib_mr *mr[IB_UMAD_MAX_AGENTS];
  83. };
  84. struct ib_umad_packet {
  85. struct ib_user_mad mad;
  86. struct ib_ah *ah;
  87. struct list_head list;
  88. DECLARE_PCI_UNMAP_ADDR(mapping)
  89. };
  90. static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
  91. static spinlock_t map_lock;
  92. static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS * 2);
  93. static void ib_umad_add_one(struct ib_device *device);
  94. static void ib_umad_remove_one(struct ib_device *device);
  95. static int queue_packet(struct ib_umad_file *file,
  96. struct ib_mad_agent *agent,
  97. struct ib_umad_packet *packet)
  98. {
  99. int ret = 1;
  100. down_read(&file->agent_mutex);
  101. for (packet->mad.id = 0;
  102. packet->mad.id < IB_UMAD_MAX_AGENTS;
  103. packet->mad.id++)
  104. if (agent == file->agent[packet->mad.id]) {
  105. spin_lock_irq(&file->recv_lock);
  106. list_add_tail(&packet->list, &file->recv_list);
  107. spin_unlock_irq(&file->recv_lock);
  108. wake_up_interruptible(&file->recv_wait);
  109. ret = 0;
  110. break;
  111. }
  112. up_read(&file->agent_mutex);
  113. return ret;
  114. }
  115. static void send_handler(struct ib_mad_agent *agent,
  116. struct ib_mad_send_wc *send_wc)
  117. {
  118. struct ib_umad_file *file = agent->context;
  119. struct ib_umad_packet *packet =
  120. (void *) (unsigned long) send_wc->wr_id;
  121. dma_unmap_single(agent->device->dma_device,
  122. pci_unmap_addr(packet, mapping),
  123. sizeof packet->mad.data,
  124. DMA_TO_DEVICE);
  125. ib_destroy_ah(packet->ah);
  126. if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
  127. packet->mad.status = ETIMEDOUT;
  128. if (!queue_packet(file, agent, packet))
  129. return;
  130. }
  131. kfree(packet);
  132. }
  133. static void recv_handler(struct ib_mad_agent *agent,
  134. struct ib_mad_recv_wc *mad_recv_wc)
  135. {
  136. struct ib_umad_file *file = agent->context;
  137. struct ib_umad_packet *packet;
  138. if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
  139. goto out;
  140. packet = kmalloc(sizeof *packet, GFP_KERNEL);
  141. if (!packet)
  142. goto out;
  143. memset(packet, 0, sizeof *packet);
  144. memcpy(packet->mad.data, mad_recv_wc->recv_buf.mad, sizeof packet->mad.data);
  145. packet->mad.status = 0;
  146. packet->mad.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
  147. packet->mad.lid = cpu_to_be16(mad_recv_wc->wc->slid);
  148. packet->mad.sl = mad_recv_wc->wc->sl;
  149. packet->mad.path_bits = mad_recv_wc->wc->dlid_path_bits;
  150. packet->mad.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
  151. if (packet->mad.grh_present) {
  152. /* XXX parse GRH */
  153. packet->mad.gid_index = 0;
  154. packet->mad.hop_limit = 0;
  155. packet->mad.traffic_class = 0;
  156. memset(packet->mad.gid, 0, 16);
  157. packet->mad.flow_label = 0;
  158. }
  159. if (queue_packet(file, agent, packet))
  160. kfree(packet);
  161. out:
  162. ib_free_recv_mad(mad_recv_wc);
  163. }
  164. static ssize_t ib_umad_read(struct file *filp, char __user *buf,
  165. size_t count, loff_t *pos)
  166. {
  167. struct ib_umad_file *file = filp->private_data;
  168. struct ib_umad_packet *packet;
  169. ssize_t ret;
  170. if (count < sizeof (struct ib_user_mad))
  171. return -EINVAL;
  172. spin_lock_irq(&file->recv_lock);
  173. while (list_empty(&file->recv_list)) {
  174. spin_unlock_irq(&file->recv_lock);
  175. if (filp->f_flags & O_NONBLOCK)
  176. return -EAGAIN;
  177. if (wait_event_interruptible(file->recv_wait,
  178. !list_empty(&file->recv_list)))
  179. return -ERESTARTSYS;
  180. spin_lock_irq(&file->recv_lock);
  181. }
  182. packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
  183. list_del(&packet->list);
  184. spin_unlock_irq(&file->recv_lock);
  185. if (copy_to_user(buf, &packet->mad, sizeof packet->mad))
  186. ret = -EFAULT;
  187. else
  188. ret = sizeof packet->mad;
  189. kfree(packet);
  190. return ret;
  191. }
  192. static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
  193. size_t count, loff_t *pos)
  194. {
  195. struct ib_umad_file *file = filp->private_data;
  196. struct ib_umad_packet *packet;
  197. struct ib_mad_agent *agent;
  198. struct ib_ah_attr ah_attr;
  199. struct ib_sge gather_list;
  200. struct ib_send_wr *bad_wr, wr = {
  201. .opcode = IB_WR_SEND,
  202. .sg_list = &gather_list,
  203. .num_sge = 1,
  204. .send_flags = IB_SEND_SIGNALED,
  205. };
  206. u8 method;
  207. u64 *tid;
  208. int ret;
  209. if (count < sizeof (struct ib_user_mad))
  210. return -EINVAL;
  211. packet = kmalloc(sizeof *packet, GFP_KERNEL);
  212. if (!packet)
  213. return -ENOMEM;
  214. if (copy_from_user(&packet->mad, buf, sizeof packet->mad)) {
  215. kfree(packet);
  216. return -EFAULT;
  217. }
  218. if (packet->mad.id < 0 || packet->mad.id >= IB_UMAD_MAX_AGENTS) {
  219. ret = -EINVAL;
  220. goto err;
  221. }
  222. down_read(&file->agent_mutex);
  223. agent = file->agent[packet->mad.id];
  224. if (!agent) {
  225. ret = -EINVAL;
  226. goto err_up;
  227. }
  228. /*
  229. * If userspace is generating a request that will generate a
  230. * response, we need to make sure the high-order part of the
  231. * transaction ID matches the agent being used to send the
  232. * MAD.
  233. */
  234. method = ((struct ib_mad_hdr *) packet->mad.data)->method;
  235. if (!(method & IB_MGMT_METHOD_RESP) &&
  236. method != IB_MGMT_METHOD_TRAP_REPRESS &&
  237. method != IB_MGMT_METHOD_SEND) {
  238. tid = &((struct ib_mad_hdr *) packet->mad.data)->tid;
  239. *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
  240. (be64_to_cpup(tid) & 0xffffffff));
  241. }
  242. memset(&ah_attr, 0, sizeof ah_attr);
  243. ah_attr.dlid = be16_to_cpu(packet->mad.lid);
  244. ah_attr.sl = packet->mad.sl;
  245. ah_attr.src_path_bits = packet->mad.path_bits;
  246. ah_attr.port_num = file->port->port_num;
  247. if (packet->mad.grh_present) {
  248. ah_attr.ah_flags = IB_AH_GRH;
  249. memcpy(ah_attr.grh.dgid.raw, packet->mad.gid, 16);
  250. ah_attr.grh.flow_label = packet->mad.flow_label;
  251. ah_attr.grh.hop_limit = packet->mad.hop_limit;
  252. ah_attr.grh.traffic_class = packet->mad.traffic_class;
  253. }
  254. packet->ah = ib_create_ah(agent->qp->pd, &ah_attr);
  255. if (IS_ERR(packet->ah)) {
  256. ret = PTR_ERR(packet->ah);
  257. goto err_up;
  258. }
  259. gather_list.addr = dma_map_single(agent->device->dma_device,
  260. packet->mad.data,
  261. sizeof packet->mad.data,
  262. DMA_TO_DEVICE);
  263. gather_list.length = sizeof packet->mad.data;
  264. gather_list.lkey = file->mr[packet->mad.id]->lkey;
  265. pci_unmap_addr_set(packet, mapping, gather_list.addr);
  266. wr.wr.ud.mad_hdr = (struct ib_mad_hdr *) packet->mad.data;
  267. wr.wr.ud.ah = packet->ah;
  268. wr.wr.ud.remote_qpn = be32_to_cpu(packet->mad.qpn);
  269. wr.wr.ud.remote_qkey = be32_to_cpu(packet->mad.qkey);
  270. wr.wr.ud.timeout_ms = packet->mad.timeout_ms;
  271. wr.wr_id = (unsigned long) packet;
  272. ret = ib_post_send_mad(agent, &wr, &bad_wr);
  273. if (ret) {
  274. dma_unmap_single(agent->device->dma_device,
  275. pci_unmap_addr(packet, mapping),
  276. sizeof packet->mad.data,
  277. DMA_TO_DEVICE);
  278. goto err_up;
  279. }
  280. up_read(&file->agent_mutex);
  281. return sizeof packet->mad;
  282. err_up:
  283. up_read(&file->agent_mutex);
  284. err:
  285. kfree(packet);
  286. return ret;
  287. }
  288. static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
  289. {
  290. struct ib_umad_file *file = filp->private_data;
  291. /* we will always be able to post a MAD send */
  292. unsigned int mask = POLLOUT | POLLWRNORM;
  293. poll_wait(filp, &file->recv_wait, wait);
  294. if (!list_empty(&file->recv_list))
  295. mask |= POLLIN | POLLRDNORM;
  296. return mask;
  297. }
  298. static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
  299. {
  300. struct ib_user_mad_reg_req ureq;
  301. struct ib_mad_reg_req req;
  302. struct ib_mad_agent *agent;
  303. int agent_id;
  304. int ret;
  305. down_write(&file->agent_mutex);
  306. if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) {
  307. ret = -EFAULT;
  308. goto out;
  309. }
  310. if (ureq.qpn != 0 && ureq.qpn != 1) {
  311. ret = -EINVAL;
  312. goto out;
  313. }
  314. for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
  315. if (!file->agent[agent_id])
  316. goto found;
  317. ret = -ENOMEM;
  318. goto out;
  319. found:
  320. if (ureq.mgmt_class) {
  321. req.mgmt_class = ureq.mgmt_class;
  322. req.mgmt_class_version = ureq.mgmt_class_version;
  323. memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask);
  324. memcpy(req.oui, ureq.oui, sizeof req.oui);
  325. }
  326. agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
  327. ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
  328. ureq.mgmt_class ? &req : NULL,
  329. 0, send_handler, recv_handler, file);
  330. if (IS_ERR(agent)) {
  331. ret = PTR_ERR(agent);
  332. goto out;
  333. }
  334. file->agent[agent_id] = agent;
  335. file->mr[agent_id] = ib_get_dma_mr(agent->qp->pd, IB_ACCESS_LOCAL_WRITE);
  336. if (IS_ERR(file->mr[agent_id])) {
  337. ret = -ENOMEM;
  338. goto err;
  339. }
  340. if (put_user(agent_id,
  341. (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
  342. ret = -EFAULT;
  343. goto err_mr;
  344. }
  345. ret = 0;
  346. goto out;
  347. err_mr:
  348. ib_dereg_mr(file->mr[agent_id]);
  349. err:
  350. file->agent[agent_id] = NULL;
  351. ib_unregister_mad_agent(agent);
  352. out:
  353. up_write(&file->agent_mutex);
  354. return ret;
  355. }
  356. static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
  357. {
  358. u32 id;
  359. int ret = 0;
  360. down_write(&file->agent_mutex);
  361. if (get_user(id, (u32 __user *) arg)) {
  362. ret = -EFAULT;
  363. goto out;
  364. }
  365. if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !file->agent[id]) {
  366. ret = -EINVAL;
  367. goto out;
  368. }
  369. ib_dereg_mr(file->mr[id]);
  370. ib_unregister_mad_agent(file->agent[id]);
  371. file->agent[id] = NULL;
  372. out:
  373. up_write(&file->agent_mutex);
  374. return ret;
  375. }
  376. static long ib_umad_ioctl(struct file *filp,
  377. unsigned int cmd, unsigned long arg)
  378. {
  379. switch (cmd) {
  380. case IB_USER_MAD_REGISTER_AGENT:
  381. return ib_umad_reg_agent(filp->private_data, arg);
  382. case IB_USER_MAD_UNREGISTER_AGENT:
  383. return ib_umad_unreg_agent(filp->private_data, arg);
  384. default:
  385. return -ENOIOCTLCMD;
  386. }
  387. }
  388. static int ib_umad_open(struct inode *inode, struct file *filp)
  389. {
  390. struct ib_umad_port *port =
  391. container_of(inode->i_cdev, struct ib_umad_port, dev);
  392. struct ib_umad_file *file;
  393. file = kmalloc(sizeof *file, GFP_KERNEL);
  394. if (!file)
  395. return -ENOMEM;
  396. memset(file, 0, sizeof *file);
  397. spin_lock_init(&file->recv_lock);
  398. init_rwsem(&file->agent_mutex);
  399. INIT_LIST_HEAD(&file->recv_list);
  400. init_waitqueue_head(&file->recv_wait);
  401. file->port = port;
  402. filp->private_data = file;
  403. return 0;
  404. }
  405. static int ib_umad_close(struct inode *inode, struct file *filp)
  406. {
  407. struct ib_umad_file *file = filp->private_data;
  408. struct ib_umad_packet *packet, *tmp;
  409. int i;
  410. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  411. if (file->agent[i]) {
  412. ib_dereg_mr(file->mr[i]);
  413. ib_unregister_mad_agent(file->agent[i]);
  414. }
  415. list_for_each_entry_safe(packet, tmp, &file->recv_list, list)
  416. kfree(packet);
  417. kfree(file);
  418. return 0;
  419. }
  420. static struct file_operations umad_fops = {
  421. .owner = THIS_MODULE,
  422. .read = ib_umad_read,
  423. .write = ib_umad_write,
  424. .poll = ib_umad_poll,
  425. .unlocked_ioctl = ib_umad_ioctl,
  426. .compat_ioctl = ib_umad_ioctl,
  427. .open = ib_umad_open,
  428. .release = ib_umad_close
  429. };
  430. static int ib_umad_sm_open(struct inode *inode, struct file *filp)
  431. {
  432. struct ib_umad_port *port =
  433. container_of(inode->i_cdev, struct ib_umad_port, sm_dev);
  434. struct ib_port_modify props = {
  435. .set_port_cap_mask = IB_PORT_SM
  436. };
  437. int ret;
  438. if (filp->f_flags & O_NONBLOCK) {
  439. if (down_trylock(&port->sm_sem))
  440. return -EAGAIN;
  441. } else {
  442. if (down_interruptible(&port->sm_sem))
  443. return -ERESTARTSYS;
  444. }
  445. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  446. if (ret) {
  447. up(&port->sm_sem);
  448. return ret;
  449. }
  450. filp->private_data = port;
  451. return 0;
  452. }
  453. static int ib_umad_sm_close(struct inode *inode, struct file *filp)
  454. {
  455. struct ib_umad_port *port = filp->private_data;
  456. struct ib_port_modify props = {
  457. .clr_port_cap_mask = IB_PORT_SM
  458. };
  459. int ret;
  460. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  461. up(&port->sm_sem);
  462. return ret;
  463. }
  464. static struct file_operations umad_sm_fops = {
  465. .owner = THIS_MODULE,
  466. .open = ib_umad_sm_open,
  467. .release = ib_umad_sm_close
  468. };
  469. static struct ib_client umad_client = {
  470. .name = "umad",
  471. .add = ib_umad_add_one,
  472. .remove = ib_umad_remove_one
  473. };
  474. static ssize_t show_dev(struct class_device *class_dev, char *buf)
  475. {
  476. struct ib_umad_port *port = class_get_devdata(class_dev);
  477. if (class_dev == &port->class_dev)
  478. return print_dev_t(buf, port->dev.dev);
  479. else
  480. return print_dev_t(buf, port->sm_dev.dev);
  481. }
  482. static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL);
  483. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  484. {
  485. struct ib_umad_port *port = class_get_devdata(class_dev);
  486. return sprintf(buf, "%s\n", port->ib_dev->name);
  487. }
  488. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  489. static ssize_t show_port(struct class_device *class_dev, char *buf)
  490. {
  491. struct ib_umad_port *port = class_get_devdata(class_dev);
  492. return sprintf(buf, "%d\n", port->port_num);
  493. }
  494. static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
  495. static void ib_umad_release_dev(struct kref *ref)
  496. {
  497. struct ib_umad_device *dev =
  498. container_of(ref, struct ib_umad_device, ref);
  499. kfree(dev);
  500. }
  501. static void ib_umad_release_port(struct class_device *class_dev)
  502. {
  503. struct ib_umad_port *port = class_get_devdata(class_dev);
  504. if (class_dev == &port->class_dev) {
  505. cdev_del(&port->dev);
  506. clear_bit(port->devnum, dev_map);
  507. } else {
  508. cdev_del(&port->sm_dev);
  509. clear_bit(port->sm_devnum, dev_map);
  510. }
  511. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  512. }
  513. static struct class umad_class = {
  514. .name = "infiniband_mad",
  515. .release = ib_umad_release_port
  516. };
  517. static ssize_t show_abi_version(struct class *class, char *buf)
  518. {
  519. return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
  520. }
  521. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  522. static int ib_umad_init_port(struct ib_device *device, int port_num,
  523. struct ib_umad_port *port)
  524. {
  525. spin_lock(&map_lock);
  526. port->devnum = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
  527. if (port->devnum >= IB_UMAD_MAX_PORTS) {
  528. spin_unlock(&map_lock);
  529. return -1;
  530. }
  531. port->sm_devnum = find_next_zero_bit(dev_map, IB_UMAD_MAX_PORTS * 2, IB_UMAD_MAX_PORTS);
  532. if (port->sm_devnum >= IB_UMAD_MAX_PORTS * 2) {
  533. spin_unlock(&map_lock);
  534. return -1;
  535. }
  536. set_bit(port->devnum, dev_map);
  537. set_bit(port->sm_devnum, dev_map);
  538. spin_unlock(&map_lock);
  539. port->ib_dev = device;
  540. port->port_num = port_num;
  541. init_MUTEX(&port->sm_sem);
  542. cdev_init(&port->dev, &umad_fops);
  543. port->dev.owner = THIS_MODULE;
  544. kobject_set_name(&port->dev.kobj, "umad%d", port->devnum);
  545. if (cdev_add(&port->dev, base_dev + port->devnum, 1))
  546. return -1;
  547. port->class_dev.class = &umad_class;
  548. port->class_dev.dev = device->dma_device;
  549. snprintf(port->class_dev.class_id, BUS_ID_SIZE, "umad%d", port->devnum);
  550. if (class_device_register(&port->class_dev))
  551. goto err_cdev;
  552. class_set_devdata(&port->class_dev, port);
  553. kref_get(&port->umad_dev->ref);
  554. if (class_device_create_file(&port->class_dev, &class_device_attr_dev))
  555. goto err_class;
  556. if (class_device_create_file(&port->class_dev, &class_device_attr_ibdev))
  557. goto err_class;
  558. if (class_device_create_file(&port->class_dev, &class_device_attr_port))
  559. goto err_class;
  560. cdev_init(&port->sm_dev, &umad_sm_fops);
  561. port->sm_dev.owner = THIS_MODULE;
  562. kobject_set_name(&port->dev.kobj, "issm%d", port->sm_devnum - IB_UMAD_MAX_PORTS);
  563. if (cdev_add(&port->sm_dev, base_dev + port->sm_devnum, 1))
  564. return -1;
  565. port->sm_class_dev.class = &umad_class;
  566. port->sm_class_dev.dev = device->dma_device;
  567. snprintf(port->sm_class_dev.class_id, BUS_ID_SIZE, "issm%d", port->sm_devnum - IB_UMAD_MAX_PORTS);
  568. if (class_device_register(&port->sm_class_dev))
  569. goto err_sm_cdev;
  570. class_set_devdata(&port->sm_class_dev, port);
  571. kref_get(&port->umad_dev->ref);
  572. if (class_device_create_file(&port->sm_class_dev, &class_device_attr_dev))
  573. goto err_sm_class;
  574. if (class_device_create_file(&port->sm_class_dev, &class_device_attr_ibdev))
  575. goto err_sm_class;
  576. if (class_device_create_file(&port->sm_class_dev, &class_device_attr_port))
  577. goto err_sm_class;
  578. return 0;
  579. err_sm_class:
  580. class_device_unregister(&port->sm_class_dev);
  581. err_sm_cdev:
  582. cdev_del(&port->sm_dev);
  583. err_class:
  584. class_device_unregister(&port->class_dev);
  585. err_cdev:
  586. cdev_del(&port->dev);
  587. clear_bit(port->devnum, dev_map);
  588. return -1;
  589. }
  590. static void ib_umad_add_one(struct ib_device *device)
  591. {
  592. struct ib_umad_device *umad_dev;
  593. int s, e, i;
  594. if (device->node_type == IB_NODE_SWITCH)
  595. s = e = 0;
  596. else {
  597. s = 1;
  598. e = device->phys_port_cnt;
  599. }
  600. umad_dev = kmalloc(sizeof *umad_dev +
  601. (e - s + 1) * sizeof (struct ib_umad_port),
  602. GFP_KERNEL);
  603. if (!umad_dev)
  604. return;
  605. memset(umad_dev, 0, sizeof *umad_dev +
  606. (e - s + 1) * sizeof (struct ib_umad_port));
  607. kref_init(&umad_dev->ref);
  608. umad_dev->start_port = s;
  609. umad_dev->end_port = e;
  610. for (i = s; i <= e; ++i) {
  611. umad_dev->port[i - s].umad_dev = umad_dev;
  612. if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
  613. goto err;
  614. }
  615. ib_set_client_data(device, &umad_client, umad_dev);
  616. return;
  617. err:
  618. while (--i >= s) {
  619. class_device_unregister(&umad_dev->port[i - s].class_dev);
  620. class_device_unregister(&umad_dev->port[i - s].sm_class_dev);
  621. }
  622. kref_put(&umad_dev->ref, ib_umad_release_dev);
  623. }
  624. static void ib_umad_remove_one(struct ib_device *device)
  625. {
  626. struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
  627. int i;
  628. if (!umad_dev)
  629. return;
  630. for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i) {
  631. class_device_unregister(&umad_dev->port[i].class_dev);
  632. class_device_unregister(&umad_dev->port[i].sm_class_dev);
  633. }
  634. kref_put(&umad_dev->ref, ib_umad_release_dev);
  635. }
  636. static int __init ib_umad_init(void)
  637. {
  638. int ret;
  639. spin_lock_init(&map_lock);
  640. ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
  641. "infiniband_mad");
  642. if (ret) {
  643. printk(KERN_ERR "user_mad: couldn't register device number\n");
  644. goto out;
  645. }
  646. ret = class_register(&umad_class);
  647. if (ret) {
  648. printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
  649. goto out_chrdev;
  650. }
  651. ret = class_create_file(&umad_class, &class_attr_abi_version);
  652. if (ret) {
  653. printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
  654. goto out_class;
  655. }
  656. ret = ib_register_client(&umad_client);
  657. if (ret) {
  658. printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
  659. goto out_class;
  660. }
  661. return 0;
  662. out_class:
  663. class_unregister(&umad_class);
  664. out_chrdev:
  665. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  666. out:
  667. return ret;
  668. }
  669. static void __exit ib_umad_cleanup(void)
  670. {
  671. ib_unregister_client(&umad_client);
  672. class_unregister(&umad_class);
  673. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  674. }
  675. module_init(ib_umad_init);
  676. module_exit(ib_umad_cleanup);