user_mad.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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 4010 2005-11-09 23:11:56Z roland $
  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. int agents_dead;
  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. /* caller must hold port->mutex at least for reading */
  128. static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
  129. {
  130. return file->agents_dead ? NULL : file->agent[id];
  131. }
  132. static int queue_packet(struct ib_umad_file *file,
  133. struct ib_mad_agent *agent,
  134. struct ib_umad_packet *packet)
  135. {
  136. int ret = 1;
  137. down_read(&file->port->mutex);
  138. for (packet->mad.hdr.id = 0;
  139. packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
  140. packet->mad.hdr.id++)
  141. if (agent == __get_agent(file, packet->mad.hdr.id)) {
  142. spin_lock_irq(&file->recv_lock);
  143. list_add_tail(&packet->list, &file->recv_list);
  144. spin_unlock_irq(&file->recv_lock);
  145. wake_up_interruptible(&file->recv_wait);
  146. ret = 0;
  147. break;
  148. }
  149. up_read(&file->port->mutex);
  150. return ret;
  151. }
  152. static void send_handler(struct ib_mad_agent *agent,
  153. struct ib_mad_send_wc *send_wc)
  154. {
  155. struct ib_umad_file *file = agent->context;
  156. struct ib_umad_packet *timeout;
  157. struct ib_umad_packet *packet = send_wc->send_buf->context[0];
  158. ib_destroy_ah(packet->msg->ah);
  159. ib_free_send_mad(packet->msg);
  160. if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
  161. timeout = kzalloc(sizeof *timeout + IB_MGMT_MAD_HDR, GFP_KERNEL);
  162. if (!timeout)
  163. goto out;
  164. timeout->length = IB_MGMT_MAD_HDR;
  165. timeout->mad.hdr.id = packet->mad.hdr.id;
  166. timeout->mad.hdr.status = ETIMEDOUT;
  167. memcpy(timeout->mad.data, packet->mad.data,
  168. sizeof (struct ib_mad_hdr));
  169. if (queue_packet(file, agent, timeout))
  170. kfree(timeout);
  171. }
  172. out:
  173. kfree(packet);
  174. }
  175. static void recv_handler(struct ib_mad_agent *agent,
  176. struct ib_mad_recv_wc *mad_recv_wc)
  177. {
  178. struct ib_umad_file *file = agent->context;
  179. struct ib_umad_packet *packet;
  180. int length;
  181. if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
  182. goto out;
  183. length = mad_recv_wc->mad_len;
  184. packet = kzalloc(sizeof *packet + length, GFP_KERNEL);
  185. if (!packet)
  186. goto out;
  187. packet->length = length;
  188. ib_coalesce_recv_mad(mad_recv_wc, packet->mad.data);
  189. packet->mad.hdr.status = 0;
  190. packet->mad.hdr.length = length + sizeof (struct ib_user_mad);
  191. packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
  192. packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);
  193. packet->mad.hdr.sl = mad_recv_wc->wc->sl;
  194. packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
  195. packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
  196. if (packet->mad.hdr.grh_present) {
  197. /* XXX parse GRH */
  198. packet->mad.hdr.gid_index = 0;
  199. packet->mad.hdr.hop_limit = 0;
  200. packet->mad.hdr.traffic_class = 0;
  201. memset(packet->mad.hdr.gid, 0, 16);
  202. packet->mad.hdr.flow_label = 0;
  203. }
  204. if (queue_packet(file, agent, packet))
  205. kfree(packet);
  206. out:
  207. ib_free_recv_mad(mad_recv_wc);
  208. }
  209. static ssize_t ib_umad_read(struct file *filp, char __user *buf,
  210. size_t count, loff_t *pos)
  211. {
  212. struct ib_umad_file *file = filp->private_data;
  213. struct ib_umad_packet *packet;
  214. ssize_t ret;
  215. if (count < sizeof (struct ib_user_mad) + sizeof (struct ib_mad))
  216. return -EINVAL;
  217. spin_lock_irq(&file->recv_lock);
  218. while (list_empty(&file->recv_list)) {
  219. spin_unlock_irq(&file->recv_lock);
  220. if (filp->f_flags & O_NONBLOCK)
  221. return -EAGAIN;
  222. if (wait_event_interruptible(file->recv_wait,
  223. !list_empty(&file->recv_list)))
  224. return -ERESTARTSYS;
  225. spin_lock_irq(&file->recv_lock);
  226. }
  227. packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
  228. list_del(&packet->list);
  229. spin_unlock_irq(&file->recv_lock);
  230. if (count < packet->length + sizeof (struct ib_user_mad)) {
  231. /* Return length needed (and first RMPP segment) if too small */
  232. if (copy_to_user(buf, &packet->mad,
  233. sizeof (struct ib_user_mad) + sizeof (struct ib_mad)))
  234. ret = -EFAULT;
  235. else
  236. ret = -ENOSPC;
  237. } else if (copy_to_user(buf, &packet->mad,
  238. packet->length + sizeof (struct ib_user_mad)))
  239. ret = -EFAULT;
  240. else
  241. ret = packet->length + sizeof (struct ib_user_mad);
  242. if (ret < 0) {
  243. /* Requeue packet */
  244. spin_lock_irq(&file->recv_lock);
  245. list_add(&packet->list, &file->recv_list);
  246. spin_unlock_irq(&file->recv_lock);
  247. } else
  248. kfree(packet);
  249. return ret;
  250. }
  251. static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
  252. size_t count, loff_t *pos)
  253. {
  254. struct ib_umad_file *file = filp->private_data;
  255. struct ib_umad_packet *packet;
  256. struct ib_mad_agent *agent;
  257. struct ib_ah_attr ah_attr;
  258. struct ib_ah *ah;
  259. struct ib_rmpp_mad *rmpp_mad;
  260. u8 method;
  261. __be64 *tid;
  262. int ret, length, hdr_len, copy_offset;
  263. int rmpp_active, has_rmpp_header;
  264. if (count < sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)
  265. return -EINVAL;
  266. length = count - sizeof (struct ib_user_mad);
  267. packet = kmalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
  268. if (!packet)
  269. return -ENOMEM;
  270. if (copy_from_user(&packet->mad, buf,
  271. sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)) {
  272. ret = -EFAULT;
  273. goto err;
  274. }
  275. if (packet->mad.hdr.id < 0 ||
  276. packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
  277. ret = -EINVAL;
  278. goto err;
  279. }
  280. down_read(&file->port->mutex);
  281. agent = __get_agent(file, packet->mad.hdr.id);
  282. if (!agent) {
  283. ret = -EINVAL;
  284. goto err_up;
  285. }
  286. memset(&ah_attr, 0, sizeof ah_attr);
  287. ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid);
  288. ah_attr.sl = packet->mad.hdr.sl;
  289. ah_attr.src_path_bits = packet->mad.hdr.path_bits;
  290. ah_attr.port_num = file->port->port_num;
  291. if (packet->mad.hdr.grh_present) {
  292. ah_attr.ah_flags = IB_AH_GRH;
  293. memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16);
  294. ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label);
  295. ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit;
  296. ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class;
  297. }
  298. ah = ib_create_ah(agent->qp->pd, &ah_attr);
  299. if (IS_ERR(ah)) {
  300. ret = PTR_ERR(ah);
  301. goto err_up;
  302. }
  303. rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
  304. if (rmpp_mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_ADM) {
  305. hdr_len = IB_MGMT_SA_HDR;
  306. copy_offset = IB_MGMT_RMPP_HDR;
  307. has_rmpp_header = 1;
  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. copy_offset = IB_MGMT_RMPP_HDR;
  312. has_rmpp_header = 1;
  313. } else {
  314. hdr_len = IB_MGMT_MAD_HDR;
  315. copy_offset = IB_MGMT_MAD_HDR;
  316. has_rmpp_header = 0;
  317. }
  318. if (has_rmpp_header)
  319. rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  320. IB_MGMT_RMPP_FLAG_ACTIVE;
  321. else
  322. rmpp_active = 0;
  323. /* Validate that the management class can support RMPP */
  324. if (rmpp_active && !agent->rmpp_version) {
  325. ret = -EINVAL;
  326. goto err_ah;
  327. }
  328. packet->msg = ib_create_send_mad(agent,
  329. be32_to_cpu(packet->mad.hdr.qpn),
  330. 0, rmpp_active,
  331. hdr_len, length - hdr_len,
  332. GFP_KERNEL);
  333. if (IS_ERR(packet->msg)) {
  334. ret = PTR_ERR(packet->msg);
  335. goto err_ah;
  336. }
  337. packet->msg->ah = ah;
  338. packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
  339. packet->msg->retries = packet->mad.hdr.retries;
  340. packet->msg->context[0] = packet;
  341. /* Copy MAD headers (RMPP header in place) */
  342. memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
  343. /* Now, copy rest of message from user into send buffer */
  344. if (copy_from_user(packet->msg->mad + copy_offset,
  345. buf + sizeof (struct ib_user_mad) + copy_offset,
  346. length - copy_offset)) {
  347. ret = -EFAULT;
  348. goto err_msg;
  349. }
  350. /*
  351. * If userspace is generating a request that will generate a
  352. * response, we need to make sure the high-order part of the
  353. * transaction ID matches the agent being used to send the
  354. * MAD.
  355. */
  356. method = ((struct ib_mad_hdr *) packet->msg->mad)->method;
  357. if (!(method & IB_MGMT_METHOD_RESP) &&
  358. method != IB_MGMT_METHOD_TRAP_REPRESS &&
  359. method != IB_MGMT_METHOD_SEND) {
  360. tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
  361. *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
  362. (be64_to_cpup(tid) & 0xffffffff));
  363. }
  364. ret = ib_post_send_mad(packet->msg, NULL);
  365. if (ret)
  366. goto err_msg;
  367. up_read(&file->port->mutex);
  368. return count;
  369. err_msg:
  370. ib_free_send_mad(packet->msg);
  371. err_ah:
  372. ib_destroy_ah(ah);
  373. err_up:
  374. up_read(&file->port->mutex);
  375. err:
  376. kfree(packet);
  377. return ret;
  378. }
  379. static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
  380. {
  381. struct ib_umad_file *file = filp->private_data;
  382. /* we will always be able to post a MAD send */
  383. unsigned int mask = POLLOUT | POLLWRNORM;
  384. poll_wait(filp, &file->recv_wait, wait);
  385. if (!list_empty(&file->recv_list))
  386. mask |= POLLIN | POLLRDNORM;
  387. return mask;
  388. }
  389. static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
  390. {
  391. struct ib_user_mad_reg_req ureq;
  392. struct ib_mad_reg_req req;
  393. struct ib_mad_agent *agent;
  394. int agent_id;
  395. int ret;
  396. down_write(&file->port->mutex);
  397. if (!file->port->ib_dev) {
  398. ret = -EPIPE;
  399. goto out;
  400. }
  401. if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) {
  402. ret = -EFAULT;
  403. goto out;
  404. }
  405. if (ureq.qpn != 0 && ureq.qpn != 1) {
  406. ret = -EINVAL;
  407. goto out;
  408. }
  409. for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
  410. if (!__get_agent(file, agent_id))
  411. goto found;
  412. ret = -ENOMEM;
  413. goto out;
  414. found:
  415. if (ureq.mgmt_class) {
  416. req.mgmt_class = ureq.mgmt_class;
  417. req.mgmt_class_version = ureq.mgmt_class_version;
  418. memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask);
  419. memcpy(req.oui, ureq.oui, sizeof req.oui);
  420. }
  421. agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
  422. ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
  423. ureq.mgmt_class ? &req : NULL,
  424. ureq.rmpp_version,
  425. send_handler, recv_handler, file);
  426. if (IS_ERR(agent)) {
  427. ret = PTR_ERR(agent);
  428. goto out;
  429. }
  430. if (put_user(agent_id,
  431. (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
  432. ret = -EFAULT;
  433. ib_unregister_mad_agent(agent);
  434. goto out;
  435. }
  436. file->agent[agent_id] = agent;
  437. ret = 0;
  438. out:
  439. up_write(&file->port->mutex);
  440. return ret;
  441. }
  442. static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
  443. {
  444. struct ib_mad_agent *agent = NULL;
  445. u32 id;
  446. int ret = 0;
  447. if (get_user(id, (u32 __user *) arg))
  448. return -EFAULT;
  449. down_write(&file->port->mutex);
  450. if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
  451. ret = -EINVAL;
  452. goto out;
  453. }
  454. agent = file->agent[id];
  455. file->agent[id] = NULL;
  456. out:
  457. up_write(&file->port->mutex);
  458. if (agent)
  459. ib_unregister_mad_agent(agent);
  460. return ret;
  461. }
  462. static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
  463. unsigned long arg)
  464. {
  465. switch (cmd) {
  466. case IB_USER_MAD_REGISTER_AGENT:
  467. return ib_umad_reg_agent(filp->private_data, arg);
  468. case IB_USER_MAD_UNREGISTER_AGENT:
  469. return ib_umad_unreg_agent(filp->private_data, arg);
  470. default:
  471. return -ENOIOCTLCMD;
  472. }
  473. }
  474. static int ib_umad_open(struct inode *inode, struct file *filp)
  475. {
  476. struct ib_umad_port *port;
  477. struct ib_umad_file *file;
  478. int ret = 0;
  479. spin_lock(&port_lock);
  480. port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE];
  481. if (port)
  482. kref_get(&port->umad_dev->ref);
  483. spin_unlock(&port_lock);
  484. if (!port)
  485. return -ENXIO;
  486. down_write(&port->mutex);
  487. if (!port->ib_dev) {
  488. ret = -ENXIO;
  489. goto out;
  490. }
  491. file = kzalloc(sizeof *file, GFP_KERNEL);
  492. if (!file) {
  493. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  494. ret = -ENOMEM;
  495. goto out;
  496. }
  497. spin_lock_init(&file->recv_lock);
  498. INIT_LIST_HEAD(&file->recv_list);
  499. init_waitqueue_head(&file->recv_wait);
  500. file->port = port;
  501. filp->private_data = file;
  502. list_add_tail(&file->port_list, &port->file_list);
  503. out:
  504. up_write(&port->mutex);
  505. return ret;
  506. }
  507. static int ib_umad_close(struct inode *inode, struct file *filp)
  508. {
  509. struct ib_umad_file *file = filp->private_data;
  510. struct ib_umad_device *dev = file->port->umad_dev;
  511. struct ib_umad_packet *packet, *tmp;
  512. int already_dead;
  513. int i;
  514. down_write(&file->port->mutex);
  515. already_dead = file->agents_dead;
  516. file->agents_dead = 1;
  517. list_for_each_entry_safe(packet, tmp, &file->recv_list, list)
  518. kfree(packet);
  519. list_del(&file->port_list);
  520. downgrade_write(&file->port->mutex);
  521. if (!already_dead)
  522. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  523. if (file->agent[i])
  524. ib_unregister_mad_agent(file->agent[i]);
  525. up_read(&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->sm_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. /*
  705. * Now go through the list of files attached to this port and
  706. * unregister all of their MAD agents. We need to hold
  707. * port->mutex while doing this to avoid racing with
  708. * ib_umad_close(), but we can't hold the mutex for writing
  709. * while calling ib_unregister_mad_agent(), since that might
  710. * deadlock by calling back into queue_packet(). So we
  711. * downgrade our lock to a read lock, and then drop and
  712. * reacquire the write lock for the next iteration.
  713. *
  714. * We do list_del_init() on the file's list_head so that the
  715. * list_del in ib_umad_close() is still OK, even after the
  716. * file is removed from the list.
  717. */
  718. while (!list_empty(&port->file_list)) {
  719. file = list_entry(port->file_list.next, struct ib_umad_file,
  720. port_list);
  721. file->agents_dead = 1;
  722. list_del_init(&file->port_list);
  723. downgrade_write(&port->mutex);
  724. for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
  725. if (file->agent[id])
  726. ib_unregister_mad_agent(file->agent[id]);
  727. up_read(&port->mutex);
  728. down_write(&port->mutex);
  729. }
  730. up_write(&port->mutex);
  731. clear_bit(port->dev_num, dev_map);
  732. }
  733. static void ib_umad_add_one(struct ib_device *device)
  734. {
  735. struct ib_umad_device *umad_dev;
  736. int s, e, i;
  737. if (device->node_type == IB_NODE_SWITCH)
  738. s = e = 0;
  739. else {
  740. s = 1;
  741. e = device->phys_port_cnt;
  742. }
  743. umad_dev = kzalloc(sizeof *umad_dev +
  744. (e - s + 1) * sizeof (struct ib_umad_port),
  745. GFP_KERNEL);
  746. if (!umad_dev)
  747. return;
  748. kref_init(&umad_dev->ref);
  749. umad_dev->start_port = s;
  750. umad_dev->end_port = e;
  751. for (i = s; i <= e; ++i) {
  752. umad_dev->port[i - s].umad_dev = umad_dev;
  753. if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
  754. goto err;
  755. }
  756. ib_set_client_data(device, &umad_client, umad_dev);
  757. return;
  758. err:
  759. while (--i >= s)
  760. ib_umad_kill_port(&umad_dev->port[i - s]);
  761. kref_put(&umad_dev->ref, ib_umad_release_dev);
  762. }
  763. static void ib_umad_remove_one(struct ib_device *device)
  764. {
  765. struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
  766. int i;
  767. if (!umad_dev)
  768. return;
  769. for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
  770. ib_umad_kill_port(&umad_dev->port[i]);
  771. kref_put(&umad_dev->ref, ib_umad_release_dev);
  772. }
  773. static int __init ib_umad_init(void)
  774. {
  775. int ret;
  776. ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
  777. "infiniband_mad");
  778. if (ret) {
  779. printk(KERN_ERR "user_mad: couldn't register device number\n");
  780. goto out;
  781. }
  782. umad_class = class_create(THIS_MODULE, "infiniband_mad");
  783. if (IS_ERR(umad_class)) {
  784. ret = PTR_ERR(umad_class);
  785. printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
  786. goto out_chrdev;
  787. }
  788. ret = class_create_file(umad_class, &class_attr_abi_version);
  789. if (ret) {
  790. printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
  791. goto out_class;
  792. }
  793. ret = ib_register_client(&umad_client);
  794. if (ret) {
  795. printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
  796. goto out_class;
  797. }
  798. return 0;
  799. out_class:
  800. class_destroy(umad_class);
  801. out_chrdev:
  802. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  803. out:
  804. return ret;
  805. }
  806. static void __exit ib_umad_cleanup(void)
  807. {
  808. ib_unregister_client(&umad_client);
  809. class_destroy(umad_class);
  810. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  811. }
  812. module_init(ib_umad_init);
  813. module_exit(ib_umad_cleanup);