user_mad.c 30 KB

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