user_mad.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
  4. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  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. *
  34. * $Id: user_mad.c 2814 2005-07-06 19:14:09Z halr $
  35. */
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/device.h>
  39. #include <linux/err.h>
  40. #include <linux/fs.h>
  41. #include <linux/cdev.h>
  42. #include <linux/pci.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/poll.h>
  45. #include <linux/rwsem.h>
  46. #include <linux/kref.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/semaphore.h>
  49. #include <rdma/ib_mad.h>
  50. #include <rdma/ib_user_mad.h>
  51. MODULE_AUTHOR("Roland Dreier");
  52. MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
  53. MODULE_LICENSE("Dual BSD/GPL");
  54. enum {
  55. IB_UMAD_MAX_PORTS = 64,
  56. IB_UMAD_MAX_AGENTS = 32,
  57. IB_UMAD_MAJOR = 231,
  58. IB_UMAD_MINOR_BASE = 0
  59. };
  60. struct ib_umad_port {
  61. int devnum;
  62. struct cdev dev;
  63. struct class_device class_dev;
  64. int sm_devnum;
  65. struct cdev sm_dev;
  66. struct class_device sm_class_dev;
  67. struct semaphore sm_sem;
  68. struct ib_device *ib_dev;
  69. struct ib_umad_device *umad_dev;
  70. u8 port_num;
  71. };
  72. struct ib_umad_device {
  73. int start_port, end_port;
  74. struct kref ref;
  75. struct ib_umad_port port[0];
  76. };
  77. struct ib_umad_file {
  78. struct ib_umad_port *port;
  79. spinlock_t recv_lock;
  80. struct list_head recv_list;
  81. wait_queue_head_t recv_wait;
  82. struct rw_semaphore agent_mutex;
  83. struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];
  84. struct ib_mr *mr[IB_UMAD_MAX_AGENTS];
  85. };
  86. struct ib_umad_packet {
  87. struct ib_mad_send_buf *msg;
  88. struct list_head list;
  89. int length;
  90. DECLARE_PCI_UNMAP_ADDR(mapping)
  91. struct ib_user_mad mad;
  92. };
  93. static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
  94. static spinlock_t map_lock;
  95. static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS * 2);
  96. static void ib_umad_add_one(struct ib_device *device);
  97. static void ib_umad_remove_one(struct ib_device *device);
  98. static int queue_packet(struct ib_umad_file *file,
  99. struct ib_mad_agent *agent,
  100. struct ib_umad_packet *packet)
  101. {
  102. int ret = 1;
  103. down_read(&file->agent_mutex);
  104. for (packet->mad.hdr.id = 0;
  105. packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
  106. packet->mad.hdr.id++)
  107. if (agent == file->agent[packet->mad.hdr.id]) {
  108. spin_lock_irq(&file->recv_lock);
  109. list_add_tail(&packet->list, &file->recv_list);
  110. spin_unlock_irq(&file->recv_lock);
  111. wake_up_interruptible(&file->recv_wait);
  112. ret = 0;
  113. break;
  114. }
  115. up_read(&file->agent_mutex);
  116. return ret;
  117. }
  118. static void send_handler(struct ib_mad_agent *agent,
  119. struct ib_mad_send_wc *send_wc)
  120. {
  121. struct ib_umad_file *file = agent->context;
  122. struct ib_umad_packet *timeout;
  123. struct ib_umad_packet *packet = send_wc->send_buf->context[0];
  124. ib_destroy_ah(packet->msg->ah);
  125. ib_free_send_mad(packet->msg);
  126. if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
  127. timeout = kmalloc(sizeof *timeout + sizeof (struct ib_mad_hdr),
  128. GFP_KERNEL);
  129. if (!timeout)
  130. goto out;
  131. memset(timeout, 0, sizeof *timeout + sizeof (struct ib_mad_hdr));
  132. timeout->length = sizeof (struct ib_mad_hdr);
  133. timeout->mad.hdr.id = packet->mad.hdr.id;
  134. timeout->mad.hdr.status = ETIMEDOUT;
  135. memcpy(timeout->mad.data, packet->mad.data,
  136. sizeof (struct ib_mad_hdr));
  137. if (!queue_packet(file, agent, timeout))
  138. return;
  139. }
  140. out:
  141. kfree(packet);
  142. }
  143. static void recv_handler(struct ib_mad_agent *agent,
  144. struct ib_mad_recv_wc *mad_recv_wc)
  145. {
  146. struct ib_umad_file *file = agent->context;
  147. struct ib_umad_packet *packet;
  148. int length;
  149. if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
  150. goto out;
  151. length = mad_recv_wc->mad_len;
  152. packet = kmalloc(sizeof *packet + length, GFP_KERNEL);
  153. if (!packet)
  154. goto out;
  155. memset(packet, 0, sizeof *packet + length);
  156. packet->length = length;
  157. ib_coalesce_recv_mad(mad_recv_wc, packet->mad.data);
  158. packet->mad.hdr.status = 0;
  159. packet->mad.hdr.length = length + sizeof (struct ib_user_mad);
  160. packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
  161. packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);
  162. packet->mad.hdr.sl = mad_recv_wc->wc->sl;
  163. packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
  164. packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
  165. if (packet->mad.hdr.grh_present) {
  166. /* XXX parse GRH */
  167. packet->mad.hdr.gid_index = 0;
  168. packet->mad.hdr.hop_limit = 0;
  169. packet->mad.hdr.traffic_class = 0;
  170. memset(packet->mad.hdr.gid, 0, 16);
  171. packet->mad.hdr.flow_label = 0;
  172. }
  173. if (queue_packet(file, agent, packet))
  174. kfree(packet);
  175. out:
  176. ib_free_recv_mad(mad_recv_wc);
  177. }
  178. static ssize_t ib_umad_read(struct file *filp, char __user *buf,
  179. size_t count, loff_t *pos)
  180. {
  181. struct ib_umad_file *file = filp->private_data;
  182. struct ib_umad_packet *packet;
  183. ssize_t ret;
  184. if (count < sizeof (struct ib_user_mad) + sizeof (struct ib_mad))
  185. return -EINVAL;
  186. spin_lock_irq(&file->recv_lock);
  187. while (list_empty(&file->recv_list)) {
  188. spin_unlock_irq(&file->recv_lock);
  189. if (filp->f_flags & O_NONBLOCK)
  190. return -EAGAIN;
  191. if (wait_event_interruptible(file->recv_wait,
  192. !list_empty(&file->recv_list)))
  193. return -ERESTARTSYS;
  194. spin_lock_irq(&file->recv_lock);
  195. }
  196. packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
  197. list_del(&packet->list);
  198. spin_unlock_irq(&file->recv_lock);
  199. if (count < packet->length + sizeof (struct ib_user_mad)) {
  200. /* Return length needed (and first RMPP segment) if too small */
  201. if (copy_to_user(buf, &packet->mad,
  202. sizeof (struct ib_user_mad) + sizeof (struct ib_mad)))
  203. ret = -EFAULT;
  204. else
  205. ret = -ENOSPC;
  206. } else if (copy_to_user(buf, &packet->mad,
  207. packet->length + sizeof (struct ib_user_mad)))
  208. ret = -EFAULT;
  209. else
  210. ret = packet->length + sizeof (struct ib_user_mad);
  211. if (ret < 0) {
  212. /* Requeue packet */
  213. spin_lock_irq(&file->recv_lock);
  214. list_add(&packet->list, &file->recv_list);
  215. spin_unlock_irq(&file->recv_lock);
  216. } else
  217. kfree(packet);
  218. return ret;
  219. }
  220. static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
  221. size_t count, loff_t *pos)
  222. {
  223. struct ib_umad_file *file = filp->private_data;
  224. struct ib_umad_packet *packet;
  225. struct ib_mad_agent *agent;
  226. struct ib_ah_attr ah_attr;
  227. struct ib_ah *ah;
  228. struct ib_rmpp_mad *rmpp_mad;
  229. u8 method;
  230. __be64 *tid;
  231. int ret, length, hdr_len, rmpp_hdr_size;
  232. int rmpp_active = 0;
  233. if (count < sizeof (struct ib_user_mad))
  234. return -EINVAL;
  235. length = count - sizeof (struct ib_user_mad);
  236. packet = kmalloc(sizeof *packet + sizeof(struct ib_mad_hdr) +
  237. sizeof (struct ib_rmpp_hdr), GFP_KERNEL);
  238. if (!packet)
  239. return -ENOMEM;
  240. if (copy_from_user(&packet->mad, buf,
  241. sizeof (struct ib_user_mad) +
  242. sizeof (struct ib_mad_hdr) +
  243. sizeof (struct ib_rmpp_hdr))) {
  244. ret = -EFAULT;
  245. goto err;
  246. }
  247. if (packet->mad.hdr.id < 0 ||
  248. packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
  249. ret = -EINVAL;
  250. goto err;
  251. }
  252. packet->length = length;
  253. down_read(&file->agent_mutex);
  254. agent = file->agent[packet->mad.hdr.id];
  255. if (!agent) {
  256. ret = -EINVAL;
  257. goto err_up;
  258. }
  259. memset(&ah_attr, 0, sizeof ah_attr);
  260. ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid);
  261. ah_attr.sl = packet->mad.hdr.sl;
  262. ah_attr.src_path_bits = packet->mad.hdr.path_bits;
  263. ah_attr.port_num = file->port->port_num;
  264. if (packet->mad.hdr.grh_present) {
  265. ah_attr.ah_flags = IB_AH_GRH;
  266. memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16);
  267. ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label);
  268. ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit;
  269. ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class;
  270. }
  271. ah = ib_create_ah(agent->qp->pd, &ah_attr);
  272. if (IS_ERR(ah)) {
  273. ret = PTR_ERR(ah);
  274. goto err_up;
  275. }
  276. rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
  277. if (ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_ACTIVE) {
  278. /* RMPP active */
  279. if (!agent->rmpp_version) {
  280. ret = -EINVAL;
  281. goto err_ah;
  282. }
  283. /* Validate that the management class can support RMPP */
  284. if (rmpp_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_ADM) {
  285. hdr_len = IB_MGMT_SA_HDR;
  286. } else if ((rmpp_mad->mad_hdr.mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
  287. (rmpp_mad->mad_hdr.mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)) {
  288. hdr_len = IB_MGMT_VENDOR_HDR;
  289. } else {
  290. ret = -EINVAL;
  291. goto err_ah;
  292. }
  293. rmpp_active = 1;
  294. } else {
  295. if (length > sizeof (struct ib_mad)) {
  296. ret = -EINVAL;
  297. goto err_ah;
  298. }
  299. hdr_len = IB_MGMT_MAD_HDR;
  300. }
  301. packet->msg = ib_create_send_mad(agent,
  302. be32_to_cpu(packet->mad.hdr.qpn),
  303. 0, rmpp_active,
  304. hdr_len, length - hdr_len,
  305. GFP_KERNEL);
  306. if (IS_ERR(packet->msg)) {
  307. ret = PTR_ERR(packet->msg);
  308. goto err_ah;
  309. }
  310. packet->msg->ah = ah;
  311. packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
  312. packet->msg->retries = packet->mad.hdr.retries;
  313. packet->msg->context[0] = packet;
  314. if (!rmpp_active) {
  315. /* Copy message from user into send buffer */
  316. if (copy_from_user(packet->msg->mad,
  317. buf + sizeof (struct ib_user_mad), length)) {
  318. ret = -EFAULT;
  319. goto err_msg;
  320. }
  321. } else {
  322. rmpp_hdr_size = sizeof (struct ib_mad_hdr) +
  323. sizeof (struct ib_rmpp_hdr);
  324. /* Only copy MAD headers (RMPP header in place) */
  325. memcpy(packet->msg->mad, packet->mad.data,
  326. sizeof (struct ib_mad_hdr));
  327. /* Now, copy rest of message from user into send buffer */
  328. if (copy_from_user(((struct ib_rmpp_mad *) packet->msg->mad)->data,
  329. buf + sizeof (struct ib_user_mad) + rmpp_hdr_size,
  330. length - rmpp_hdr_size)) {
  331. ret = -EFAULT;
  332. goto err_msg;
  333. }
  334. }
  335. /*
  336. * If userspace is generating a request that will generate a
  337. * response, we need to make sure the high-order part of the
  338. * transaction ID matches the agent being used to send the
  339. * MAD.
  340. */
  341. method = ((struct ib_mad_hdr *) packet->msg->mad)->method;
  342. if (!(method & IB_MGMT_METHOD_RESP) &&
  343. method != IB_MGMT_METHOD_TRAP_REPRESS &&
  344. method != IB_MGMT_METHOD_SEND) {
  345. tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
  346. *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
  347. (be64_to_cpup(tid) & 0xffffffff));
  348. }
  349. ret = ib_post_send_mad(packet->msg, NULL);
  350. if (ret)
  351. goto err_msg;
  352. up_read(&file->agent_mutex);
  353. return sizeof (struct ib_user_mad_hdr) + packet->length;
  354. err_msg:
  355. ib_free_send_mad(packet->msg);
  356. err_ah:
  357. ib_destroy_ah(ah);
  358. err_up:
  359. up_read(&file->agent_mutex);
  360. err:
  361. kfree(packet);
  362. return ret;
  363. }
  364. static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
  365. {
  366. struct ib_umad_file *file = filp->private_data;
  367. /* we will always be able to post a MAD send */
  368. unsigned int mask = POLLOUT | POLLWRNORM;
  369. poll_wait(filp, &file->recv_wait, wait);
  370. if (!list_empty(&file->recv_list))
  371. mask |= POLLIN | POLLRDNORM;
  372. return mask;
  373. }
  374. static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
  375. {
  376. struct ib_user_mad_reg_req ureq;
  377. struct ib_mad_reg_req req;
  378. struct ib_mad_agent *agent;
  379. int agent_id;
  380. int ret;
  381. down_write(&file->agent_mutex);
  382. if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) {
  383. ret = -EFAULT;
  384. goto out;
  385. }
  386. if (ureq.qpn != 0 && ureq.qpn != 1) {
  387. ret = -EINVAL;
  388. goto out;
  389. }
  390. for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
  391. if (!file->agent[agent_id])
  392. goto found;
  393. ret = -ENOMEM;
  394. goto out;
  395. found:
  396. if (ureq.mgmt_class) {
  397. req.mgmt_class = ureq.mgmt_class;
  398. req.mgmt_class_version = ureq.mgmt_class_version;
  399. memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask);
  400. memcpy(req.oui, ureq.oui, sizeof req.oui);
  401. }
  402. agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
  403. ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
  404. ureq.mgmt_class ? &req : NULL,
  405. ureq.rmpp_version,
  406. send_handler, recv_handler, file);
  407. if (IS_ERR(agent)) {
  408. ret = PTR_ERR(agent);
  409. goto out;
  410. }
  411. file->agent[agent_id] = agent;
  412. file->mr[agent_id] = ib_get_dma_mr(agent->qp->pd, IB_ACCESS_LOCAL_WRITE);
  413. if (IS_ERR(file->mr[agent_id])) {
  414. ret = -ENOMEM;
  415. goto err;
  416. }
  417. if (put_user(agent_id,
  418. (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
  419. ret = -EFAULT;
  420. goto err_mr;
  421. }
  422. ret = 0;
  423. goto out;
  424. err_mr:
  425. ib_dereg_mr(file->mr[agent_id]);
  426. err:
  427. file->agent[agent_id] = NULL;
  428. ib_unregister_mad_agent(agent);
  429. out:
  430. up_write(&file->agent_mutex);
  431. return ret;
  432. }
  433. static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
  434. {
  435. u32 id;
  436. int ret = 0;
  437. down_write(&file->agent_mutex);
  438. if (get_user(id, (u32 __user *) arg)) {
  439. ret = -EFAULT;
  440. goto out;
  441. }
  442. if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !file->agent[id]) {
  443. ret = -EINVAL;
  444. goto out;
  445. }
  446. ib_dereg_mr(file->mr[id]);
  447. ib_unregister_mad_agent(file->agent[id]);
  448. file->agent[id] = NULL;
  449. out:
  450. up_write(&file->agent_mutex);
  451. return ret;
  452. }
  453. static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
  454. unsigned long arg)
  455. {
  456. switch (cmd) {
  457. case IB_USER_MAD_REGISTER_AGENT:
  458. return ib_umad_reg_agent(filp->private_data, arg);
  459. case IB_USER_MAD_UNREGISTER_AGENT:
  460. return ib_umad_unreg_agent(filp->private_data, arg);
  461. default:
  462. return -ENOIOCTLCMD;
  463. }
  464. }
  465. static int ib_umad_open(struct inode *inode, struct file *filp)
  466. {
  467. struct ib_umad_port *port =
  468. container_of(inode->i_cdev, struct ib_umad_port, dev);
  469. struct ib_umad_file *file;
  470. file = kmalloc(sizeof *file, GFP_KERNEL);
  471. if (!file)
  472. return -ENOMEM;
  473. memset(file, 0, sizeof *file);
  474. spin_lock_init(&file->recv_lock);
  475. init_rwsem(&file->agent_mutex);
  476. INIT_LIST_HEAD(&file->recv_list);
  477. init_waitqueue_head(&file->recv_wait);
  478. file->port = port;
  479. filp->private_data = file;
  480. return 0;
  481. }
  482. static int ib_umad_close(struct inode *inode, struct file *filp)
  483. {
  484. struct ib_umad_file *file = filp->private_data;
  485. struct ib_umad_packet *packet, *tmp;
  486. int i;
  487. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  488. if (file->agent[i]) {
  489. ib_dereg_mr(file->mr[i]);
  490. ib_unregister_mad_agent(file->agent[i]);
  491. }
  492. list_for_each_entry_safe(packet, tmp, &file->recv_list, list)
  493. kfree(packet);
  494. kfree(file);
  495. return 0;
  496. }
  497. static struct file_operations umad_fops = {
  498. .owner = THIS_MODULE,
  499. .read = ib_umad_read,
  500. .write = ib_umad_write,
  501. .poll = ib_umad_poll,
  502. .unlocked_ioctl = ib_umad_ioctl,
  503. .compat_ioctl = ib_umad_ioctl,
  504. .open = ib_umad_open,
  505. .release = ib_umad_close
  506. };
  507. static int ib_umad_sm_open(struct inode *inode, struct file *filp)
  508. {
  509. struct ib_umad_port *port =
  510. container_of(inode->i_cdev, struct ib_umad_port, sm_dev);
  511. struct ib_port_modify props = {
  512. .set_port_cap_mask = IB_PORT_SM
  513. };
  514. int ret;
  515. if (filp->f_flags & O_NONBLOCK) {
  516. if (down_trylock(&port->sm_sem))
  517. return -EAGAIN;
  518. } else {
  519. if (down_interruptible(&port->sm_sem))
  520. return -ERESTARTSYS;
  521. }
  522. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  523. if (ret) {
  524. up(&port->sm_sem);
  525. return ret;
  526. }
  527. filp->private_data = port;
  528. return 0;
  529. }
  530. static int ib_umad_sm_close(struct inode *inode, struct file *filp)
  531. {
  532. struct ib_umad_port *port = filp->private_data;
  533. struct ib_port_modify props = {
  534. .clr_port_cap_mask = IB_PORT_SM
  535. };
  536. int ret;
  537. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  538. up(&port->sm_sem);
  539. return ret;
  540. }
  541. static struct file_operations umad_sm_fops = {
  542. .owner = THIS_MODULE,
  543. .open = ib_umad_sm_open,
  544. .release = ib_umad_sm_close
  545. };
  546. static struct ib_client umad_client = {
  547. .name = "umad",
  548. .add = ib_umad_add_one,
  549. .remove = ib_umad_remove_one
  550. };
  551. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  552. {
  553. struct ib_umad_port *port = class_get_devdata(class_dev);
  554. return sprintf(buf, "%s\n", port->ib_dev->name);
  555. }
  556. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  557. static ssize_t show_port(struct class_device *class_dev, char *buf)
  558. {
  559. struct ib_umad_port *port = class_get_devdata(class_dev);
  560. return sprintf(buf, "%d\n", port->port_num);
  561. }
  562. static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
  563. static void ib_umad_release_dev(struct kref *ref)
  564. {
  565. struct ib_umad_device *dev =
  566. container_of(ref, struct ib_umad_device, ref);
  567. kfree(dev);
  568. }
  569. static void ib_umad_release_port(struct class_device *class_dev)
  570. {
  571. struct ib_umad_port *port = class_get_devdata(class_dev);
  572. if (class_dev == &port->class_dev) {
  573. cdev_del(&port->dev);
  574. clear_bit(port->devnum, dev_map);
  575. } else {
  576. cdev_del(&port->sm_dev);
  577. clear_bit(port->sm_devnum, dev_map);
  578. }
  579. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  580. }
  581. static struct class umad_class = {
  582. .name = "infiniband_mad",
  583. .release = ib_umad_release_port
  584. };
  585. static ssize_t show_abi_version(struct class *class, char *buf)
  586. {
  587. return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
  588. }
  589. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  590. static int ib_umad_init_port(struct ib_device *device, int port_num,
  591. struct ib_umad_port *port)
  592. {
  593. spin_lock(&map_lock);
  594. port->devnum = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
  595. if (port->devnum >= IB_UMAD_MAX_PORTS) {
  596. spin_unlock(&map_lock);
  597. return -1;
  598. }
  599. port->sm_devnum = find_next_zero_bit(dev_map, IB_UMAD_MAX_PORTS * 2, IB_UMAD_MAX_PORTS);
  600. if (port->sm_devnum >= IB_UMAD_MAX_PORTS * 2) {
  601. spin_unlock(&map_lock);
  602. return -1;
  603. }
  604. set_bit(port->devnum, dev_map);
  605. set_bit(port->sm_devnum, dev_map);
  606. spin_unlock(&map_lock);
  607. port->ib_dev = device;
  608. port->port_num = port_num;
  609. init_MUTEX(&port->sm_sem);
  610. cdev_init(&port->dev, &umad_fops);
  611. port->dev.owner = THIS_MODULE;
  612. kobject_set_name(&port->dev.kobj, "umad%d", port->devnum);
  613. if (cdev_add(&port->dev, base_dev + port->devnum, 1))
  614. return -1;
  615. port->class_dev.class = &umad_class;
  616. port->class_dev.dev = device->dma_device;
  617. port->class_dev.devt = port->dev.dev;
  618. snprintf(port->class_dev.class_id, BUS_ID_SIZE, "umad%d", port->devnum);
  619. if (class_device_register(&port->class_dev))
  620. goto err_cdev;
  621. class_set_devdata(&port->class_dev, port);
  622. kref_get(&port->umad_dev->ref);
  623. if (class_device_create_file(&port->class_dev, &class_device_attr_ibdev))
  624. goto err_class;
  625. if (class_device_create_file(&port->class_dev, &class_device_attr_port))
  626. goto err_class;
  627. cdev_init(&port->sm_dev, &umad_sm_fops);
  628. port->sm_dev.owner = THIS_MODULE;
  629. kobject_set_name(&port->dev.kobj, "issm%d", port->sm_devnum - IB_UMAD_MAX_PORTS);
  630. if (cdev_add(&port->sm_dev, base_dev + port->sm_devnum, 1))
  631. return -1;
  632. port->sm_class_dev.class = &umad_class;
  633. port->sm_class_dev.dev = device->dma_device;
  634. port->sm_class_dev.devt = port->sm_dev.dev;
  635. snprintf(port->sm_class_dev.class_id, BUS_ID_SIZE, "issm%d", port->sm_devnum - IB_UMAD_MAX_PORTS);
  636. if (class_device_register(&port->sm_class_dev))
  637. goto err_sm_cdev;
  638. class_set_devdata(&port->sm_class_dev, port);
  639. kref_get(&port->umad_dev->ref);
  640. if (class_device_create_file(&port->sm_class_dev, &class_device_attr_ibdev))
  641. goto err_sm_class;
  642. if (class_device_create_file(&port->sm_class_dev, &class_device_attr_port))
  643. goto err_sm_class;
  644. return 0;
  645. err_sm_class:
  646. class_device_unregister(&port->sm_class_dev);
  647. err_sm_cdev:
  648. cdev_del(&port->sm_dev);
  649. err_class:
  650. class_device_unregister(&port->class_dev);
  651. err_cdev:
  652. cdev_del(&port->dev);
  653. clear_bit(port->devnum, dev_map);
  654. return -1;
  655. }
  656. static void ib_umad_add_one(struct ib_device *device)
  657. {
  658. struct ib_umad_device *umad_dev;
  659. int s, e, i;
  660. if (device->node_type == IB_NODE_SWITCH)
  661. s = e = 0;
  662. else {
  663. s = 1;
  664. e = device->phys_port_cnt;
  665. }
  666. umad_dev = kmalloc(sizeof *umad_dev +
  667. (e - s + 1) * sizeof (struct ib_umad_port),
  668. GFP_KERNEL);
  669. if (!umad_dev)
  670. return;
  671. memset(umad_dev, 0, sizeof *umad_dev +
  672. (e - s + 1) * sizeof (struct ib_umad_port));
  673. kref_init(&umad_dev->ref);
  674. umad_dev->start_port = s;
  675. umad_dev->end_port = e;
  676. for (i = s; i <= e; ++i) {
  677. umad_dev->port[i - s].umad_dev = umad_dev;
  678. if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
  679. goto err;
  680. }
  681. ib_set_client_data(device, &umad_client, umad_dev);
  682. return;
  683. err:
  684. while (--i >= s) {
  685. class_device_unregister(&umad_dev->port[i - s].class_dev);
  686. class_device_unregister(&umad_dev->port[i - s].sm_class_dev);
  687. }
  688. kref_put(&umad_dev->ref, ib_umad_release_dev);
  689. }
  690. static void ib_umad_remove_one(struct ib_device *device)
  691. {
  692. struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
  693. int i;
  694. if (!umad_dev)
  695. return;
  696. for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i) {
  697. class_device_unregister(&umad_dev->port[i].class_dev);
  698. class_device_unregister(&umad_dev->port[i].sm_class_dev);
  699. }
  700. kref_put(&umad_dev->ref, ib_umad_release_dev);
  701. }
  702. static int __init ib_umad_init(void)
  703. {
  704. int ret;
  705. spin_lock_init(&map_lock);
  706. ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
  707. "infiniband_mad");
  708. if (ret) {
  709. printk(KERN_ERR "user_mad: couldn't register device number\n");
  710. goto out;
  711. }
  712. ret = class_register(&umad_class);
  713. if (ret) {
  714. printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
  715. goto out_chrdev;
  716. }
  717. ret = class_create_file(&umad_class, &class_attr_abi_version);
  718. if (ret) {
  719. printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
  720. goto out_class;
  721. }
  722. ret = ib_register_client(&umad_client);
  723. if (ret) {
  724. printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
  725. goto out_class;
  726. }
  727. return 0;
  728. out_class:
  729. class_unregister(&umad_class);
  730. out_chrdev:
  731. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  732. out:
  733. return ret;
  734. }
  735. static void __exit ib_umad_cleanup(void)
  736. {
  737. ib_unregister_client(&umad_client);
  738. class_unregister(&umad_class);
  739. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  740. }
  741. module_init(ib_umad_init);
  742. module_exit(ib_umad_cleanup);