user_mad.c 23 KB

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