user_mad.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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.ud.retries = 0;
  272. wr.wr_id = (unsigned long) packet;
  273. ret = ib_post_send_mad(agent, &wr, &bad_wr);
  274. if (ret) {
  275. dma_unmap_single(agent->device->dma_device,
  276. pci_unmap_addr(packet, mapping),
  277. sizeof packet->mad.data,
  278. DMA_TO_DEVICE);
  279. goto err_up;
  280. }
  281. up_read(&file->agent_mutex);
  282. return sizeof packet->mad;
  283. err_up:
  284. up_read(&file->agent_mutex);
  285. err:
  286. kfree(packet);
  287. return ret;
  288. }
  289. static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
  290. {
  291. struct ib_umad_file *file = filp->private_data;
  292. /* we will always be able to post a MAD send */
  293. unsigned int mask = POLLOUT | POLLWRNORM;
  294. poll_wait(filp, &file->recv_wait, wait);
  295. if (!list_empty(&file->recv_list))
  296. mask |= POLLIN | POLLRDNORM;
  297. return mask;
  298. }
  299. static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
  300. {
  301. struct ib_user_mad_reg_req ureq;
  302. struct ib_mad_reg_req req;
  303. struct ib_mad_agent *agent;
  304. int agent_id;
  305. int ret;
  306. down_write(&file->agent_mutex);
  307. if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) {
  308. ret = -EFAULT;
  309. goto out;
  310. }
  311. if (ureq.qpn != 0 && ureq.qpn != 1) {
  312. ret = -EINVAL;
  313. goto out;
  314. }
  315. for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
  316. if (!file->agent[agent_id])
  317. goto found;
  318. ret = -ENOMEM;
  319. goto out;
  320. found:
  321. if (ureq.mgmt_class) {
  322. req.mgmt_class = ureq.mgmt_class;
  323. req.mgmt_class_version = ureq.mgmt_class_version;
  324. memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask);
  325. memcpy(req.oui, ureq.oui, sizeof req.oui);
  326. }
  327. agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
  328. ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
  329. ureq.mgmt_class ? &req : NULL,
  330. 0, send_handler, recv_handler, file);
  331. if (IS_ERR(agent)) {
  332. ret = PTR_ERR(agent);
  333. goto out;
  334. }
  335. file->agent[agent_id] = agent;
  336. file->mr[agent_id] = ib_get_dma_mr(agent->qp->pd, IB_ACCESS_LOCAL_WRITE);
  337. if (IS_ERR(file->mr[agent_id])) {
  338. ret = -ENOMEM;
  339. goto err;
  340. }
  341. if (put_user(agent_id,
  342. (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
  343. ret = -EFAULT;
  344. goto err_mr;
  345. }
  346. ret = 0;
  347. goto out;
  348. err_mr:
  349. ib_dereg_mr(file->mr[agent_id]);
  350. err:
  351. file->agent[agent_id] = NULL;
  352. ib_unregister_mad_agent(agent);
  353. out:
  354. up_write(&file->agent_mutex);
  355. return ret;
  356. }
  357. static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
  358. {
  359. u32 id;
  360. int ret = 0;
  361. down_write(&file->agent_mutex);
  362. if (get_user(id, (u32 __user *) arg)) {
  363. ret = -EFAULT;
  364. goto out;
  365. }
  366. if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !file->agent[id]) {
  367. ret = -EINVAL;
  368. goto out;
  369. }
  370. ib_dereg_mr(file->mr[id]);
  371. ib_unregister_mad_agent(file->agent[id]);
  372. file->agent[id] = NULL;
  373. out:
  374. up_write(&file->agent_mutex);
  375. return ret;
  376. }
  377. static long ib_umad_ioctl(struct file *filp,
  378. unsigned int cmd, unsigned long arg)
  379. {
  380. switch (cmd) {
  381. case IB_USER_MAD_REGISTER_AGENT:
  382. return ib_umad_reg_agent(filp->private_data, arg);
  383. case IB_USER_MAD_UNREGISTER_AGENT:
  384. return ib_umad_unreg_agent(filp->private_data, arg);
  385. default:
  386. return -ENOIOCTLCMD;
  387. }
  388. }
  389. static int ib_umad_open(struct inode *inode, struct file *filp)
  390. {
  391. struct ib_umad_port *port =
  392. container_of(inode->i_cdev, struct ib_umad_port, dev);
  393. struct ib_umad_file *file;
  394. file = kmalloc(sizeof *file, GFP_KERNEL);
  395. if (!file)
  396. return -ENOMEM;
  397. memset(file, 0, sizeof *file);
  398. spin_lock_init(&file->recv_lock);
  399. init_rwsem(&file->agent_mutex);
  400. INIT_LIST_HEAD(&file->recv_list);
  401. init_waitqueue_head(&file->recv_wait);
  402. file->port = port;
  403. filp->private_data = file;
  404. return 0;
  405. }
  406. static int ib_umad_close(struct inode *inode, struct file *filp)
  407. {
  408. struct ib_umad_file *file = filp->private_data;
  409. struct ib_umad_packet *packet, *tmp;
  410. int i;
  411. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  412. if (file->agent[i]) {
  413. ib_dereg_mr(file->mr[i]);
  414. ib_unregister_mad_agent(file->agent[i]);
  415. }
  416. list_for_each_entry_safe(packet, tmp, &file->recv_list, list)
  417. kfree(packet);
  418. kfree(file);
  419. return 0;
  420. }
  421. static struct file_operations umad_fops = {
  422. .owner = THIS_MODULE,
  423. .read = ib_umad_read,
  424. .write = ib_umad_write,
  425. .poll = ib_umad_poll,
  426. .unlocked_ioctl = ib_umad_ioctl,
  427. .compat_ioctl = ib_umad_ioctl,
  428. .open = ib_umad_open,
  429. .release = ib_umad_close
  430. };
  431. static int ib_umad_sm_open(struct inode *inode, struct file *filp)
  432. {
  433. struct ib_umad_port *port =
  434. container_of(inode->i_cdev, struct ib_umad_port, sm_dev);
  435. struct ib_port_modify props = {
  436. .set_port_cap_mask = IB_PORT_SM
  437. };
  438. int ret;
  439. if (filp->f_flags & O_NONBLOCK) {
  440. if (down_trylock(&port->sm_sem))
  441. return -EAGAIN;
  442. } else {
  443. if (down_interruptible(&port->sm_sem))
  444. return -ERESTARTSYS;
  445. }
  446. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  447. if (ret) {
  448. up(&port->sm_sem);
  449. return ret;
  450. }
  451. filp->private_data = port;
  452. return 0;
  453. }
  454. static int ib_umad_sm_close(struct inode *inode, struct file *filp)
  455. {
  456. struct ib_umad_port *port = filp->private_data;
  457. struct ib_port_modify props = {
  458. .clr_port_cap_mask = IB_PORT_SM
  459. };
  460. int ret;
  461. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  462. up(&port->sm_sem);
  463. return ret;
  464. }
  465. static struct file_operations umad_sm_fops = {
  466. .owner = THIS_MODULE,
  467. .open = ib_umad_sm_open,
  468. .release = ib_umad_sm_close
  469. };
  470. static struct ib_client umad_client = {
  471. .name = "umad",
  472. .add = ib_umad_add_one,
  473. .remove = ib_umad_remove_one
  474. };
  475. static ssize_t show_dev(struct class_device *class_dev, char *buf)
  476. {
  477. struct ib_umad_port *port = class_get_devdata(class_dev);
  478. if (class_dev == &port->class_dev)
  479. return print_dev_t(buf, port->dev.dev);
  480. else
  481. return print_dev_t(buf, port->sm_dev.dev);
  482. }
  483. static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL);
  484. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  485. {
  486. struct ib_umad_port *port = class_get_devdata(class_dev);
  487. return sprintf(buf, "%s\n", port->ib_dev->name);
  488. }
  489. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  490. static ssize_t show_port(struct class_device *class_dev, char *buf)
  491. {
  492. struct ib_umad_port *port = class_get_devdata(class_dev);
  493. return sprintf(buf, "%d\n", port->port_num);
  494. }
  495. static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
  496. static void ib_umad_release_dev(struct kref *ref)
  497. {
  498. struct ib_umad_device *dev =
  499. container_of(ref, struct ib_umad_device, ref);
  500. kfree(dev);
  501. }
  502. static void ib_umad_release_port(struct class_device *class_dev)
  503. {
  504. struct ib_umad_port *port = class_get_devdata(class_dev);
  505. if (class_dev == &port->class_dev) {
  506. cdev_del(&port->dev);
  507. clear_bit(port->devnum, dev_map);
  508. } else {
  509. cdev_del(&port->sm_dev);
  510. clear_bit(port->sm_devnum, dev_map);
  511. }
  512. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  513. }
  514. static struct class umad_class = {
  515. .name = "infiniband_mad",
  516. .release = ib_umad_release_port
  517. };
  518. static ssize_t show_abi_version(struct class *class, char *buf)
  519. {
  520. return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
  521. }
  522. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  523. static int ib_umad_init_port(struct ib_device *device, int port_num,
  524. struct ib_umad_port *port)
  525. {
  526. spin_lock(&map_lock);
  527. port->devnum = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
  528. if (port->devnum >= IB_UMAD_MAX_PORTS) {
  529. spin_unlock(&map_lock);
  530. return -1;
  531. }
  532. port->sm_devnum = find_next_zero_bit(dev_map, IB_UMAD_MAX_PORTS * 2, IB_UMAD_MAX_PORTS);
  533. if (port->sm_devnum >= IB_UMAD_MAX_PORTS * 2) {
  534. spin_unlock(&map_lock);
  535. return -1;
  536. }
  537. set_bit(port->devnum, dev_map);
  538. set_bit(port->sm_devnum, dev_map);
  539. spin_unlock(&map_lock);
  540. port->ib_dev = device;
  541. port->port_num = port_num;
  542. init_MUTEX(&port->sm_sem);
  543. cdev_init(&port->dev, &umad_fops);
  544. port->dev.owner = THIS_MODULE;
  545. kobject_set_name(&port->dev.kobj, "umad%d", port->devnum);
  546. if (cdev_add(&port->dev, base_dev + port->devnum, 1))
  547. return -1;
  548. port->class_dev.class = &umad_class;
  549. port->class_dev.dev = device->dma_device;
  550. snprintf(port->class_dev.class_id, BUS_ID_SIZE, "umad%d", port->devnum);
  551. if (class_device_register(&port->class_dev))
  552. goto err_cdev;
  553. class_set_devdata(&port->class_dev, port);
  554. kref_get(&port->umad_dev->ref);
  555. if (class_device_create_file(&port->class_dev, &class_device_attr_dev))
  556. goto err_class;
  557. if (class_device_create_file(&port->class_dev, &class_device_attr_ibdev))
  558. goto err_class;
  559. if (class_device_create_file(&port->class_dev, &class_device_attr_port))
  560. goto err_class;
  561. cdev_init(&port->sm_dev, &umad_sm_fops);
  562. port->sm_dev.owner = THIS_MODULE;
  563. kobject_set_name(&port->dev.kobj, "issm%d", port->sm_devnum - IB_UMAD_MAX_PORTS);
  564. if (cdev_add(&port->sm_dev, base_dev + port->sm_devnum, 1))
  565. return -1;
  566. port->sm_class_dev.class = &umad_class;
  567. port->sm_class_dev.dev = device->dma_device;
  568. snprintf(port->sm_class_dev.class_id, BUS_ID_SIZE, "issm%d", port->sm_devnum - IB_UMAD_MAX_PORTS);
  569. if (class_device_register(&port->sm_class_dev))
  570. goto err_sm_cdev;
  571. class_set_devdata(&port->sm_class_dev, port);
  572. kref_get(&port->umad_dev->ref);
  573. if (class_device_create_file(&port->sm_class_dev, &class_device_attr_dev))
  574. goto err_sm_class;
  575. if (class_device_create_file(&port->sm_class_dev, &class_device_attr_ibdev))
  576. goto err_sm_class;
  577. if (class_device_create_file(&port->sm_class_dev, &class_device_attr_port))
  578. goto err_sm_class;
  579. return 0;
  580. err_sm_class:
  581. class_device_unregister(&port->sm_class_dev);
  582. err_sm_cdev:
  583. cdev_del(&port->sm_dev);
  584. err_class:
  585. class_device_unregister(&port->class_dev);
  586. err_cdev:
  587. cdev_del(&port->dev);
  588. clear_bit(port->devnum, dev_map);
  589. return -1;
  590. }
  591. static void ib_umad_add_one(struct ib_device *device)
  592. {
  593. struct ib_umad_device *umad_dev;
  594. int s, e, i;
  595. if (device->node_type == IB_NODE_SWITCH)
  596. s = e = 0;
  597. else {
  598. s = 1;
  599. e = device->phys_port_cnt;
  600. }
  601. umad_dev = kmalloc(sizeof *umad_dev +
  602. (e - s + 1) * sizeof (struct ib_umad_port),
  603. GFP_KERNEL);
  604. if (!umad_dev)
  605. return;
  606. memset(umad_dev, 0, sizeof *umad_dev +
  607. (e - s + 1) * sizeof (struct ib_umad_port));
  608. kref_init(&umad_dev->ref);
  609. umad_dev->start_port = s;
  610. umad_dev->end_port = e;
  611. for (i = s; i <= e; ++i) {
  612. umad_dev->port[i - s].umad_dev = umad_dev;
  613. if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
  614. goto err;
  615. }
  616. ib_set_client_data(device, &umad_client, umad_dev);
  617. return;
  618. err:
  619. while (--i >= s) {
  620. class_device_unregister(&umad_dev->port[i - s].class_dev);
  621. class_device_unregister(&umad_dev->port[i - s].sm_class_dev);
  622. }
  623. kref_put(&umad_dev->ref, ib_umad_release_dev);
  624. }
  625. static void ib_umad_remove_one(struct ib_device *device)
  626. {
  627. struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
  628. int i;
  629. if (!umad_dev)
  630. return;
  631. for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i) {
  632. class_device_unregister(&umad_dev->port[i].class_dev);
  633. class_device_unregister(&umad_dev->port[i].sm_class_dev);
  634. }
  635. kref_put(&umad_dev->ref, ib_umad_release_dev);
  636. }
  637. static int __init ib_umad_init(void)
  638. {
  639. int ret;
  640. spin_lock_init(&map_lock);
  641. ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
  642. "infiniband_mad");
  643. if (ret) {
  644. printk(KERN_ERR "user_mad: couldn't register device number\n");
  645. goto out;
  646. }
  647. ret = class_register(&umad_class);
  648. if (ret) {
  649. printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
  650. goto out_chrdev;
  651. }
  652. ret = class_create_file(&umad_class, &class_attr_abi_version);
  653. if (ret) {
  654. printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
  655. goto out_class;
  656. }
  657. ret = ib_register_client(&umad_client);
  658. if (ret) {
  659. printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
  660. goto out_class;
  661. }
  662. return 0;
  663. out_class:
  664. class_unregister(&umad_class);
  665. out_chrdev:
  666. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  667. out:
  668. return ret;
  669. }
  670. static void __exit ib_umad_cleanup(void)
  671. {
  672. ib_unregister_client(&umad_client);
  673. class_unregister(&umad_class);
  674. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  675. }
  676. module_init(ib_umad_init);
  677. module_exit(ib_umad_cleanup);