user_mad.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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 5596 2006-03-03 01:00:07Z sean.hefty $
  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 send_list;
  103. struct list_head port_list;
  104. spinlock_t recv_lock;
  105. spinlock_t send_lock;
  106. wait_queue_head_t recv_wait;
  107. struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];
  108. int agents_dead;
  109. };
  110. struct ib_umad_packet {
  111. struct ib_mad_send_buf *msg;
  112. struct ib_mad_recv_wc *recv_wc;
  113. struct list_head list;
  114. int length;
  115. struct ib_user_mad mad;
  116. };
  117. static struct class *umad_class;
  118. static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
  119. static DEFINE_SPINLOCK(port_lock);
  120. static struct ib_umad_port *umad_port[IB_UMAD_MAX_PORTS];
  121. static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS * 2);
  122. static void ib_umad_add_one(struct ib_device *device);
  123. static void ib_umad_remove_one(struct ib_device *device);
  124. static void ib_umad_release_dev(struct kref *ref)
  125. {
  126. struct ib_umad_device *dev =
  127. container_of(ref, struct ib_umad_device, ref);
  128. kfree(dev);
  129. }
  130. /* caller must hold port->mutex at least for reading */
  131. static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
  132. {
  133. return file->agents_dead ? NULL : file->agent[id];
  134. }
  135. static int queue_packet(struct ib_umad_file *file,
  136. struct ib_mad_agent *agent,
  137. struct ib_umad_packet *packet)
  138. {
  139. int ret = 1;
  140. down_read(&file->port->mutex);
  141. for (packet->mad.hdr.id = 0;
  142. packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
  143. packet->mad.hdr.id++)
  144. if (agent == __get_agent(file, packet->mad.hdr.id)) {
  145. spin_lock_irq(&file->recv_lock);
  146. list_add_tail(&packet->list, &file->recv_list);
  147. spin_unlock_irq(&file->recv_lock);
  148. wake_up_interruptible(&file->recv_wait);
  149. ret = 0;
  150. break;
  151. }
  152. up_read(&file->port->mutex);
  153. return ret;
  154. }
  155. static void dequeue_send(struct ib_umad_file *file,
  156. struct ib_umad_packet *packet)
  157. {
  158. spin_lock_irq(&file->send_lock);
  159. list_del(&packet->list);
  160. spin_unlock_irq(&file->send_lock);
  161. }
  162. static void send_handler(struct ib_mad_agent *agent,
  163. struct ib_mad_send_wc *send_wc)
  164. {
  165. struct ib_umad_file *file = agent->context;
  166. struct ib_umad_packet *packet = send_wc->send_buf->context[0];
  167. dequeue_send(file, packet);
  168. ib_destroy_ah(packet->msg->ah);
  169. ib_free_send_mad(packet->msg);
  170. if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
  171. packet->length = IB_MGMT_MAD_HDR;
  172. packet->mad.hdr.status = ETIMEDOUT;
  173. if (!queue_packet(file, agent, packet))
  174. return;
  175. }
  176. kfree(packet);
  177. }
  178. static void recv_handler(struct ib_mad_agent *agent,
  179. struct ib_mad_recv_wc *mad_recv_wc)
  180. {
  181. struct ib_umad_file *file = agent->context;
  182. struct ib_umad_packet *packet;
  183. if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
  184. goto err1;
  185. packet = kzalloc(sizeof *packet, GFP_KERNEL);
  186. if (!packet)
  187. goto err1;
  188. packet->length = mad_recv_wc->mad_len;
  189. packet->recv_wc = mad_recv_wc;
  190. packet->mad.hdr.status = 0;
  191. packet->mad.hdr.length = sizeof (struct ib_user_mad) +
  192. mad_recv_wc->mad_len;
  193. packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
  194. packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);
  195. packet->mad.hdr.sl = mad_recv_wc->wc->sl;
  196. packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
  197. packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
  198. if (packet->mad.hdr.grh_present) {
  199. /* XXX parse GRH */
  200. packet->mad.hdr.gid_index = 0;
  201. packet->mad.hdr.hop_limit = 0;
  202. packet->mad.hdr.traffic_class = 0;
  203. memset(packet->mad.hdr.gid, 0, 16);
  204. packet->mad.hdr.flow_label = 0;
  205. }
  206. if (queue_packet(file, agent, packet))
  207. goto err2;
  208. return;
  209. err2:
  210. kfree(packet);
  211. err1:
  212. ib_free_recv_mad(mad_recv_wc);
  213. }
  214. static ssize_t copy_recv_mad(char __user *buf, struct ib_umad_packet *packet,
  215. size_t count)
  216. {
  217. struct ib_mad_recv_buf *recv_buf;
  218. int left, seg_payload, offset, max_seg_payload;
  219. /* We need enough room to copy the first (or only) MAD segment. */
  220. recv_buf = &packet->recv_wc->recv_buf;
  221. if ((packet->length <= sizeof (*recv_buf->mad) &&
  222. count < sizeof (packet->mad) + packet->length) ||
  223. (packet->length > sizeof (*recv_buf->mad) &&
  224. count < sizeof (packet->mad) + sizeof (*recv_buf->mad)))
  225. return -EINVAL;
  226. if (copy_to_user(buf, &packet->mad, sizeof (packet->mad)))
  227. return -EFAULT;
  228. buf += sizeof (packet->mad);
  229. seg_payload = min_t(int, packet->length, sizeof (*recv_buf->mad));
  230. if (copy_to_user(buf, recv_buf->mad, seg_payload))
  231. return -EFAULT;
  232. if (seg_payload < packet->length) {
  233. /*
  234. * Multipacket RMPP MAD message. Copy remainder of message.
  235. * Note that last segment may have a shorter payload.
  236. */
  237. if (count < sizeof (packet->mad) + packet->length) {
  238. /*
  239. * The buffer is too small, return the first RMPP segment,
  240. * which includes the RMPP message length.
  241. */
  242. return -ENOSPC;
  243. }
  244. offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);
  245. max_seg_payload = sizeof (struct ib_mad) - offset;
  246. for (left = packet->length - seg_payload, buf += seg_payload;
  247. left; left -= seg_payload, buf += seg_payload) {
  248. recv_buf = container_of(recv_buf->list.next,
  249. struct ib_mad_recv_buf, list);
  250. seg_payload = min(left, max_seg_payload);
  251. if (copy_to_user(buf, ((void *) recv_buf->mad) + offset,
  252. seg_payload))
  253. return -EFAULT;
  254. }
  255. }
  256. return sizeof (packet->mad) + packet->length;
  257. }
  258. static ssize_t copy_send_mad(char __user *buf, struct ib_umad_packet *packet,
  259. size_t count)
  260. {
  261. ssize_t size = sizeof (packet->mad) + packet->length;
  262. if (count < size)
  263. return -EINVAL;
  264. if (copy_to_user(buf, &packet->mad, size))
  265. return -EFAULT;
  266. return size;
  267. }
  268. static ssize_t ib_umad_read(struct file *filp, char __user *buf,
  269. size_t count, loff_t *pos)
  270. {
  271. struct ib_umad_file *file = filp->private_data;
  272. struct ib_umad_packet *packet;
  273. ssize_t ret;
  274. if (count < sizeof (struct ib_user_mad))
  275. return -EINVAL;
  276. spin_lock_irq(&file->recv_lock);
  277. while (list_empty(&file->recv_list)) {
  278. spin_unlock_irq(&file->recv_lock);
  279. if (filp->f_flags & O_NONBLOCK)
  280. return -EAGAIN;
  281. if (wait_event_interruptible(file->recv_wait,
  282. !list_empty(&file->recv_list)))
  283. return -ERESTARTSYS;
  284. spin_lock_irq(&file->recv_lock);
  285. }
  286. packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
  287. list_del(&packet->list);
  288. spin_unlock_irq(&file->recv_lock);
  289. if (packet->recv_wc)
  290. ret = copy_recv_mad(buf, packet, count);
  291. else
  292. ret = copy_send_mad(buf, packet, count);
  293. if (ret < 0) {
  294. /* Requeue packet */
  295. spin_lock_irq(&file->recv_lock);
  296. list_add(&packet->list, &file->recv_list);
  297. spin_unlock_irq(&file->recv_lock);
  298. } else {
  299. if (packet->recv_wc)
  300. ib_free_recv_mad(packet->recv_wc);
  301. kfree(packet);
  302. }
  303. return ret;
  304. }
  305. static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
  306. {
  307. int left, seg;
  308. /* Copy class specific header */
  309. if ((msg->hdr_len > IB_MGMT_RMPP_HDR) &&
  310. copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR,
  311. msg->hdr_len - IB_MGMT_RMPP_HDR))
  312. return -EFAULT;
  313. /* All headers are in place. Copy data segments. */
  314. for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0;
  315. seg++, left -= msg->seg_size, buf += msg->seg_size) {
  316. if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf,
  317. min(left, msg->seg_size)))
  318. return -EFAULT;
  319. }
  320. return 0;
  321. }
  322. static int same_destination(struct ib_user_mad_hdr *hdr1,
  323. struct ib_user_mad_hdr *hdr2)
  324. {
  325. if (!hdr1->grh_present && !hdr2->grh_present)
  326. return (hdr1->lid == hdr2->lid);
  327. if (hdr1->grh_present && hdr2->grh_present)
  328. return !memcmp(hdr1->gid, hdr2->gid, 16);
  329. return 0;
  330. }
  331. static int is_duplicate(struct ib_umad_file *file,
  332. struct ib_umad_packet *packet)
  333. {
  334. struct ib_umad_packet *sent_packet;
  335. struct ib_mad_hdr *sent_hdr, *hdr;
  336. hdr = (struct ib_mad_hdr *) packet->mad.data;
  337. list_for_each_entry(sent_packet, &file->send_list, list) {
  338. sent_hdr = (struct ib_mad_hdr *) sent_packet->mad.data;
  339. if ((hdr->tid != sent_hdr->tid) ||
  340. (hdr->mgmt_class != sent_hdr->mgmt_class))
  341. continue;
  342. /*
  343. * No need to be overly clever here. If two new operations have
  344. * the same TID, reject the second as a duplicate. This is more
  345. * restrictive than required by the spec.
  346. */
  347. if (!ib_response_mad((struct ib_mad *) hdr)) {
  348. if (!ib_response_mad((struct ib_mad *) sent_hdr))
  349. return 1;
  350. continue;
  351. } else if (!ib_response_mad((struct ib_mad *) sent_hdr))
  352. continue;
  353. if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))
  354. return 1;
  355. }
  356. return 0;
  357. }
  358. static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
  359. size_t count, loff_t *pos)
  360. {
  361. struct ib_umad_file *file = filp->private_data;
  362. struct ib_umad_packet *packet;
  363. struct ib_mad_agent *agent;
  364. struct ib_ah_attr ah_attr;
  365. struct ib_ah *ah;
  366. struct ib_rmpp_mad *rmpp_mad;
  367. __be64 *tid;
  368. int ret, data_len, hdr_len, copy_offset, rmpp_active;
  369. if (count < sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)
  370. return -EINVAL;
  371. packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
  372. if (!packet)
  373. return -ENOMEM;
  374. if (copy_from_user(&packet->mad, buf,
  375. sizeof (struct ib_user_mad) + IB_MGMT_RMPP_HDR)) {
  376. ret = -EFAULT;
  377. goto err;
  378. }
  379. if (packet->mad.hdr.id < 0 ||
  380. packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
  381. ret = -EINVAL;
  382. goto err;
  383. }
  384. down_read(&file->port->mutex);
  385. agent = __get_agent(file, packet->mad.hdr.id);
  386. if (!agent) {
  387. ret = -EINVAL;
  388. goto err_up;
  389. }
  390. memset(&ah_attr, 0, sizeof ah_attr);
  391. ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid);
  392. ah_attr.sl = packet->mad.hdr.sl;
  393. ah_attr.src_path_bits = packet->mad.hdr.path_bits;
  394. ah_attr.port_num = file->port->port_num;
  395. if (packet->mad.hdr.grh_present) {
  396. ah_attr.ah_flags = IB_AH_GRH;
  397. memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16);
  398. ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label);
  399. ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit;
  400. ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class;
  401. }
  402. ah = ib_create_ah(agent->qp->pd, &ah_attr);
  403. if (IS_ERR(ah)) {
  404. ret = PTR_ERR(ah);
  405. goto err_up;
  406. }
  407. rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
  408. hdr_len = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
  409. if (!ib_is_mad_class_rmpp(rmpp_mad->mad_hdr.mgmt_class)) {
  410. copy_offset = IB_MGMT_MAD_HDR;
  411. rmpp_active = 0;
  412. } else {
  413. copy_offset = IB_MGMT_RMPP_HDR;
  414. rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  415. IB_MGMT_RMPP_FLAG_ACTIVE;
  416. }
  417. data_len = count - sizeof (struct ib_user_mad) - hdr_len;
  418. packet->msg = ib_create_send_mad(agent,
  419. be32_to_cpu(packet->mad.hdr.qpn),
  420. 0, rmpp_active, hdr_len,
  421. data_len, GFP_KERNEL);
  422. if (IS_ERR(packet->msg)) {
  423. ret = PTR_ERR(packet->msg);
  424. goto err_ah;
  425. }
  426. packet->msg->ah = ah;
  427. packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
  428. packet->msg->retries = packet->mad.hdr.retries;
  429. packet->msg->context[0] = packet;
  430. /* Copy MAD header. Any RMPP header is already in place. */
  431. memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
  432. buf += sizeof (struct ib_user_mad);
  433. if (!rmpp_active) {
  434. if (copy_from_user(packet->msg->mad + copy_offset,
  435. buf + copy_offset,
  436. hdr_len + data_len - copy_offset)) {
  437. ret = -EFAULT;
  438. goto err_msg;
  439. }
  440. } else {
  441. ret = copy_rmpp_mad(packet->msg, buf);
  442. if (ret)
  443. goto err_msg;
  444. }
  445. /*
  446. * Set the high-order part of the transaction ID to make MADs from
  447. * different agents unique, and allow routing responses back to the
  448. * original requestor.
  449. */
  450. if (!ib_response_mad(packet->msg->mad)) {
  451. tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
  452. *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
  453. (be64_to_cpup(tid) & 0xffffffff));
  454. rmpp_mad->mad_hdr.tid = *tid;
  455. }
  456. spin_lock_irq(&file->send_lock);
  457. ret = is_duplicate(file, packet);
  458. if (!ret)
  459. list_add_tail(&packet->list, &file->send_list);
  460. spin_unlock_irq(&file->send_lock);
  461. if (ret) {
  462. ret = -EINVAL;
  463. goto err_msg;
  464. }
  465. ret = ib_post_send_mad(packet->msg, NULL);
  466. if (ret)
  467. goto err_send;
  468. up_read(&file->port->mutex);
  469. return count;
  470. err_send:
  471. dequeue_send(file, packet);
  472. err_msg:
  473. ib_free_send_mad(packet->msg);
  474. err_ah:
  475. ib_destroy_ah(ah);
  476. err_up:
  477. up_read(&file->port->mutex);
  478. err:
  479. kfree(packet);
  480. return ret;
  481. }
  482. static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
  483. {
  484. struct ib_umad_file *file = filp->private_data;
  485. /* we will always be able to post a MAD send */
  486. unsigned int mask = POLLOUT | POLLWRNORM;
  487. poll_wait(filp, &file->recv_wait, wait);
  488. if (!list_empty(&file->recv_list))
  489. mask |= POLLIN | POLLRDNORM;
  490. return mask;
  491. }
  492. static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
  493. {
  494. struct ib_user_mad_reg_req ureq;
  495. struct ib_mad_reg_req req;
  496. struct ib_mad_agent *agent;
  497. int agent_id;
  498. int ret;
  499. down_write(&file->port->mutex);
  500. if (!file->port->ib_dev) {
  501. ret = -EPIPE;
  502. goto out;
  503. }
  504. if (copy_from_user(&ureq, (void __user *) arg, sizeof ureq)) {
  505. ret = -EFAULT;
  506. goto out;
  507. }
  508. if (ureq.qpn != 0 && ureq.qpn != 1) {
  509. ret = -EINVAL;
  510. goto out;
  511. }
  512. for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
  513. if (!__get_agent(file, agent_id))
  514. goto found;
  515. ret = -ENOMEM;
  516. goto out;
  517. found:
  518. if (ureq.mgmt_class) {
  519. req.mgmt_class = ureq.mgmt_class;
  520. req.mgmt_class_version = ureq.mgmt_class_version;
  521. memcpy(req.method_mask, ureq.method_mask, sizeof req.method_mask);
  522. memcpy(req.oui, ureq.oui, sizeof req.oui);
  523. }
  524. agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
  525. ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
  526. ureq.mgmt_class ? &req : NULL,
  527. ureq.rmpp_version,
  528. send_handler, recv_handler, file);
  529. if (IS_ERR(agent)) {
  530. ret = PTR_ERR(agent);
  531. goto out;
  532. }
  533. if (put_user(agent_id,
  534. (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
  535. ret = -EFAULT;
  536. ib_unregister_mad_agent(agent);
  537. goto out;
  538. }
  539. file->agent[agent_id] = agent;
  540. ret = 0;
  541. out:
  542. up_write(&file->port->mutex);
  543. return ret;
  544. }
  545. static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
  546. {
  547. struct ib_mad_agent *agent = NULL;
  548. u32 id;
  549. int ret = 0;
  550. if (get_user(id, (u32 __user *) arg))
  551. return -EFAULT;
  552. down_write(&file->port->mutex);
  553. if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
  554. ret = -EINVAL;
  555. goto out;
  556. }
  557. agent = file->agent[id];
  558. file->agent[id] = NULL;
  559. out:
  560. up_write(&file->port->mutex);
  561. if (agent)
  562. ib_unregister_mad_agent(agent);
  563. return ret;
  564. }
  565. static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
  566. unsigned long arg)
  567. {
  568. switch (cmd) {
  569. case IB_USER_MAD_REGISTER_AGENT:
  570. return ib_umad_reg_agent(filp->private_data, arg);
  571. case IB_USER_MAD_UNREGISTER_AGENT:
  572. return ib_umad_unreg_agent(filp->private_data, arg);
  573. default:
  574. return -ENOIOCTLCMD;
  575. }
  576. }
  577. static int ib_umad_open(struct inode *inode, struct file *filp)
  578. {
  579. struct ib_umad_port *port;
  580. struct ib_umad_file *file;
  581. int ret = 0;
  582. spin_lock(&port_lock);
  583. port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE];
  584. if (port)
  585. kref_get(&port->umad_dev->ref);
  586. spin_unlock(&port_lock);
  587. if (!port)
  588. return -ENXIO;
  589. down_write(&port->mutex);
  590. if (!port->ib_dev) {
  591. ret = -ENXIO;
  592. goto out;
  593. }
  594. file = kzalloc(sizeof *file, GFP_KERNEL);
  595. if (!file) {
  596. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  597. ret = -ENOMEM;
  598. goto out;
  599. }
  600. spin_lock_init(&file->recv_lock);
  601. spin_lock_init(&file->send_lock);
  602. INIT_LIST_HEAD(&file->recv_list);
  603. INIT_LIST_HEAD(&file->send_list);
  604. init_waitqueue_head(&file->recv_wait);
  605. file->port = port;
  606. filp->private_data = file;
  607. list_add_tail(&file->port_list, &port->file_list);
  608. out:
  609. up_write(&port->mutex);
  610. return ret;
  611. }
  612. static int ib_umad_close(struct inode *inode, struct file *filp)
  613. {
  614. struct ib_umad_file *file = filp->private_data;
  615. struct ib_umad_device *dev = file->port->umad_dev;
  616. struct ib_umad_packet *packet, *tmp;
  617. int already_dead;
  618. int i;
  619. down_write(&file->port->mutex);
  620. already_dead = file->agents_dead;
  621. file->agents_dead = 1;
  622. list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {
  623. if (packet->recv_wc)
  624. ib_free_recv_mad(packet->recv_wc);
  625. kfree(packet);
  626. }
  627. list_del(&file->port_list);
  628. downgrade_write(&file->port->mutex);
  629. if (!already_dead)
  630. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  631. if (file->agent[i])
  632. ib_unregister_mad_agent(file->agent[i]);
  633. up_read(&file->port->mutex);
  634. kfree(file);
  635. kref_put(&dev->ref, ib_umad_release_dev);
  636. return 0;
  637. }
  638. static struct file_operations umad_fops = {
  639. .owner = THIS_MODULE,
  640. .read = ib_umad_read,
  641. .write = ib_umad_write,
  642. .poll = ib_umad_poll,
  643. .unlocked_ioctl = ib_umad_ioctl,
  644. .compat_ioctl = ib_umad_ioctl,
  645. .open = ib_umad_open,
  646. .release = ib_umad_close
  647. };
  648. static int ib_umad_sm_open(struct inode *inode, struct file *filp)
  649. {
  650. struct ib_umad_port *port;
  651. struct ib_port_modify props = {
  652. .set_port_cap_mask = IB_PORT_SM
  653. };
  654. int ret;
  655. spin_lock(&port_lock);
  656. port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS];
  657. if (port)
  658. kref_get(&port->umad_dev->ref);
  659. spin_unlock(&port_lock);
  660. if (!port)
  661. return -ENXIO;
  662. if (filp->f_flags & O_NONBLOCK) {
  663. if (down_trylock(&port->sm_sem)) {
  664. ret = -EAGAIN;
  665. goto fail;
  666. }
  667. } else {
  668. if (down_interruptible(&port->sm_sem)) {
  669. ret = -ERESTARTSYS;
  670. goto fail;
  671. }
  672. }
  673. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  674. if (ret) {
  675. up(&port->sm_sem);
  676. goto fail;
  677. }
  678. filp->private_data = port;
  679. return 0;
  680. fail:
  681. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  682. return ret;
  683. }
  684. static int ib_umad_sm_close(struct inode *inode, struct file *filp)
  685. {
  686. struct ib_umad_port *port = filp->private_data;
  687. struct ib_port_modify props = {
  688. .clr_port_cap_mask = IB_PORT_SM
  689. };
  690. int ret = 0;
  691. down_write(&port->mutex);
  692. if (port->ib_dev)
  693. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  694. up_write(&port->mutex);
  695. up(&port->sm_sem);
  696. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  697. return ret;
  698. }
  699. static struct file_operations umad_sm_fops = {
  700. .owner = THIS_MODULE,
  701. .open = ib_umad_sm_open,
  702. .release = ib_umad_sm_close
  703. };
  704. static struct ib_client umad_client = {
  705. .name = "umad",
  706. .add = ib_umad_add_one,
  707. .remove = ib_umad_remove_one
  708. };
  709. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  710. {
  711. struct ib_umad_port *port = class_get_devdata(class_dev);
  712. if (!port)
  713. return -ENODEV;
  714. return sprintf(buf, "%s\n", port->ib_dev->name);
  715. }
  716. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  717. static ssize_t show_port(struct class_device *class_dev, char *buf)
  718. {
  719. struct ib_umad_port *port = class_get_devdata(class_dev);
  720. if (!port)
  721. return -ENODEV;
  722. return sprintf(buf, "%d\n", port->port_num);
  723. }
  724. static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
  725. static ssize_t show_abi_version(struct class *class, char *buf)
  726. {
  727. return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
  728. }
  729. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  730. static int ib_umad_init_port(struct ib_device *device, int port_num,
  731. struct ib_umad_port *port)
  732. {
  733. spin_lock(&port_lock);
  734. port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
  735. if (port->dev_num >= IB_UMAD_MAX_PORTS) {
  736. spin_unlock(&port_lock);
  737. return -1;
  738. }
  739. set_bit(port->dev_num, dev_map);
  740. spin_unlock(&port_lock);
  741. port->ib_dev = device;
  742. port->port_num = port_num;
  743. init_MUTEX(&port->sm_sem);
  744. init_rwsem(&port->mutex);
  745. INIT_LIST_HEAD(&port->file_list);
  746. port->dev = cdev_alloc();
  747. if (!port->dev)
  748. return -1;
  749. port->dev->owner = THIS_MODULE;
  750. port->dev->ops = &umad_fops;
  751. kobject_set_name(&port->dev->kobj, "umad%d", port->dev_num);
  752. if (cdev_add(port->dev, base_dev + port->dev_num, 1))
  753. goto err_cdev;
  754. port->class_dev = class_device_create(umad_class, NULL, port->dev->dev,
  755. device->dma_device,
  756. "umad%d", port->dev_num);
  757. if (IS_ERR(port->class_dev))
  758. goto err_cdev;
  759. if (class_device_create_file(port->class_dev, &class_device_attr_ibdev))
  760. goto err_class;
  761. if (class_device_create_file(port->class_dev, &class_device_attr_port))
  762. goto err_class;
  763. port->sm_dev = cdev_alloc();
  764. if (!port->sm_dev)
  765. goto err_class;
  766. port->sm_dev->owner = THIS_MODULE;
  767. port->sm_dev->ops = &umad_sm_fops;
  768. kobject_set_name(&port->sm_dev->kobj, "issm%d", port->dev_num);
  769. if (cdev_add(port->sm_dev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1))
  770. goto err_sm_cdev;
  771. port->sm_class_dev = class_device_create(umad_class, NULL, port->sm_dev->dev,
  772. device->dma_device,
  773. "issm%d", port->dev_num);
  774. if (IS_ERR(port->sm_class_dev))
  775. goto err_sm_cdev;
  776. class_set_devdata(port->class_dev, port);
  777. class_set_devdata(port->sm_class_dev, port);
  778. if (class_device_create_file(port->sm_class_dev, &class_device_attr_ibdev))
  779. goto err_sm_class;
  780. if (class_device_create_file(port->sm_class_dev, &class_device_attr_port))
  781. goto err_sm_class;
  782. spin_lock(&port_lock);
  783. umad_port[port->dev_num] = port;
  784. spin_unlock(&port_lock);
  785. return 0;
  786. err_sm_class:
  787. class_device_destroy(umad_class, port->sm_dev->dev);
  788. err_sm_cdev:
  789. cdev_del(port->sm_dev);
  790. err_class:
  791. class_device_destroy(umad_class, port->dev->dev);
  792. err_cdev:
  793. cdev_del(port->dev);
  794. clear_bit(port->dev_num, dev_map);
  795. return -1;
  796. }
  797. static void ib_umad_kill_port(struct ib_umad_port *port)
  798. {
  799. struct ib_umad_file *file;
  800. int id;
  801. class_set_devdata(port->class_dev, NULL);
  802. class_set_devdata(port->sm_class_dev, NULL);
  803. class_device_destroy(umad_class, port->dev->dev);
  804. class_device_destroy(umad_class, port->sm_dev->dev);
  805. cdev_del(port->dev);
  806. cdev_del(port->sm_dev);
  807. spin_lock(&port_lock);
  808. umad_port[port->dev_num] = NULL;
  809. spin_unlock(&port_lock);
  810. down_write(&port->mutex);
  811. port->ib_dev = NULL;
  812. /*
  813. * Now go through the list of files attached to this port and
  814. * unregister all of their MAD agents. We need to hold
  815. * port->mutex while doing this to avoid racing with
  816. * ib_umad_close(), but we can't hold the mutex for writing
  817. * while calling ib_unregister_mad_agent(), since that might
  818. * deadlock by calling back into queue_packet(). So we
  819. * downgrade our lock to a read lock, and then drop and
  820. * reacquire the write lock for the next iteration.
  821. *
  822. * We do list_del_init() on the file's list_head so that the
  823. * list_del in ib_umad_close() is still OK, even after the
  824. * file is removed from the list.
  825. */
  826. while (!list_empty(&port->file_list)) {
  827. file = list_entry(port->file_list.next, struct ib_umad_file,
  828. port_list);
  829. file->agents_dead = 1;
  830. list_del_init(&file->port_list);
  831. downgrade_write(&port->mutex);
  832. for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
  833. if (file->agent[id])
  834. ib_unregister_mad_agent(file->agent[id]);
  835. up_read(&port->mutex);
  836. down_write(&port->mutex);
  837. }
  838. up_write(&port->mutex);
  839. clear_bit(port->dev_num, dev_map);
  840. }
  841. static void ib_umad_add_one(struct ib_device *device)
  842. {
  843. struct ib_umad_device *umad_dev;
  844. int s, e, i;
  845. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  846. return;
  847. if (device->node_type == RDMA_NODE_IB_SWITCH)
  848. s = e = 0;
  849. else {
  850. s = 1;
  851. e = device->phys_port_cnt;
  852. }
  853. umad_dev = kzalloc(sizeof *umad_dev +
  854. (e - s + 1) * sizeof (struct ib_umad_port),
  855. GFP_KERNEL);
  856. if (!umad_dev)
  857. return;
  858. kref_init(&umad_dev->ref);
  859. umad_dev->start_port = s;
  860. umad_dev->end_port = e;
  861. for (i = s; i <= e; ++i) {
  862. umad_dev->port[i - s].umad_dev = umad_dev;
  863. if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
  864. goto err;
  865. }
  866. ib_set_client_data(device, &umad_client, umad_dev);
  867. return;
  868. err:
  869. while (--i >= s)
  870. ib_umad_kill_port(&umad_dev->port[i - s]);
  871. kref_put(&umad_dev->ref, ib_umad_release_dev);
  872. }
  873. static void ib_umad_remove_one(struct ib_device *device)
  874. {
  875. struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
  876. int i;
  877. if (!umad_dev)
  878. return;
  879. for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
  880. ib_umad_kill_port(&umad_dev->port[i]);
  881. kref_put(&umad_dev->ref, ib_umad_release_dev);
  882. }
  883. static int __init ib_umad_init(void)
  884. {
  885. int ret;
  886. ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
  887. "infiniband_mad");
  888. if (ret) {
  889. printk(KERN_ERR "user_mad: couldn't register device number\n");
  890. goto out;
  891. }
  892. umad_class = class_create(THIS_MODULE, "infiniband_mad");
  893. if (IS_ERR(umad_class)) {
  894. ret = PTR_ERR(umad_class);
  895. printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
  896. goto out_chrdev;
  897. }
  898. ret = class_create_file(umad_class, &class_attr_abi_version);
  899. if (ret) {
  900. printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
  901. goto out_class;
  902. }
  903. ret = ib_register_client(&umad_client);
  904. if (ret) {
  905. printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
  906. goto out_class;
  907. }
  908. return 0;
  909. out_class:
  910. class_destroy(umad_class);
  911. out_chrdev:
  912. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  913. out:
  914. return ret;
  915. }
  916. static void __exit ib_umad_cleanup(void)
  917. {
  918. ib_unregister_client(&umad_client);
  919. class_destroy(umad_class);
  920. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  921. }
  922. module_init(ib_umad_init);
  923. module_exit(ib_umad_cleanup);