user_mad.c 30 KB

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