user_mad.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. int i;
  409. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  410. if (file->agent[i]) {
  411. ib_dereg_mr(file->mr[i]);
  412. ib_unregister_mad_agent(file->agent[i]);
  413. }
  414. kfree(file);
  415. return 0;
  416. }
  417. static struct file_operations umad_fops = {
  418. .owner = THIS_MODULE,
  419. .read = ib_umad_read,
  420. .write = ib_umad_write,
  421. .poll = ib_umad_poll,
  422. .unlocked_ioctl = ib_umad_ioctl,
  423. .compat_ioctl = ib_umad_ioctl,
  424. .open = ib_umad_open,
  425. .release = ib_umad_close
  426. };
  427. static int ib_umad_sm_open(struct inode *inode, struct file *filp)
  428. {
  429. struct ib_umad_port *port =
  430. container_of(inode->i_cdev, struct ib_umad_port, sm_dev);
  431. struct ib_port_modify props = {
  432. .set_port_cap_mask = IB_PORT_SM
  433. };
  434. int ret;
  435. if (filp->f_flags & O_NONBLOCK) {
  436. if (down_trylock(&port->sm_sem))
  437. return -EAGAIN;
  438. } else {
  439. if (down_interruptible(&port->sm_sem))
  440. return -ERESTARTSYS;
  441. }
  442. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  443. if (ret) {
  444. up(&port->sm_sem);
  445. return ret;
  446. }
  447. filp->private_data = port;
  448. return 0;
  449. }
  450. static int ib_umad_sm_close(struct inode *inode, struct file *filp)
  451. {
  452. struct ib_umad_port *port = filp->private_data;
  453. struct ib_port_modify props = {
  454. .clr_port_cap_mask = IB_PORT_SM
  455. };
  456. int ret;
  457. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  458. up(&port->sm_sem);
  459. return ret;
  460. }
  461. static struct file_operations umad_sm_fops = {
  462. .owner = THIS_MODULE,
  463. .open = ib_umad_sm_open,
  464. .release = ib_umad_sm_close
  465. };
  466. static struct ib_client umad_client = {
  467. .name = "umad",
  468. .add = ib_umad_add_one,
  469. .remove = ib_umad_remove_one
  470. };
  471. static ssize_t show_dev(struct class_device *class_dev, char *buf)
  472. {
  473. struct ib_umad_port *port = class_get_devdata(class_dev);
  474. if (class_dev == &port->class_dev)
  475. return print_dev_t(buf, port->dev.dev);
  476. else
  477. return print_dev_t(buf, port->sm_dev.dev);
  478. }
  479. static CLASS_DEVICE_ATTR(dev, S_IRUGO, show_dev, NULL);
  480. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  481. {
  482. struct ib_umad_port *port = class_get_devdata(class_dev);
  483. return sprintf(buf, "%s\n", port->ib_dev->name);
  484. }
  485. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  486. static ssize_t show_port(struct class_device *class_dev, char *buf)
  487. {
  488. struct ib_umad_port *port = class_get_devdata(class_dev);
  489. return sprintf(buf, "%d\n", port->port_num);
  490. }
  491. static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
  492. static void ib_umad_release_dev(struct kref *ref)
  493. {
  494. struct ib_umad_device *dev =
  495. container_of(ref, struct ib_umad_device, ref);
  496. kfree(dev);
  497. }
  498. static void ib_umad_release_port(struct class_device *class_dev)
  499. {
  500. struct ib_umad_port *port = class_get_devdata(class_dev);
  501. if (class_dev == &port->class_dev) {
  502. cdev_del(&port->dev);
  503. clear_bit(port->devnum, dev_map);
  504. } else {
  505. cdev_del(&port->sm_dev);
  506. clear_bit(port->sm_devnum, dev_map);
  507. }
  508. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  509. }
  510. static struct class umad_class = {
  511. .name = "infiniband_mad",
  512. .release = ib_umad_release_port
  513. };
  514. static ssize_t show_abi_version(struct class *class, char *buf)
  515. {
  516. return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
  517. }
  518. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  519. static int ib_umad_init_port(struct ib_device *device, int port_num,
  520. struct ib_umad_port *port)
  521. {
  522. spin_lock(&map_lock);
  523. port->devnum = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
  524. if (port->devnum >= IB_UMAD_MAX_PORTS) {
  525. spin_unlock(&map_lock);
  526. return -1;
  527. }
  528. port->sm_devnum = find_next_zero_bit(dev_map, IB_UMAD_MAX_PORTS * 2, IB_UMAD_MAX_PORTS);
  529. if (port->sm_devnum >= IB_UMAD_MAX_PORTS * 2) {
  530. spin_unlock(&map_lock);
  531. return -1;
  532. }
  533. set_bit(port->devnum, dev_map);
  534. set_bit(port->sm_devnum, dev_map);
  535. spin_unlock(&map_lock);
  536. port->ib_dev = device;
  537. port->port_num = port_num;
  538. init_MUTEX(&port->sm_sem);
  539. cdev_init(&port->dev, &umad_fops);
  540. port->dev.owner = THIS_MODULE;
  541. kobject_set_name(&port->dev.kobj, "umad%d", port->devnum);
  542. if (cdev_add(&port->dev, base_dev + port->devnum, 1))
  543. return -1;
  544. port->class_dev.class = &umad_class;
  545. port->class_dev.dev = device->dma_device;
  546. snprintf(port->class_dev.class_id, BUS_ID_SIZE, "umad%d", port->devnum);
  547. if (class_device_register(&port->class_dev))
  548. goto err_cdev;
  549. class_set_devdata(&port->class_dev, port);
  550. kref_get(&port->umad_dev->ref);
  551. if (class_device_create_file(&port->class_dev, &class_device_attr_dev))
  552. goto err_class;
  553. if (class_device_create_file(&port->class_dev, &class_device_attr_ibdev))
  554. goto err_class;
  555. if (class_device_create_file(&port->class_dev, &class_device_attr_port))
  556. goto err_class;
  557. cdev_init(&port->sm_dev, &umad_sm_fops);
  558. port->sm_dev.owner = THIS_MODULE;
  559. kobject_set_name(&port->dev.kobj, "issm%d", port->sm_devnum - IB_UMAD_MAX_PORTS);
  560. if (cdev_add(&port->sm_dev, base_dev + port->sm_devnum, 1))
  561. return -1;
  562. port->sm_class_dev.class = &umad_class;
  563. port->sm_class_dev.dev = device->dma_device;
  564. snprintf(port->sm_class_dev.class_id, BUS_ID_SIZE, "issm%d", port->sm_devnum - IB_UMAD_MAX_PORTS);
  565. if (class_device_register(&port->sm_class_dev))
  566. goto err_sm_cdev;
  567. class_set_devdata(&port->sm_class_dev, port);
  568. kref_get(&port->umad_dev->ref);
  569. if (class_device_create_file(&port->sm_class_dev, &class_device_attr_dev))
  570. goto err_sm_class;
  571. if (class_device_create_file(&port->sm_class_dev, &class_device_attr_ibdev))
  572. goto err_sm_class;
  573. if (class_device_create_file(&port->sm_class_dev, &class_device_attr_port))
  574. goto err_sm_class;
  575. return 0;
  576. err_sm_class:
  577. class_device_unregister(&port->sm_class_dev);
  578. err_sm_cdev:
  579. cdev_del(&port->sm_dev);
  580. err_class:
  581. class_device_unregister(&port->class_dev);
  582. err_cdev:
  583. cdev_del(&port->dev);
  584. clear_bit(port->devnum, dev_map);
  585. return -1;
  586. }
  587. static void ib_umad_add_one(struct ib_device *device)
  588. {
  589. struct ib_umad_device *umad_dev;
  590. int s, e, i;
  591. if (device->node_type == IB_NODE_SWITCH)
  592. s = e = 0;
  593. else {
  594. s = 1;
  595. e = device->phys_port_cnt;
  596. }
  597. umad_dev = kmalloc(sizeof *umad_dev +
  598. (e - s + 1) * sizeof (struct ib_umad_port),
  599. GFP_KERNEL);
  600. if (!umad_dev)
  601. return;
  602. memset(umad_dev, 0, sizeof *umad_dev +
  603. (e - s + 1) * sizeof (struct ib_umad_port));
  604. kref_init(&umad_dev->ref);
  605. umad_dev->start_port = s;
  606. umad_dev->end_port = e;
  607. for (i = s; i <= e; ++i) {
  608. umad_dev->port[i - s].umad_dev = umad_dev;
  609. if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
  610. goto err;
  611. }
  612. ib_set_client_data(device, &umad_client, umad_dev);
  613. return;
  614. err:
  615. while (--i >= s) {
  616. class_device_unregister(&umad_dev->port[i - s].class_dev);
  617. class_device_unregister(&umad_dev->port[i - s].sm_class_dev);
  618. }
  619. kref_put(&umad_dev->ref, ib_umad_release_dev);
  620. }
  621. static void ib_umad_remove_one(struct ib_device *device)
  622. {
  623. struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
  624. int i;
  625. if (!umad_dev)
  626. return;
  627. for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i) {
  628. class_device_unregister(&umad_dev->port[i].class_dev);
  629. class_device_unregister(&umad_dev->port[i].sm_class_dev);
  630. }
  631. kref_put(&umad_dev->ref, ib_umad_release_dev);
  632. }
  633. static int __init ib_umad_init(void)
  634. {
  635. int ret;
  636. spin_lock_init(&map_lock);
  637. ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
  638. "infiniband_mad");
  639. if (ret) {
  640. printk(KERN_ERR "user_mad: couldn't register device number\n");
  641. goto out;
  642. }
  643. ret = class_register(&umad_class);
  644. if (ret) {
  645. printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
  646. goto out_chrdev;
  647. }
  648. ret = class_create_file(&umad_class, &class_attr_abi_version);
  649. if (ret) {
  650. printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
  651. goto out_class;
  652. }
  653. ret = ib_register_client(&umad_client);
  654. if (ret) {
  655. printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
  656. goto out_class;
  657. }
  658. return 0;
  659. out_class:
  660. class_unregister(&umad_class);
  661. out_chrdev:
  662. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  663. out:
  664. return ret;
  665. }
  666. static void __exit ib_umad_cleanup(void)
  667. {
  668. ib_unregister_client(&umad_client);
  669. class_unregister(&umad_class);
  670. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  671. }
  672. module_init(ib_umad_init);
  673. module_exit(ib_umad_cleanup);