user_mad.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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->agent[agent_id] = agent;
  424. file->mr[agent_id] = ib_get_dma_mr(agent->qp->pd, IB_ACCESS_LOCAL_WRITE);
  425. if (IS_ERR(file->mr[agent_id])) {
  426. ret = -ENOMEM;
  427. goto err;
  428. }
  429. if (put_user(agent_id,
  430. (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
  431. ret = -EFAULT;
  432. goto err_mr;
  433. }
  434. ret = 0;
  435. goto out;
  436. err_mr:
  437. ib_dereg_mr(file->mr[agent_id]);
  438. err:
  439. file->agent[agent_id] = NULL;
  440. ib_unregister_mad_agent(agent);
  441. out:
  442. up_write(&file->port->mutex);
  443. return ret;
  444. }
  445. static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
  446. {
  447. u32 id;
  448. int ret = 0;
  449. down_write(&file->port->mutex);
  450. if (get_user(id, (u32 __user *) arg)) {
  451. ret = -EFAULT;
  452. goto out;
  453. }
  454. if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !file->agent[id]) {
  455. ret = -EINVAL;
  456. goto out;
  457. }
  458. ib_dereg_mr(file->mr[id]);
  459. ib_unregister_mad_agent(file->agent[id]);
  460. file->agent[id] = NULL;
  461. out:
  462. up_write(&file->port->mutex);
  463. return ret;
  464. }
  465. static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
  466. unsigned long arg)
  467. {
  468. switch (cmd) {
  469. case IB_USER_MAD_REGISTER_AGENT:
  470. return ib_umad_reg_agent(filp->private_data, arg);
  471. case IB_USER_MAD_UNREGISTER_AGENT:
  472. return ib_umad_unreg_agent(filp->private_data, arg);
  473. default:
  474. return -ENOIOCTLCMD;
  475. }
  476. }
  477. static int ib_umad_open(struct inode *inode, struct file *filp)
  478. {
  479. struct ib_umad_port *port;
  480. struct ib_umad_file *file;
  481. int ret = 0;
  482. spin_lock(&port_lock);
  483. port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE];
  484. if (port)
  485. kref_get(&port->umad_dev->ref);
  486. spin_unlock(&port_lock);
  487. if (!port)
  488. return -ENXIO;
  489. down_write(&port->mutex);
  490. if (!port->ib_dev) {
  491. ret = -ENXIO;
  492. goto out;
  493. }
  494. file = kzalloc(sizeof *file, GFP_KERNEL);
  495. if (!file) {
  496. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  497. ret = -ENOMEM;
  498. goto out;
  499. }
  500. spin_lock_init(&file->recv_lock);
  501. INIT_LIST_HEAD(&file->recv_list);
  502. init_waitqueue_head(&file->recv_wait);
  503. file->port = port;
  504. filp->private_data = file;
  505. list_add_tail(&file->port_list, &port->file_list);
  506. out:
  507. up_write(&port->mutex);
  508. return ret;
  509. }
  510. static int ib_umad_close(struct inode *inode, struct file *filp)
  511. {
  512. struct ib_umad_file *file = filp->private_data;
  513. struct ib_umad_device *dev = file->port->umad_dev;
  514. struct ib_umad_packet *packet, *tmp;
  515. int i;
  516. down_write(&file->port->mutex);
  517. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  518. if (file->agent[i]) {
  519. ib_dereg_mr(file->mr[i]);
  520. ib_unregister_mad_agent(file->agent[i]);
  521. }
  522. list_for_each_entry_safe(packet, tmp, &file->recv_list, list)
  523. kfree(packet);
  524. list_del(&file->port_list);
  525. up_write(&file->port->mutex);
  526. kfree(file);
  527. kref_put(&dev->ref, ib_umad_release_dev);
  528. return 0;
  529. }
  530. static struct file_operations umad_fops = {
  531. .owner = THIS_MODULE,
  532. .read = ib_umad_read,
  533. .write = ib_umad_write,
  534. .poll = ib_umad_poll,
  535. .unlocked_ioctl = ib_umad_ioctl,
  536. .compat_ioctl = ib_umad_ioctl,
  537. .open = ib_umad_open,
  538. .release = ib_umad_close
  539. };
  540. static int ib_umad_sm_open(struct inode *inode, struct file *filp)
  541. {
  542. struct ib_umad_port *port;
  543. struct ib_port_modify props = {
  544. .set_port_cap_mask = IB_PORT_SM
  545. };
  546. int ret;
  547. spin_lock(&port_lock);
  548. port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS];
  549. if (port)
  550. kref_get(&port->umad_dev->ref);
  551. spin_unlock(&port_lock);
  552. if (!port)
  553. return -ENXIO;
  554. if (filp->f_flags & O_NONBLOCK) {
  555. if (down_trylock(&port->sm_sem)) {
  556. ret = -EAGAIN;
  557. goto fail;
  558. }
  559. } else {
  560. if (down_interruptible(&port->sm_sem)) {
  561. ret = -ERESTARTSYS;
  562. goto fail;
  563. }
  564. }
  565. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  566. if (ret) {
  567. up(&port->sm_sem);
  568. goto fail;
  569. }
  570. filp->private_data = port;
  571. return 0;
  572. fail:
  573. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  574. return ret;
  575. }
  576. static int ib_umad_sm_close(struct inode *inode, struct file *filp)
  577. {
  578. struct ib_umad_port *port = filp->private_data;
  579. struct ib_port_modify props = {
  580. .clr_port_cap_mask = IB_PORT_SM
  581. };
  582. int ret = 0;
  583. down_write(&port->mutex);
  584. if (port->ib_dev)
  585. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  586. up_write(&port->mutex);
  587. up(&port->sm_sem);
  588. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  589. return ret;
  590. }
  591. static struct file_operations umad_sm_fops = {
  592. .owner = THIS_MODULE,
  593. .open = ib_umad_sm_open,
  594. .release = ib_umad_sm_close
  595. };
  596. static struct ib_client umad_client = {
  597. .name = "umad",
  598. .add = ib_umad_add_one,
  599. .remove = ib_umad_remove_one
  600. };
  601. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  602. {
  603. struct ib_umad_port *port = class_get_devdata(class_dev);
  604. if (!port)
  605. return -ENODEV;
  606. return sprintf(buf, "%s\n", port->ib_dev->name);
  607. }
  608. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  609. static ssize_t show_port(struct class_device *class_dev, char *buf)
  610. {
  611. struct ib_umad_port *port = class_get_devdata(class_dev);
  612. if (!port)
  613. return -ENODEV;
  614. return sprintf(buf, "%d\n", port->port_num);
  615. }
  616. static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
  617. static ssize_t show_abi_version(struct class *class, char *buf)
  618. {
  619. return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
  620. }
  621. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  622. static int ib_umad_init_port(struct ib_device *device, int port_num,
  623. struct ib_umad_port *port)
  624. {
  625. spin_lock(&port_lock);
  626. port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
  627. if (port->dev_num >= IB_UMAD_MAX_PORTS) {
  628. spin_unlock(&port_lock);
  629. return -1;
  630. }
  631. set_bit(port->dev_num, dev_map);
  632. spin_unlock(&port_lock);
  633. port->ib_dev = device;
  634. port->port_num = port_num;
  635. init_MUTEX(&port->sm_sem);
  636. init_rwsem(&port->mutex);
  637. INIT_LIST_HEAD(&port->file_list);
  638. port->dev = cdev_alloc();
  639. if (!port->dev)
  640. return -1;
  641. port->dev->owner = THIS_MODULE;
  642. port->dev->ops = &umad_fops;
  643. kobject_set_name(&port->dev->kobj, "umad%d", port->dev_num);
  644. if (cdev_add(port->dev, base_dev + port->dev_num, 1))
  645. goto err_cdev;
  646. port->class_dev = class_device_create(umad_class, NULL, port->dev->dev,
  647. device->dma_device,
  648. "umad%d", port->dev_num);
  649. if (IS_ERR(port->class_dev))
  650. goto err_cdev;
  651. if (class_device_create_file(port->class_dev, &class_device_attr_ibdev))
  652. goto err_class;
  653. if (class_device_create_file(port->class_dev, &class_device_attr_port))
  654. goto err_class;
  655. port->sm_dev = cdev_alloc();
  656. if (!port->sm_dev)
  657. goto err_class;
  658. port->sm_dev->owner = THIS_MODULE;
  659. port->sm_dev->ops = &umad_sm_fops;
  660. kobject_set_name(&port->dev->kobj, "issm%d", port->dev_num);
  661. if (cdev_add(port->sm_dev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1))
  662. goto err_sm_cdev;
  663. port->sm_class_dev = class_device_create(umad_class, NULL, port->sm_dev->dev,
  664. device->dma_device,
  665. "issm%d", port->dev_num);
  666. if (IS_ERR(port->sm_class_dev))
  667. goto err_sm_cdev;
  668. class_set_devdata(port->class_dev, port);
  669. class_set_devdata(port->sm_class_dev, port);
  670. if (class_device_create_file(port->sm_class_dev, &class_device_attr_ibdev))
  671. goto err_sm_class;
  672. if (class_device_create_file(port->sm_class_dev, &class_device_attr_port))
  673. goto err_sm_class;
  674. spin_lock(&port_lock);
  675. umad_port[port->dev_num] = port;
  676. spin_unlock(&port_lock);
  677. return 0;
  678. err_sm_class:
  679. class_device_destroy(umad_class, port->sm_dev->dev);
  680. err_sm_cdev:
  681. cdev_del(port->sm_dev);
  682. err_class:
  683. class_device_destroy(umad_class, port->dev->dev);
  684. err_cdev:
  685. cdev_del(port->dev);
  686. clear_bit(port->dev_num, dev_map);
  687. return -1;
  688. }
  689. static void ib_umad_kill_port(struct ib_umad_port *port)
  690. {
  691. struct ib_umad_file *file;
  692. int id;
  693. class_set_devdata(port->class_dev, NULL);
  694. class_set_devdata(port->sm_class_dev, NULL);
  695. class_device_destroy(umad_class, port->dev->dev);
  696. class_device_destroy(umad_class, port->sm_dev->dev);
  697. cdev_del(port->dev);
  698. cdev_del(port->sm_dev);
  699. spin_lock(&port_lock);
  700. umad_port[port->dev_num] = NULL;
  701. spin_unlock(&port_lock);
  702. down_write(&port->mutex);
  703. port->ib_dev = NULL;
  704. list_for_each_entry(file, &port->file_list, port_list)
  705. for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id) {
  706. if (!file->agent[id])
  707. continue;
  708. ib_dereg_mr(file->mr[id]);
  709. ib_unregister_mad_agent(file->agent[id]);
  710. file->agent[id] = NULL;
  711. }
  712. up_write(&port->mutex);
  713. clear_bit(port->dev_num, dev_map);
  714. }
  715. static void ib_umad_add_one(struct ib_device *device)
  716. {
  717. struct ib_umad_device *umad_dev;
  718. int s, e, i;
  719. if (device->node_type == IB_NODE_SWITCH)
  720. s = e = 0;
  721. else {
  722. s = 1;
  723. e = device->phys_port_cnt;
  724. }
  725. umad_dev = kzalloc(sizeof *umad_dev +
  726. (e - s + 1) * sizeof (struct ib_umad_port),
  727. GFP_KERNEL);
  728. if (!umad_dev)
  729. return;
  730. kref_init(&umad_dev->ref);
  731. umad_dev->start_port = s;
  732. umad_dev->end_port = e;
  733. for (i = s; i <= e; ++i) {
  734. umad_dev->port[i - s].umad_dev = umad_dev;
  735. if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
  736. goto err;
  737. }
  738. ib_set_client_data(device, &umad_client, umad_dev);
  739. return;
  740. err:
  741. while (--i >= s)
  742. ib_umad_kill_port(&umad_dev->port[i]);
  743. kref_put(&umad_dev->ref, ib_umad_release_dev);
  744. }
  745. static void ib_umad_remove_one(struct ib_device *device)
  746. {
  747. struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
  748. int i;
  749. if (!umad_dev)
  750. return;
  751. for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
  752. ib_umad_kill_port(&umad_dev->port[i]);
  753. kref_put(&umad_dev->ref, ib_umad_release_dev);
  754. }
  755. static int __init ib_umad_init(void)
  756. {
  757. int ret;
  758. ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
  759. "infiniband_mad");
  760. if (ret) {
  761. printk(KERN_ERR "user_mad: couldn't register device number\n");
  762. goto out;
  763. }
  764. umad_class = class_create(THIS_MODULE, "infiniband_mad");
  765. if (IS_ERR(umad_class)) {
  766. ret = PTR_ERR(umad_class);
  767. printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
  768. goto out_chrdev;
  769. }
  770. ret = class_create_file(umad_class, &class_attr_abi_version);
  771. if (ret) {
  772. printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
  773. goto out_class;
  774. }
  775. ret = ib_register_client(&umad_client);
  776. if (ret) {
  777. printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
  778. goto out_class;
  779. }
  780. return 0;
  781. out_class:
  782. class_destroy(umad_class);
  783. out_chrdev:
  784. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  785. out:
  786. return ret;
  787. }
  788. static void __exit ib_umad_cleanup(void)
  789. {
  790. ib_unregister_client(&umad_client);
  791. class_destroy(umad_class);
  792. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  793. }
  794. module_init(ib_umad_init);
  795. module_exit(ib_umad_cleanup);