user_mad.c 23 KB

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