user_mad.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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. * Copyright (c) 2008 Cisco. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. *
  35. * $Id: user_mad.c 5596 2006-03-03 01:00:07Z sean.hefty $
  36. */
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/device.h>
  40. #include <linux/err.h>
  41. #include <linux/fs.h>
  42. #include <linux/cdev.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/poll.h>
  45. #include <linux/mutex.h>
  46. #include <linux/kref.h>
  47. #include <linux/compat.h>
  48. #include <linux/semaphore.h>
  49. #include <asm/uaccess.h>
  50. #include <rdma/ib_mad.h>
  51. #include <rdma/ib_user_mad.h>
  52. MODULE_AUTHOR("Roland Dreier");
  53. MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
  54. MODULE_LICENSE("Dual BSD/GPL");
  55. enum {
  56. IB_UMAD_MAX_PORTS = 64,
  57. IB_UMAD_MAX_AGENTS = 32,
  58. IB_UMAD_MAJOR = 231,
  59. IB_UMAD_MINOR_BASE = 0
  60. };
  61. /*
  62. * Our lifetime rules for these structs are the following: each time a
  63. * device special file is opened, we look up the corresponding struct
  64. * ib_umad_port by minor in the umad_port[] table while holding the
  65. * port_lock. If this lookup succeeds, we take a reference on the
  66. * ib_umad_port's struct ib_umad_device while still holding the
  67. * port_lock; if the lookup fails, we fail the open(). We drop these
  68. * references in the corresponding close().
  69. *
  70. * In addition to references coming from open character devices, there
  71. * is one more reference to each ib_umad_device representing the
  72. * module's reference taken when allocating the ib_umad_device in
  73. * ib_umad_add_one().
  74. *
  75. * When destroying an ib_umad_device, we clear all of its
  76. * ib_umad_ports from umad_port[] while holding port_lock before
  77. * dropping the module's reference to the ib_umad_device. This is
  78. * always safe because any open() calls will either succeed and obtain
  79. * a reference before we clear the umad_port[] entries, or fail after
  80. * we clear the umad_port[] entries.
  81. */
  82. struct ib_umad_port {
  83. struct cdev *cdev;
  84. struct device *dev;
  85. struct cdev *sm_cdev;
  86. struct device *sm_dev;
  87. struct semaphore sm_sem;
  88. struct mutex file_mutex;
  89. struct list_head file_list;
  90. struct ib_device *ib_dev;
  91. struct ib_umad_device *umad_dev;
  92. int dev_num;
  93. u8 port_num;
  94. };
  95. struct ib_umad_device {
  96. int start_port, end_port;
  97. struct kref ref;
  98. struct ib_umad_port port[0];
  99. };
  100. struct ib_umad_file {
  101. struct mutex mutex;
  102. struct ib_umad_port *port;
  103. struct list_head recv_list;
  104. struct list_head send_list;
  105. struct list_head port_list;
  106. spinlock_t send_lock;
  107. wait_queue_head_t recv_wait;
  108. struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];
  109. int agents_dead;
  110. u8 use_pkey_index;
  111. u8 already_used;
  112. };
  113. struct ib_umad_packet {
  114. struct ib_mad_send_buf *msg;
  115. struct ib_mad_recv_wc *recv_wc;
  116. struct list_head list;
  117. int length;
  118. struct ib_user_mad mad;
  119. };
  120. static struct class *umad_class;
  121. static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
  122. static DEFINE_SPINLOCK(port_lock);
  123. static struct ib_umad_port *umad_port[IB_UMAD_MAX_PORTS];
  124. static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS);
  125. static void ib_umad_add_one(struct ib_device *device);
  126. static void ib_umad_remove_one(struct ib_device *device);
  127. static void ib_umad_release_dev(struct kref *ref)
  128. {
  129. struct ib_umad_device *dev =
  130. container_of(ref, struct ib_umad_device, ref);
  131. kfree(dev);
  132. }
  133. static int hdr_size(struct ib_umad_file *file)
  134. {
  135. return file->use_pkey_index ? sizeof (struct ib_user_mad_hdr) :
  136. sizeof (struct ib_user_mad_hdr_old);
  137. }
  138. /* caller must hold file->mutex */
  139. static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
  140. {
  141. return file->agents_dead ? NULL : file->agent[id];
  142. }
  143. static int queue_packet(struct ib_umad_file *file,
  144. struct ib_mad_agent *agent,
  145. struct ib_umad_packet *packet)
  146. {
  147. int ret = 1;
  148. mutex_lock(&file->mutex);
  149. for (packet->mad.hdr.id = 0;
  150. packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
  151. packet->mad.hdr.id++)
  152. if (agent == __get_agent(file, packet->mad.hdr.id)) {
  153. list_add_tail(&packet->list, &file->recv_list);
  154. wake_up_interruptible(&file->recv_wait);
  155. ret = 0;
  156. break;
  157. }
  158. mutex_unlock(&file->mutex);
  159. return ret;
  160. }
  161. static void dequeue_send(struct ib_umad_file *file,
  162. struct ib_umad_packet *packet)
  163. {
  164. spin_lock_irq(&file->send_lock);
  165. list_del(&packet->list);
  166. spin_unlock_irq(&file->send_lock);
  167. }
  168. static void send_handler(struct ib_mad_agent *agent,
  169. struct ib_mad_send_wc *send_wc)
  170. {
  171. struct ib_umad_file *file = agent->context;
  172. struct ib_umad_packet *packet = send_wc->send_buf->context[0];
  173. dequeue_send(file, packet);
  174. ib_destroy_ah(packet->msg->ah);
  175. ib_free_send_mad(packet->msg);
  176. if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
  177. packet->length = IB_MGMT_MAD_HDR;
  178. packet->mad.hdr.status = ETIMEDOUT;
  179. if (!queue_packet(file, agent, packet))
  180. return;
  181. }
  182. kfree(packet);
  183. }
  184. static void recv_handler(struct ib_mad_agent *agent,
  185. struct ib_mad_recv_wc *mad_recv_wc)
  186. {
  187. struct ib_umad_file *file = agent->context;
  188. struct ib_umad_packet *packet;
  189. if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
  190. goto err1;
  191. packet = kzalloc(sizeof *packet, GFP_KERNEL);
  192. if (!packet)
  193. goto err1;
  194. packet->length = mad_recv_wc->mad_len;
  195. packet->recv_wc = mad_recv_wc;
  196. packet->mad.hdr.status = 0;
  197. packet->mad.hdr.length = hdr_size(file) + mad_recv_wc->mad_len;
  198. packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
  199. packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);
  200. packet->mad.hdr.sl = mad_recv_wc->wc->sl;
  201. packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
  202. packet->mad.hdr.pkey_index = mad_recv_wc->wc->pkey_index;
  203. packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
  204. if (packet->mad.hdr.grh_present) {
  205. struct ib_ah_attr ah_attr;
  206. ib_init_ah_from_wc(agent->device, agent->port_num,
  207. mad_recv_wc->wc, mad_recv_wc->recv_buf.grh,
  208. &ah_attr);
  209. packet->mad.hdr.gid_index = ah_attr.grh.sgid_index;
  210. packet->mad.hdr.hop_limit = ah_attr.grh.hop_limit;
  211. packet->mad.hdr.traffic_class = ah_attr.grh.traffic_class;
  212. memcpy(packet->mad.hdr.gid, &ah_attr.grh.dgid, 16);
  213. packet->mad.hdr.flow_label = cpu_to_be32(ah_attr.grh.flow_label);
  214. }
  215. if (queue_packet(file, agent, packet))
  216. goto err2;
  217. return;
  218. err2:
  219. kfree(packet);
  220. err1:
  221. ib_free_recv_mad(mad_recv_wc);
  222. }
  223. static ssize_t copy_recv_mad(struct ib_umad_file *file, char __user *buf,
  224. struct ib_umad_packet *packet, size_t count)
  225. {
  226. struct ib_mad_recv_buf *recv_buf;
  227. int left, seg_payload, offset, max_seg_payload;
  228. /* We need enough room to copy the first (or only) MAD segment. */
  229. recv_buf = &packet->recv_wc->recv_buf;
  230. if ((packet->length <= sizeof (*recv_buf->mad) &&
  231. count < hdr_size(file) + packet->length) ||
  232. (packet->length > sizeof (*recv_buf->mad) &&
  233. count < hdr_size(file) + sizeof (*recv_buf->mad)))
  234. return -EINVAL;
  235. if (copy_to_user(buf, &packet->mad, hdr_size(file)))
  236. return -EFAULT;
  237. buf += hdr_size(file);
  238. seg_payload = min_t(int, packet->length, sizeof (*recv_buf->mad));
  239. if (copy_to_user(buf, recv_buf->mad, seg_payload))
  240. return -EFAULT;
  241. if (seg_payload < packet->length) {
  242. /*
  243. * Multipacket RMPP MAD message. Copy remainder of message.
  244. * Note that last segment may have a shorter payload.
  245. */
  246. if (count < hdr_size(file) + packet->length) {
  247. /*
  248. * The buffer is too small, return the first RMPP segment,
  249. * which includes the RMPP message length.
  250. */
  251. return -ENOSPC;
  252. }
  253. offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);
  254. max_seg_payload = sizeof (struct ib_mad) - offset;
  255. for (left = packet->length - seg_payload, buf += seg_payload;
  256. left; left -= seg_payload, buf += seg_payload) {
  257. recv_buf = container_of(recv_buf->list.next,
  258. struct ib_mad_recv_buf, list);
  259. seg_payload = min(left, max_seg_payload);
  260. if (copy_to_user(buf, ((void *) recv_buf->mad) + offset,
  261. seg_payload))
  262. return -EFAULT;
  263. }
  264. }
  265. return hdr_size(file) + packet->length;
  266. }
  267. static ssize_t copy_send_mad(struct ib_umad_file *file, char __user *buf,
  268. struct ib_umad_packet *packet, size_t count)
  269. {
  270. ssize_t size = hdr_size(file) + packet->length;
  271. if (count < size)
  272. return -EINVAL;
  273. if (copy_to_user(buf, &packet->mad, hdr_size(file)))
  274. return -EFAULT;
  275. buf += hdr_size(file);
  276. if (copy_to_user(buf, packet->mad.data, packet->length))
  277. return -EFAULT;
  278. return size;
  279. }
  280. static ssize_t ib_umad_read(struct file *filp, char __user *buf,
  281. size_t count, loff_t *pos)
  282. {
  283. struct ib_umad_file *file = filp->private_data;
  284. struct ib_umad_packet *packet;
  285. ssize_t ret;
  286. if (count < hdr_size(file))
  287. return -EINVAL;
  288. mutex_lock(&file->mutex);
  289. while (list_empty(&file->recv_list)) {
  290. mutex_unlock(&file->mutex);
  291. if (filp->f_flags & O_NONBLOCK)
  292. return -EAGAIN;
  293. if (wait_event_interruptible(file->recv_wait,
  294. !list_empty(&file->recv_list)))
  295. return -ERESTARTSYS;
  296. mutex_lock(&file->mutex);
  297. }
  298. packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
  299. list_del(&packet->list);
  300. mutex_unlock(&file->mutex);
  301. if (packet->recv_wc)
  302. ret = copy_recv_mad(file, buf, packet, count);
  303. else
  304. ret = copy_send_mad(file, buf, packet, count);
  305. if (ret < 0) {
  306. /* Requeue packet */
  307. mutex_lock(&file->mutex);
  308. list_add(&packet->list, &file->recv_list);
  309. mutex_unlock(&file->mutex);
  310. } else {
  311. if (packet->recv_wc)
  312. ib_free_recv_mad(packet->recv_wc);
  313. kfree(packet);
  314. }
  315. return ret;
  316. }
  317. static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
  318. {
  319. int left, seg;
  320. /* Copy class specific header */
  321. if ((msg->hdr_len > IB_MGMT_RMPP_HDR) &&
  322. copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR,
  323. msg->hdr_len - IB_MGMT_RMPP_HDR))
  324. return -EFAULT;
  325. /* All headers are in place. Copy data segments. */
  326. for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0;
  327. seg++, left -= msg->seg_size, buf += msg->seg_size) {
  328. if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf,
  329. min(left, msg->seg_size)))
  330. return -EFAULT;
  331. }
  332. return 0;
  333. }
  334. static int same_destination(struct ib_user_mad_hdr *hdr1,
  335. struct ib_user_mad_hdr *hdr2)
  336. {
  337. if (!hdr1->grh_present && !hdr2->grh_present)
  338. return (hdr1->lid == hdr2->lid);
  339. if (hdr1->grh_present && hdr2->grh_present)
  340. return !memcmp(hdr1->gid, hdr2->gid, 16);
  341. return 0;
  342. }
  343. static int is_duplicate(struct ib_umad_file *file,
  344. struct ib_umad_packet *packet)
  345. {
  346. struct ib_umad_packet *sent_packet;
  347. struct ib_mad_hdr *sent_hdr, *hdr;
  348. hdr = (struct ib_mad_hdr *) packet->mad.data;
  349. list_for_each_entry(sent_packet, &file->send_list, list) {
  350. sent_hdr = (struct ib_mad_hdr *) sent_packet->mad.data;
  351. if ((hdr->tid != sent_hdr->tid) ||
  352. (hdr->mgmt_class != sent_hdr->mgmt_class))
  353. continue;
  354. /*
  355. * No need to be overly clever here. If two new operations have
  356. * the same TID, reject the second as a duplicate. This is more
  357. * restrictive than required by the spec.
  358. */
  359. if (!ib_response_mad((struct ib_mad *) hdr)) {
  360. if (!ib_response_mad((struct ib_mad *) sent_hdr))
  361. return 1;
  362. continue;
  363. } else if (!ib_response_mad((struct ib_mad *) sent_hdr))
  364. continue;
  365. if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))
  366. return 1;
  367. }
  368. return 0;
  369. }
  370. static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
  371. size_t count, loff_t *pos)
  372. {
  373. struct ib_umad_file *file = filp->private_data;
  374. struct ib_umad_packet *packet;
  375. struct ib_mad_agent *agent;
  376. struct ib_ah_attr ah_attr;
  377. struct ib_ah *ah;
  378. struct ib_rmpp_mad *rmpp_mad;
  379. __be64 *tid;
  380. int ret, data_len, hdr_len, copy_offset, rmpp_active;
  381. if (count < hdr_size(file) + IB_MGMT_RMPP_HDR)
  382. return -EINVAL;
  383. packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
  384. if (!packet)
  385. return -ENOMEM;
  386. if (copy_from_user(&packet->mad, buf, hdr_size(file))) {
  387. ret = -EFAULT;
  388. goto err;
  389. }
  390. if (packet->mad.hdr.id < 0 ||
  391. packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
  392. ret = -EINVAL;
  393. goto err;
  394. }
  395. buf += hdr_size(file);
  396. if (copy_from_user(packet->mad.data, buf, IB_MGMT_RMPP_HDR)) {
  397. ret = -EFAULT;
  398. goto err;
  399. }
  400. mutex_lock(&file->mutex);
  401. agent = __get_agent(file, packet->mad.hdr.id);
  402. if (!agent) {
  403. ret = -EINVAL;
  404. goto err_up;
  405. }
  406. memset(&ah_attr, 0, sizeof ah_attr);
  407. ah_attr.dlid = be16_to_cpu(packet->mad.hdr.lid);
  408. ah_attr.sl = packet->mad.hdr.sl;
  409. ah_attr.src_path_bits = packet->mad.hdr.path_bits;
  410. ah_attr.port_num = file->port->port_num;
  411. if (packet->mad.hdr.grh_present) {
  412. ah_attr.ah_flags = IB_AH_GRH;
  413. memcpy(ah_attr.grh.dgid.raw, packet->mad.hdr.gid, 16);
  414. ah_attr.grh.sgid_index = packet->mad.hdr.gid_index;
  415. ah_attr.grh.flow_label = be32_to_cpu(packet->mad.hdr.flow_label);
  416. ah_attr.grh.hop_limit = packet->mad.hdr.hop_limit;
  417. ah_attr.grh.traffic_class = packet->mad.hdr.traffic_class;
  418. }
  419. ah = ib_create_ah(agent->qp->pd, &ah_attr);
  420. if (IS_ERR(ah)) {
  421. ret = PTR_ERR(ah);
  422. goto err_up;
  423. }
  424. rmpp_mad = (struct ib_rmpp_mad *) packet->mad.data;
  425. hdr_len = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
  426. if (!ib_is_mad_class_rmpp(rmpp_mad->mad_hdr.mgmt_class)) {
  427. copy_offset = IB_MGMT_MAD_HDR;
  428. rmpp_active = 0;
  429. } else {
  430. copy_offset = IB_MGMT_RMPP_HDR;
  431. rmpp_active = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
  432. IB_MGMT_RMPP_FLAG_ACTIVE;
  433. }
  434. data_len = count - hdr_size(file) - hdr_len;
  435. packet->msg = ib_create_send_mad(agent,
  436. be32_to_cpu(packet->mad.hdr.qpn),
  437. packet->mad.hdr.pkey_index, rmpp_active,
  438. hdr_len, data_len, GFP_KERNEL);
  439. if (IS_ERR(packet->msg)) {
  440. ret = PTR_ERR(packet->msg);
  441. goto err_ah;
  442. }
  443. packet->msg->ah = ah;
  444. packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
  445. packet->msg->retries = packet->mad.hdr.retries;
  446. packet->msg->context[0] = packet;
  447. /* Copy MAD header. Any RMPP header is already in place. */
  448. memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
  449. if (!rmpp_active) {
  450. if (copy_from_user(packet->msg->mad + copy_offset,
  451. buf + copy_offset,
  452. hdr_len + data_len - copy_offset)) {
  453. ret = -EFAULT;
  454. goto err_msg;
  455. }
  456. } else {
  457. ret = copy_rmpp_mad(packet->msg, buf);
  458. if (ret)
  459. goto err_msg;
  460. }
  461. /*
  462. * Set the high-order part of the transaction ID to make MADs from
  463. * different agents unique, and allow routing responses back to the
  464. * original requestor.
  465. */
  466. if (!ib_response_mad(packet->msg->mad)) {
  467. tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
  468. *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
  469. (be64_to_cpup(tid) & 0xffffffff));
  470. rmpp_mad->mad_hdr.tid = *tid;
  471. }
  472. spin_lock_irq(&file->send_lock);
  473. ret = is_duplicate(file, packet);
  474. if (!ret)
  475. list_add_tail(&packet->list, &file->send_list);
  476. spin_unlock_irq(&file->send_lock);
  477. if (ret) {
  478. ret = -EINVAL;
  479. goto err_msg;
  480. }
  481. ret = ib_post_send_mad(packet->msg, NULL);
  482. if (ret)
  483. goto err_send;
  484. mutex_unlock(&file->mutex);
  485. return count;
  486. err_send:
  487. dequeue_send(file, packet);
  488. err_msg:
  489. ib_free_send_mad(packet->msg);
  490. err_ah:
  491. ib_destroy_ah(ah);
  492. err_up:
  493. mutex_unlock(&file->mutex);
  494. err:
  495. kfree(packet);
  496. return ret;
  497. }
  498. static unsigned int ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
  499. {
  500. struct ib_umad_file *file = filp->private_data;
  501. /* we will always be able to post a MAD send */
  502. unsigned int mask = POLLOUT | POLLWRNORM;
  503. poll_wait(filp, &file->recv_wait, wait);
  504. if (!list_empty(&file->recv_list))
  505. mask |= POLLIN | POLLRDNORM;
  506. return mask;
  507. }
  508. static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg,
  509. int compat_method_mask)
  510. {
  511. struct ib_user_mad_reg_req ureq;
  512. struct ib_mad_reg_req req;
  513. struct ib_mad_agent *agent = NULL;
  514. int agent_id;
  515. int ret;
  516. mutex_lock(&file->port->file_mutex);
  517. mutex_lock(&file->mutex);
  518. if (!file->port->ib_dev) {
  519. ret = -EPIPE;
  520. goto out;
  521. }
  522. if (copy_from_user(&ureq, arg, sizeof ureq)) {
  523. ret = -EFAULT;
  524. goto out;
  525. }
  526. if (ureq.qpn != 0 && ureq.qpn != 1) {
  527. ret = -EINVAL;
  528. goto out;
  529. }
  530. for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
  531. if (!__get_agent(file, agent_id))
  532. goto found;
  533. ret = -ENOMEM;
  534. goto out;
  535. found:
  536. if (ureq.mgmt_class) {
  537. req.mgmt_class = ureq.mgmt_class;
  538. req.mgmt_class_version = ureq.mgmt_class_version;
  539. memcpy(req.oui, ureq.oui, sizeof req.oui);
  540. if (compat_method_mask) {
  541. u32 *umm = (u32 *) ureq.method_mask;
  542. int i;
  543. for (i = 0; i < BITS_TO_LONGS(IB_MGMT_MAX_METHODS); ++i)
  544. req.method_mask[i] =
  545. umm[i * 2] | ((u64) umm[i * 2 + 1] << 32);
  546. } else
  547. memcpy(req.method_mask, ureq.method_mask,
  548. sizeof req.method_mask);
  549. }
  550. agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
  551. ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
  552. ureq.mgmt_class ? &req : NULL,
  553. ureq.rmpp_version,
  554. send_handler, recv_handler, file);
  555. if (IS_ERR(agent)) {
  556. ret = PTR_ERR(agent);
  557. agent = NULL;
  558. goto out;
  559. }
  560. if (put_user(agent_id,
  561. (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
  562. ret = -EFAULT;
  563. goto out;
  564. }
  565. if (!file->already_used) {
  566. file->already_used = 1;
  567. if (!file->use_pkey_index) {
  568. printk(KERN_WARNING "user_mad: process %s did not enable "
  569. "P_Key index support.\n", current->comm);
  570. printk(KERN_WARNING "user_mad: Documentation/infiniband/user_mad.txt "
  571. "has info on the new ABI.\n");
  572. }
  573. }
  574. file->agent[agent_id] = agent;
  575. ret = 0;
  576. out:
  577. mutex_unlock(&file->mutex);
  578. if (ret && agent)
  579. ib_unregister_mad_agent(agent);
  580. mutex_unlock(&file->port->file_mutex);
  581. return ret;
  582. }
  583. static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)
  584. {
  585. struct ib_mad_agent *agent = NULL;
  586. u32 id;
  587. int ret = 0;
  588. if (get_user(id, arg))
  589. return -EFAULT;
  590. mutex_lock(&file->port->file_mutex);
  591. mutex_lock(&file->mutex);
  592. if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !__get_agent(file, id)) {
  593. ret = -EINVAL;
  594. goto out;
  595. }
  596. agent = file->agent[id];
  597. file->agent[id] = NULL;
  598. out:
  599. mutex_unlock(&file->mutex);
  600. if (agent)
  601. ib_unregister_mad_agent(agent);
  602. mutex_unlock(&file->port->file_mutex);
  603. return ret;
  604. }
  605. static long ib_umad_enable_pkey(struct ib_umad_file *file)
  606. {
  607. int ret = 0;
  608. mutex_lock(&file->mutex);
  609. if (file->already_used)
  610. ret = -EINVAL;
  611. else
  612. file->use_pkey_index = 1;
  613. mutex_unlock(&file->mutex);
  614. return ret;
  615. }
  616. static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
  617. unsigned long arg)
  618. {
  619. switch (cmd) {
  620. case IB_USER_MAD_REGISTER_AGENT:
  621. return ib_umad_reg_agent(filp->private_data, (void __user *) arg, 0);
  622. case IB_USER_MAD_UNREGISTER_AGENT:
  623. return ib_umad_unreg_agent(filp->private_data, (__u32 __user *) arg);
  624. case IB_USER_MAD_ENABLE_PKEY:
  625. return ib_umad_enable_pkey(filp->private_data);
  626. default:
  627. return -ENOIOCTLCMD;
  628. }
  629. }
  630. #ifdef CONFIG_COMPAT
  631. static long ib_umad_compat_ioctl(struct file *filp, unsigned int cmd,
  632. unsigned long arg)
  633. {
  634. switch (cmd) {
  635. case IB_USER_MAD_REGISTER_AGENT:
  636. return ib_umad_reg_agent(filp->private_data, compat_ptr(arg), 1);
  637. case IB_USER_MAD_UNREGISTER_AGENT:
  638. return ib_umad_unreg_agent(filp->private_data, compat_ptr(arg));
  639. case IB_USER_MAD_ENABLE_PKEY:
  640. return ib_umad_enable_pkey(filp->private_data);
  641. default:
  642. return -ENOIOCTLCMD;
  643. }
  644. }
  645. #endif
  646. static int ib_umad_open(struct inode *inode, struct file *filp)
  647. {
  648. struct ib_umad_port *port;
  649. struct ib_umad_file *file;
  650. int ret = 0;
  651. spin_lock(&port_lock);
  652. port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE];
  653. if (port)
  654. kref_get(&port->umad_dev->ref);
  655. spin_unlock(&port_lock);
  656. if (!port)
  657. return -ENXIO;
  658. mutex_lock(&port->file_mutex);
  659. if (!port->ib_dev) {
  660. ret = -ENXIO;
  661. goto out;
  662. }
  663. file = kzalloc(sizeof *file, GFP_KERNEL);
  664. if (!file) {
  665. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  666. ret = -ENOMEM;
  667. goto out;
  668. }
  669. mutex_init(&file->mutex);
  670. spin_lock_init(&file->send_lock);
  671. INIT_LIST_HEAD(&file->recv_list);
  672. INIT_LIST_HEAD(&file->send_list);
  673. init_waitqueue_head(&file->recv_wait);
  674. file->port = port;
  675. filp->private_data = file;
  676. list_add_tail(&file->port_list, &port->file_list);
  677. out:
  678. mutex_unlock(&port->file_mutex);
  679. return ret;
  680. }
  681. static int ib_umad_close(struct inode *inode, struct file *filp)
  682. {
  683. struct ib_umad_file *file = filp->private_data;
  684. struct ib_umad_device *dev = file->port->umad_dev;
  685. struct ib_umad_packet *packet, *tmp;
  686. int already_dead;
  687. int i;
  688. mutex_lock(&file->port->file_mutex);
  689. mutex_lock(&file->mutex);
  690. already_dead = file->agents_dead;
  691. file->agents_dead = 1;
  692. list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {
  693. if (packet->recv_wc)
  694. ib_free_recv_mad(packet->recv_wc);
  695. kfree(packet);
  696. }
  697. list_del(&file->port_list);
  698. mutex_unlock(&file->mutex);
  699. if (!already_dead)
  700. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  701. if (file->agent[i])
  702. ib_unregister_mad_agent(file->agent[i]);
  703. mutex_unlock(&file->port->file_mutex);
  704. kfree(file);
  705. kref_put(&dev->ref, ib_umad_release_dev);
  706. return 0;
  707. }
  708. static const struct file_operations umad_fops = {
  709. .owner = THIS_MODULE,
  710. .read = ib_umad_read,
  711. .write = ib_umad_write,
  712. .poll = ib_umad_poll,
  713. .unlocked_ioctl = ib_umad_ioctl,
  714. #ifdef CONFIG_COMPAT
  715. .compat_ioctl = ib_umad_compat_ioctl,
  716. #endif
  717. .open = ib_umad_open,
  718. .release = ib_umad_close
  719. };
  720. static int ib_umad_sm_open(struct inode *inode, struct file *filp)
  721. {
  722. struct ib_umad_port *port;
  723. struct ib_port_modify props = {
  724. .set_port_cap_mask = IB_PORT_SM
  725. };
  726. int ret;
  727. spin_lock(&port_lock);
  728. port = umad_port[iminor(inode) - IB_UMAD_MINOR_BASE - IB_UMAD_MAX_PORTS];
  729. if (port)
  730. kref_get(&port->umad_dev->ref);
  731. spin_unlock(&port_lock);
  732. if (!port)
  733. return -ENXIO;
  734. if (filp->f_flags & O_NONBLOCK) {
  735. if (down_trylock(&port->sm_sem)) {
  736. ret = -EAGAIN;
  737. goto fail;
  738. }
  739. } else {
  740. if (down_interruptible(&port->sm_sem)) {
  741. ret = -ERESTARTSYS;
  742. goto fail;
  743. }
  744. }
  745. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  746. if (ret) {
  747. up(&port->sm_sem);
  748. goto fail;
  749. }
  750. filp->private_data = port;
  751. return 0;
  752. fail:
  753. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  754. return ret;
  755. }
  756. static int ib_umad_sm_close(struct inode *inode, struct file *filp)
  757. {
  758. struct ib_umad_port *port = filp->private_data;
  759. struct ib_port_modify props = {
  760. .clr_port_cap_mask = IB_PORT_SM
  761. };
  762. int ret = 0;
  763. mutex_lock(&port->file_mutex);
  764. if (port->ib_dev)
  765. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  766. mutex_unlock(&port->file_mutex);
  767. up(&port->sm_sem);
  768. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  769. return ret;
  770. }
  771. static const struct file_operations umad_sm_fops = {
  772. .owner = THIS_MODULE,
  773. .open = ib_umad_sm_open,
  774. .release = ib_umad_sm_close
  775. };
  776. static struct ib_client umad_client = {
  777. .name = "umad",
  778. .add = ib_umad_add_one,
  779. .remove = ib_umad_remove_one
  780. };
  781. static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
  782. char *buf)
  783. {
  784. struct ib_umad_port *port = dev_get_drvdata(dev);
  785. if (!port)
  786. return -ENODEV;
  787. return sprintf(buf, "%s\n", port->ib_dev->name);
  788. }
  789. static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  790. static ssize_t show_port(struct device *dev, struct device_attribute *attr,
  791. char *buf)
  792. {
  793. struct ib_umad_port *port = dev_get_drvdata(dev);
  794. if (!port)
  795. return -ENODEV;
  796. return sprintf(buf, "%d\n", port->port_num);
  797. }
  798. static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
  799. static ssize_t show_abi_version(struct class *class, char *buf)
  800. {
  801. return sprintf(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
  802. }
  803. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  804. static int ib_umad_init_port(struct ib_device *device, int port_num,
  805. struct ib_umad_port *port)
  806. {
  807. spin_lock(&port_lock);
  808. port->dev_num = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
  809. if (port->dev_num >= IB_UMAD_MAX_PORTS) {
  810. spin_unlock(&port_lock);
  811. return -1;
  812. }
  813. set_bit(port->dev_num, dev_map);
  814. spin_unlock(&port_lock);
  815. port->ib_dev = device;
  816. port->port_num = port_num;
  817. init_MUTEX(&port->sm_sem);
  818. mutex_init(&port->file_mutex);
  819. INIT_LIST_HEAD(&port->file_list);
  820. port->cdev = cdev_alloc();
  821. if (!port->cdev)
  822. return -1;
  823. port->cdev->owner = THIS_MODULE;
  824. port->cdev->ops = &umad_fops;
  825. kobject_set_name(&port->cdev->kobj, "umad%d", port->dev_num);
  826. if (cdev_add(port->cdev, base_dev + port->dev_num, 1))
  827. goto err_cdev;
  828. port->dev = device_create(umad_class, device->dma_device,
  829. port->cdev->dev, "umad%d", port->dev_num);
  830. if (IS_ERR(port->dev))
  831. goto err_cdev;
  832. if (device_create_file(port->dev, &dev_attr_ibdev))
  833. goto err_dev;
  834. if (device_create_file(port->dev, &dev_attr_port))
  835. goto err_dev;
  836. port->sm_cdev = cdev_alloc();
  837. if (!port->sm_cdev)
  838. goto err_dev;
  839. port->sm_cdev->owner = THIS_MODULE;
  840. port->sm_cdev->ops = &umad_sm_fops;
  841. kobject_set_name(&port->sm_cdev->kobj, "issm%d", port->dev_num);
  842. if (cdev_add(port->sm_cdev, base_dev + port->dev_num + IB_UMAD_MAX_PORTS, 1))
  843. goto err_sm_cdev;
  844. port->sm_dev = device_create(umad_class, device->dma_device,
  845. port->sm_cdev->dev,
  846. "issm%d", port->dev_num);
  847. if (IS_ERR(port->sm_dev))
  848. goto err_sm_cdev;
  849. dev_set_drvdata(port->dev, port);
  850. dev_set_drvdata(port->sm_dev, port);
  851. if (device_create_file(port->sm_dev, &dev_attr_ibdev))
  852. goto err_sm_dev;
  853. if (device_create_file(port->sm_dev, &dev_attr_port))
  854. goto err_sm_dev;
  855. spin_lock(&port_lock);
  856. umad_port[port->dev_num] = port;
  857. spin_unlock(&port_lock);
  858. return 0;
  859. err_sm_dev:
  860. device_destroy(umad_class, port->sm_cdev->dev);
  861. err_sm_cdev:
  862. cdev_del(port->sm_cdev);
  863. err_dev:
  864. device_destroy(umad_class, port->cdev->dev);
  865. err_cdev:
  866. cdev_del(port->cdev);
  867. clear_bit(port->dev_num, dev_map);
  868. return -1;
  869. }
  870. static void ib_umad_kill_port(struct ib_umad_port *port)
  871. {
  872. struct ib_umad_file *file;
  873. int already_dead;
  874. int id;
  875. dev_set_drvdata(port->dev, NULL);
  876. dev_set_drvdata(port->sm_dev, NULL);
  877. device_destroy(umad_class, port->cdev->dev);
  878. device_destroy(umad_class, port->sm_cdev->dev);
  879. cdev_del(port->cdev);
  880. cdev_del(port->sm_cdev);
  881. spin_lock(&port_lock);
  882. umad_port[port->dev_num] = NULL;
  883. spin_unlock(&port_lock);
  884. mutex_lock(&port->file_mutex);
  885. port->ib_dev = NULL;
  886. list_for_each_entry(file, &port->file_list, port_list) {
  887. mutex_lock(&file->mutex);
  888. already_dead = file->agents_dead;
  889. file->agents_dead = 1;
  890. mutex_unlock(&file->mutex);
  891. for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
  892. if (file->agent[id])
  893. ib_unregister_mad_agent(file->agent[id]);
  894. }
  895. mutex_unlock(&port->file_mutex);
  896. clear_bit(port->dev_num, dev_map);
  897. }
  898. static void ib_umad_add_one(struct ib_device *device)
  899. {
  900. struct ib_umad_device *umad_dev;
  901. int s, e, i;
  902. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  903. return;
  904. if (device->node_type == RDMA_NODE_IB_SWITCH)
  905. s = e = 0;
  906. else {
  907. s = 1;
  908. e = device->phys_port_cnt;
  909. }
  910. umad_dev = kzalloc(sizeof *umad_dev +
  911. (e - s + 1) * sizeof (struct ib_umad_port),
  912. GFP_KERNEL);
  913. if (!umad_dev)
  914. return;
  915. kref_init(&umad_dev->ref);
  916. umad_dev->start_port = s;
  917. umad_dev->end_port = e;
  918. for (i = s; i <= e; ++i) {
  919. umad_dev->port[i - s].umad_dev = umad_dev;
  920. if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
  921. goto err;
  922. }
  923. ib_set_client_data(device, &umad_client, umad_dev);
  924. return;
  925. err:
  926. while (--i >= s)
  927. ib_umad_kill_port(&umad_dev->port[i - s]);
  928. kref_put(&umad_dev->ref, ib_umad_release_dev);
  929. }
  930. static void ib_umad_remove_one(struct ib_device *device)
  931. {
  932. struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
  933. int i;
  934. if (!umad_dev)
  935. return;
  936. for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
  937. ib_umad_kill_port(&umad_dev->port[i]);
  938. kref_put(&umad_dev->ref, ib_umad_release_dev);
  939. }
  940. static int __init ib_umad_init(void)
  941. {
  942. int ret;
  943. ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
  944. "infiniband_mad");
  945. if (ret) {
  946. printk(KERN_ERR "user_mad: couldn't register device number\n");
  947. goto out;
  948. }
  949. umad_class = class_create(THIS_MODULE, "infiniband_mad");
  950. if (IS_ERR(umad_class)) {
  951. ret = PTR_ERR(umad_class);
  952. printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
  953. goto out_chrdev;
  954. }
  955. ret = class_create_file(umad_class, &class_attr_abi_version);
  956. if (ret) {
  957. printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
  958. goto out_class;
  959. }
  960. ret = ib_register_client(&umad_client);
  961. if (ret) {
  962. printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
  963. goto out_class;
  964. }
  965. return 0;
  966. out_class:
  967. class_destroy(umad_class);
  968. out_chrdev:
  969. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  970. out:
  971. return ret;
  972. }
  973. static void __exit ib_umad_cleanup(void)
  974. {
  975. ib_unregister_client(&umad_client);
  976. class_destroy(umad_class);
  977. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  978. }
  979. module_init(ib_umad_init);
  980. module_exit(ib_umad_cleanup);