user_mad.c 22 KB

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