user_mad.c 24 KB

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