user_mad.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  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 <linux/slab.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:
  63. * device special file is opened, we take a reference on the
  64. * ib_umad_port's struct ib_umad_device. We drop these
  65. * references in the corresponding close().
  66. *
  67. * In addition to references coming from open character devices, there
  68. * is one more reference to each ib_umad_device representing the
  69. * module's reference taken when allocating the ib_umad_device in
  70. * ib_umad_add_one().
  71. *
  72. * When destroying an ib_umad_device, we drop the module's reference.
  73. */
  74. struct ib_umad_port {
  75. struct cdev cdev;
  76. struct device *dev;
  77. struct cdev sm_cdev;
  78. struct device *sm_dev;
  79. struct semaphore sm_sem;
  80. struct mutex file_mutex;
  81. struct list_head file_list;
  82. struct ib_device *ib_dev;
  83. struct ib_umad_device *umad_dev;
  84. int dev_num;
  85. u8 port_num;
  86. };
  87. struct ib_umad_device {
  88. int start_port, end_port;
  89. struct kref ref;
  90. struct ib_umad_port port[0];
  91. };
  92. struct ib_umad_file {
  93. struct mutex mutex;
  94. struct ib_umad_port *port;
  95. struct list_head recv_list;
  96. struct list_head send_list;
  97. struct list_head port_list;
  98. spinlock_t send_lock;
  99. wait_queue_head_t recv_wait;
  100. struct ib_mad_agent *agent[IB_UMAD_MAX_AGENTS];
  101. int agents_dead;
  102. u8 use_pkey_index;
  103. u8 already_used;
  104. };
  105. struct ib_umad_packet {
  106. struct ib_mad_send_buf *msg;
  107. struct ib_mad_recv_wc *recv_wc;
  108. struct list_head list;
  109. int length;
  110. struct ib_user_mad mad;
  111. };
  112. static struct class *umad_class;
  113. static const dev_t base_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
  114. static DEFINE_SPINLOCK(port_lock);
  115. static DECLARE_BITMAP(dev_map, IB_UMAD_MAX_PORTS);
  116. static void ib_umad_add_one(struct ib_device *device);
  117. static void ib_umad_remove_one(struct ib_device *device);
  118. static void ib_umad_release_dev(struct kref *ref)
  119. {
  120. struct ib_umad_device *dev =
  121. container_of(ref, struct ib_umad_device, ref);
  122. kfree(dev);
  123. }
  124. static int hdr_size(struct ib_umad_file *file)
  125. {
  126. return file->use_pkey_index ? sizeof (struct ib_user_mad_hdr) :
  127. sizeof (struct ib_user_mad_hdr_old);
  128. }
  129. /* caller must hold file->mutex */
  130. static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
  131. {
  132. return file->agents_dead ? NULL : file->agent[id];
  133. }
  134. static int queue_packet(struct ib_umad_file *file,
  135. struct ib_mad_agent *agent,
  136. struct ib_umad_packet *packet)
  137. {
  138. int ret = 1;
  139. mutex_lock(&file->mutex);
  140. for (packet->mad.hdr.id = 0;
  141. packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
  142. packet->mad.hdr.id++)
  143. if (agent == __get_agent(file, packet->mad.hdr.id)) {
  144. list_add_tail(&packet->list, &file->recv_list);
  145. wake_up_interruptible(&file->recv_wait);
  146. ret = 0;
  147. break;
  148. }
  149. mutex_unlock(&file->mutex);
  150. return ret;
  151. }
  152. static void dequeue_send(struct ib_umad_file *file,
  153. struct ib_umad_packet *packet)
  154. {
  155. spin_lock_irq(&file->send_lock);
  156. list_del(&packet->list);
  157. spin_unlock_irq(&file->send_lock);
  158. }
  159. static void send_handler(struct ib_mad_agent *agent,
  160. struct ib_mad_send_wc *send_wc)
  161. {
  162. struct ib_umad_file *file = agent->context;
  163. struct ib_umad_packet *packet = send_wc->send_buf->context[0];
  164. dequeue_send(file, packet);
  165. ib_destroy_ah(packet->msg->ah);
  166. ib_free_send_mad(packet->msg);
  167. if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
  168. packet->length = IB_MGMT_MAD_HDR;
  169. packet->mad.hdr.status = ETIMEDOUT;
  170. if (!queue_packet(file, agent, packet))
  171. return;
  172. }
  173. kfree(packet);
  174. }
  175. static void recv_handler(struct ib_mad_agent *agent,
  176. struct ib_mad_recv_wc *mad_recv_wc)
  177. {
  178. struct ib_umad_file *file = agent->context;
  179. struct ib_umad_packet *packet;
  180. if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
  181. goto err1;
  182. packet = kzalloc(sizeof *packet, GFP_KERNEL);
  183. if (!packet)
  184. goto err1;
  185. packet->length = mad_recv_wc->mad_len;
  186. packet->recv_wc = mad_recv_wc;
  187. packet->mad.hdr.status = 0;
  188. packet->mad.hdr.length = hdr_size(file) + mad_recv_wc->mad_len;
  189. packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
  190. packet->mad.hdr.lid = cpu_to_be16(mad_recv_wc->wc->slid);
  191. packet->mad.hdr.sl = mad_recv_wc->wc->sl;
  192. packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
  193. packet->mad.hdr.pkey_index = mad_recv_wc->wc->pkey_index;
  194. packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
  195. if (packet->mad.hdr.grh_present) {
  196. struct ib_ah_attr ah_attr;
  197. ib_init_ah_from_wc(agent->device, agent->port_num,
  198. mad_recv_wc->wc, mad_recv_wc->recv_buf.grh,
  199. &ah_attr);
  200. packet->mad.hdr.gid_index = ah_attr.grh.sgid_index;
  201. packet->mad.hdr.hop_limit = ah_attr.grh.hop_limit;
  202. packet->mad.hdr.traffic_class = ah_attr.grh.traffic_class;
  203. memcpy(packet->mad.hdr.gid, &ah_attr.grh.dgid, 16);
  204. packet->mad.hdr.flow_label = cpu_to_be32(ah_attr.grh.flow_label);
  205. }
  206. if (queue_packet(file, agent, packet))
  207. goto err2;
  208. return;
  209. err2:
  210. kfree(packet);
  211. err1:
  212. ib_free_recv_mad(mad_recv_wc);
  213. }
  214. static ssize_t copy_recv_mad(struct ib_umad_file *file, char __user *buf,
  215. struct ib_umad_packet *packet, size_t count)
  216. {
  217. struct ib_mad_recv_buf *recv_buf;
  218. int left, seg_payload, offset, max_seg_payload;
  219. /* We need enough room to copy the first (or only) MAD segment. */
  220. recv_buf = &packet->recv_wc->recv_buf;
  221. if ((packet->length <= sizeof (*recv_buf->mad) &&
  222. count < hdr_size(file) + packet->length) ||
  223. (packet->length > sizeof (*recv_buf->mad) &&
  224. count < hdr_size(file) + sizeof (*recv_buf->mad)))
  225. return -EINVAL;
  226. if (copy_to_user(buf, &packet->mad, hdr_size(file)))
  227. return -EFAULT;
  228. buf += hdr_size(file);
  229. seg_payload = min_t(int, packet->length, sizeof (*recv_buf->mad));
  230. if (copy_to_user(buf, recv_buf->mad, seg_payload))
  231. return -EFAULT;
  232. if (seg_payload < packet->length) {
  233. /*
  234. * Multipacket RMPP MAD message. Copy remainder of message.
  235. * Note that last segment may have a shorter payload.
  236. */
  237. if (count < hdr_size(file) + packet->length) {
  238. /*
  239. * The buffer is too small, return the first RMPP segment,
  240. * which includes the RMPP message length.
  241. */
  242. return -ENOSPC;
  243. }
  244. offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);
  245. max_seg_payload = sizeof (struct ib_mad) - offset;
  246. for (left = packet->length - seg_payload, buf += seg_payload;
  247. left; left -= seg_payload, buf += seg_payload) {
  248. recv_buf = container_of(recv_buf->list.next,
  249. struct ib_mad_recv_buf, list);
  250. seg_payload = min(left, max_seg_payload);
  251. if (copy_to_user(buf, ((void *) recv_buf->mad) + offset,
  252. seg_payload))
  253. return -EFAULT;
  254. }
  255. }
  256. return hdr_size(file) + packet->length;
  257. }
  258. static ssize_t copy_send_mad(struct ib_umad_file *file, char __user *buf,
  259. struct ib_umad_packet *packet, size_t count)
  260. {
  261. ssize_t size = hdr_size(file) + packet->length;
  262. if (count < size)
  263. return -EINVAL;
  264. if (copy_to_user(buf, &packet->mad, hdr_size(file)))
  265. return -EFAULT;
  266. buf += hdr_size(file);
  267. if (copy_to_user(buf, packet->mad.data, packet->length))
  268. return -EFAULT;
  269. return size;
  270. }
  271. static ssize_t ib_umad_read(struct file *filp, char __user *buf,
  272. size_t count, loff_t *pos)
  273. {
  274. struct ib_umad_file *file = filp->private_data;
  275. struct ib_umad_packet *packet;
  276. ssize_t ret;
  277. if (count < hdr_size(file))
  278. return -EINVAL;
  279. mutex_lock(&file->mutex);
  280. while (list_empty(&file->recv_list)) {
  281. mutex_unlock(&file->mutex);
  282. if (filp->f_flags & O_NONBLOCK)
  283. return -EAGAIN;
  284. if (wait_event_interruptible(file->recv_wait,
  285. !list_empty(&file->recv_list)))
  286. return -ERESTARTSYS;
  287. mutex_lock(&file->mutex);
  288. }
  289. packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
  290. list_del(&packet->list);
  291. mutex_unlock(&file->mutex);
  292. if (packet->recv_wc)
  293. ret = copy_recv_mad(file, buf, packet, count);
  294. else
  295. ret = copy_send_mad(file, buf, packet, count);
  296. if (ret < 0) {
  297. /* Requeue packet */
  298. mutex_lock(&file->mutex);
  299. list_add(&packet->list, &file->recv_list);
  300. mutex_unlock(&file->mutex);
  301. } else {
  302. if (packet->recv_wc)
  303. ib_free_recv_mad(packet->recv_wc);
  304. kfree(packet);
  305. }
  306. return ret;
  307. }
  308. static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
  309. {
  310. int left, seg;
  311. /* Copy class specific header */
  312. if ((msg->hdr_len > IB_MGMT_RMPP_HDR) &&
  313. copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR,
  314. msg->hdr_len - IB_MGMT_RMPP_HDR))
  315. return -EFAULT;
  316. /* All headers are in place. Copy data segments. */
  317. for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0;
  318. seg++, left -= msg->seg_size, buf += msg->seg_size) {
  319. if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf,
  320. min(left, msg->seg_size)))
  321. return -EFAULT;
  322. }
  323. return 0;
  324. }
  325. static int same_destination(struct ib_user_mad_hdr *hdr1,
  326. struct ib_user_mad_hdr *hdr2)
  327. {
  328. if (!hdr1->grh_present && !hdr2->grh_present)
  329. return (hdr1->lid == hdr2->lid);
  330. if (hdr1->grh_present && hdr2->grh_present)
  331. return !memcmp(hdr1->gid, hdr2->gid, 16);
  332. return 0;
  333. }
  334. static int is_duplicate(struct ib_umad_file *file,
  335. struct ib_umad_packet *packet)
  336. {
  337. struct ib_umad_packet *sent_packet;
  338. struct ib_mad_hdr *sent_hdr, *hdr;
  339. hdr = (struct ib_mad_hdr *) packet->mad.data;
  340. list_for_each_entry(sent_packet, &file->send_list, list) {
  341. sent_hdr = (struct ib_mad_hdr *) sent_packet->mad.data;
  342. if ((hdr->tid != sent_hdr->tid) ||
  343. (hdr->mgmt_class != sent_hdr->mgmt_class))
  344. continue;
  345. /*
  346. * No need to be overly clever here. If two new operations have
  347. * the same TID, reject the second as a duplicate. This is more
  348. * restrictive than required by the spec.
  349. */
  350. if (!ib_response_mad((struct ib_mad *) hdr)) {
  351. if (!ib_response_mad((struct ib_mad *) sent_hdr))
  352. return 1;
  353. continue;
  354. } else if (!ib_response_mad((struct ib_mad *) sent_hdr))
  355. continue;
  356. if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))
  357. return 1;
  358. }
  359. return 0;
  360. }
  361. static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
  362. size_t count, loff_t *pos)
  363. {
  364. struct ib_umad_file *file = filp->private_data;
  365. struct ib_umad_packet *packet;
  366. struct ib_mad_agent *agent;
  367. struct ib_ah_attr ah_attr;
  368. struct ib_ah *ah;
  369. struct ib_rmpp_mad *rmpp_mad;
  370. __be64 *tid;
  371. int ret, data_len, hdr_len, copy_offset, rmpp_active;
  372. if (count < hdr_size(file) + IB_MGMT_RMPP_HDR)
  373. return -EINVAL;
  374. packet = kzalloc(sizeof *packet + IB_MGMT_RMPP_HDR, GFP_KERNEL);
  375. if (!packet)
  376. return -ENOMEM;
  377. if (copy_from_user(&packet->mad, buf, hdr_size(file))) {
  378. ret = -EFAULT;
  379. goto err;
  380. }
  381. if (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 >= 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;
  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. ret = nonseekable_open(inode, filp);
  675. out:
  676. mutex_unlock(&port->file_mutex);
  677. return ret;
  678. }
  679. static int ib_umad_close(struct inode *inode, struct file *filp)
  680. {
  681. struct ib_umad_file *file = filp->private_data;
  682. struct ib_umad_device *dev = file->port->umad_dev;
  683. struct ib_umad_packet *packet, *tmp;
  684. int already_dead;
  685. int i;
  686. mutex_lock(&file->port->file_mutex);
  687. mutex_lock(&file->mutex);
  688. already_dead = file->agents_dead;
  689. file->agents_dead = 1;
  690. list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {
  691. if (packet->recv_wc)
  692. ib_free_recv_mad(packet->recv_wc);
  693. kfree(packet);
  694. }
  695. list_del(&file->port_list);
  696. mutex_unlock(&file->mutex);
  697. if (!already_dead)
  698. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  699. if (file->agent[i])
  700. ib_unregister_mad_agent(file->agent[i]);
  701. mutex_unlock(&file->port->file_mutex);
  702. kfree(file);
  703. kref_put(&dev->ref, ib_umad_release_dev);
  704. return 0;
  705. }
  706. static const struct file_operations umad_fops = {
  707. .owner = THIS_MODULE,
  708. .read = ib_umad_read,
  709. .write = ib_umad_write,
  710. .poll = ib_umad_poll,
  711. .unlocked_ioctl = ib_umad_ioctl,
  712. #ifdef CONFIG_COMPAT
  713. .compat_ioctl = ib_umad_compat_ioctl,
  714. #endif
  715. .open = ib_umad_open,
  716. .release = ib_umad_close,
  717. .llseek = no_llseek,
  718. };
  719. static int ib_umad_sm_open(struct inode *inode, struct file *filp)
  720. {
  721. struct ib_umad_port *port;
  722. struct ib_port_modify props = {
  723. .set_port_cap_mask = IB_PORT_SM
  724. };
  725. int ret;
  726. port = container_of(inode->i_cdev, struct ib_umad_port, sm_cdev);
  727. if (port)
  728. kref_get(&port->umad_dev->ref);
  729. else
  730. return -ENXIO;
  731. if (filp->f_flags & O_NONBLOCK) {
  732. if (down_trylock(&port->sm_sem)) {
  733. ret = -EAGAIN;
  734. goto fail;
  735. }
  736. } else {
  737. if (down_interruptible(&port->sm_sem)) {
  738. ret = -ERESTARTSYS;
  739. goto fail;
  740. }
  741. }
  742. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  743. if (ret) {
  744. up(&port->sm_sem);
  745. goto fail;
  746. }
  747. filp->private_data = port;
  748. return nonseekable_open(inode, filp);
  749. fail:
  750. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  751. return ret;
  752. }
  753. static int ib_umad_sm_close(struct inode *inode, struct file *filp)
  754. {
  755. struct ib_umad_port *port = filp->private_data;
  756. struct ib_port_modify props = {
  757. .clr_port_cap_mask = IB_PORT_SM
  758. };
  759. int ret = 0;
  760. mutex_lock(&port->file_mutex);
  761. if (port->ib_dev)
  762. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  763. mutex_unlock(&port->file_mutex);
  764. up(&port->sm_sem);
  765. kref_put(&port->umad_dev->ref, ib_umad_release_dev);
  766. return ret;
  767. }
  768. static const struct file_operations umad_sm_fops = {
  769. .owner = THIS_MODULE,
  770. .open = ib_umad_sm_open,
  771. .release = ib_umad_sm_close,
  772. .llseek = no_llseek,
  773. };
  774. static struct ib_client umad_client = {
  775. .name = "umad",
  776. .add = ib_umad_add_one,
  777. .remove = ib_umad_remove_one
  778. };
  779. static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
  780. char *buf)
  781. {
  782. struct ib_umad_port *port = dev_get_drvdata(dev);
  783. if (!port)
  784. return -ENODEV;
  785. return sprintf(buf, "%s\n", port->ib_dev->name);
  786. }
  787. static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  788. static ssize_t show_port(struct device *dev, struct device_attribute *attr,
  789. char *buf)
  790. {
  791. struct ib_umad_port *port = dev_get_drvdata(dev);
  792. if (!port)
  793. return -ENODEV;
  794. return sprintf(buf, "%d\n", port->port_num);
  795. }
  796. static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
  797. static CLASS_ATTR_STRING(abi_version, S_IRUGO,
  798. __stringify(IB_USER_MAD_ABI_VERSION));
  799. static dev_t overflow_maj;
  800. static DECLARE_BITMAP(overflow_map, IB_UMAD_MAX_PORTS);
  801. static int find_overflow_devnum(void)
  802. {
  803. int ret;
  804. if (!overflow_maj) {
  805. ret = alloc_chrdev_region(&overflow_maj, 0, IB_UMAD_MAX_PORTS * 2,
  806. "infiniband_mad");
  807. if (ret) {
  808. printk(KERN_ERR "user_mad: couldn't register dynamic device number\n");
  809. return ret;
  810. }
  811. }
  812. ret = find_first_zero_bit(overflow_map, IB_UMAD_MAX_PORTS);
  813. if (ret >= IB_UMAD_MAX_PORTS)
  814. return -1;
  815. return ret;
  816. }
  817. static int ib_umad_init_port(struct ib_device *device, int port_num,
  818. struct ib_umad_port *port)
  819. {
  820. int devnum;
  821. dev_t base;
  822. spin_lock(&port_lock);
  823. devnum = find_first_zero_bit(dev_map, IB_UMAD_MAX_PORTS);
  824. if (devnum >= IB_UMAD_MAX_PORTS) {
  825. spin_unlock(&port_lock);
  826. devnum = find_overflow_devnum();
  827. if (devnum < 0)
  828. return -1;
  829. spin_lock(&port_lock);
  830. port->dev_num = devnum + IB_UMAD_MAX_PORTS;
  831. base = devnum + overflow_maj;
  832. set_bit(devnum, overflow_map);
  833. } else {
  834. port->dev_num = devnum;
  835. base = devnum + base_dev;
  836. set_bit(devnum, dev_map);
  837. }
  838. spin_unlock(&port_lock);
  839. port->ib_dev = device;
  840. port->port_num = port_num;
  841. sema_init(&port->sm_sem, 1);
  842. mutex_init(&port->file_mutex);
  843. INIT_LIST_HEAD(&port->file_list);
  844. cdev_init(&port->cdev, &umad_fops);
  845. port->cdev.owner = THIS_MODULE;
  846. kobject_set_name(&port->cdev.kobj, "umad%d", port->dev_num);
  847. if (cdev_add(&port->cdev, base, 1))
  848. goto err_cdev;
  849. port->dev = device_create(umad_class, device->dma_device,
  850. port->cdev.dev, port,
  851. "umad%d", port->dev_num);
  852. if (IS_ERR(port->dev))
  853. goto err_cdev;
  854. if (device_create_file(port->dev, &dev_attr_ibdev))
  855. goto err_dev;
  856. if (device_create_file(port->dev, &dev_attr_port))
  857. goto err_dev;
  858. base += IB_UMAD_MAX_PORTS;
  859. cdev_init(&port->sm_cdev, &umad_sm_fops);
  860. port->sm_cdev.owner = THIS_MODULE;
  861. kobject_set_name(&port->sm_cdev.kobj, "issm%d", port->dev_num);
  862. if (cdev_add(&port->sm_cdev, base, 1))
  863. goto err_sm_cdev;
  864. port->sm_dev = device_create(umad_class, device->dma_device,
  865. port->sm_cdev.dev, port,
  866. "issm%d", port->dev_num);
  867. if (IS_ERR(port->sm_dev))
  868. goto err_sm_cdev;
  869. if (device_create_file(port->sm_dev, &dev_attr_ibdev))
  870. goto err_sm_dev;
  871. if (device_create_file(port->sm_dev, &dev_attr_port))
  872. goto err_sm_dev;
  873. return 0;
  874. err_sm_dev:
  875. device_destroy(umad_class, port->sm_cdev.dev);
  876. err_sm_cdev:
  877. cdev_del(&port->sm_cdev);
  878. err_dev:
  879. device_destroy(umad_class, port->cdev.dev);
  880. err_cdev:
  881. cdev_del(&port->cdev);
  882. if (port->dev_num < IB_UMAD_MAX_PORTS)
  883. clear_bit(devnum, dev_map);
  884. else
  885. clear_bit(devnum, overflow_map);
  886. return -1;
  887. }
  888. static void ib_umad_kill_port(struct ib_umad_port *port)
  889. {
  890. struct ib_umad_file *file;
  891. int id;
  892. dev_set_drvdata(port->dev, NULL);
  893. dev_set_drvdata(port->sm_dev, NULL);
  894. device_destroy(umad_class, port->cdev.dev);
  895. device_destroy(umad_class, port->sm_cdev.dev);
  896. cdev_del(&port->cdev);
  897. cdev_del(&port->sm_cdev);
  898. mutex_lock(&port->file_mutex);
  899. port->ib_dev = NULL;
  900. list_for_each_entry(file, &port->file_list, port_list) {
  901. mutex_lock(&file->mutex);
  902. file->agents_dead = 1;
  903. mutex_unlock(&file->mutex);
  904. for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
  905. if (file->agent[id])
  906. ib_unregister_mad_agent(file->agent[id]);
  907. }
  908. mutex_unlock(&port->file_mutex);
  909. if (port->dev_num < IB_UMAD_MAX_PORTS)
  910. clear_bit(port->dev_num, dev_map);
  911. else
  912. clear_bit(port->dev_num - IB_UMAD_MAX_PORTS, overflow_map);
  913. }
  914. static void ib_umad_add_one(struct ib_device *device)
  915. {
  916. struct ib_umad_device *umad_dev;
  917. int s, e, i;
  918. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  919. return;
  920. if (device->node_type == RDMA_NODE_IB_SWITCH)
  921. s = e = 0;
  922. else {
  923. s = 1;
  924. e = device->phys_port_cnt;
  925. }
  926. umad_dev = kzalloc(sizeof *umad_dev +
  927. (e - s + 1) * sizeof (struct ib_umad_port),
  928. GFP_KERNEL);
  929. if (!umad_dev)
  930. return;
  931. kref_init(&umad_dev->ref);
  932. umad_dev->start_port = s;
  933. umad_dev->end_port = e;
  934. for (i = s; i <= e; ++i) {
  935. umad_dev->port[i - s].umad_dev = umad_dev;
  936. if (ib_umad_init_port(device, i, &umad_dev->port[i - s]))
  937. goto err;
  938. }
  939. ib_set_client_data(device, &umad_client, umad_dev);
  940. return;
  941. err:
  942. while (--i >= s)
  943. ib_umad_kill_port(&umad_dev->port[i - s]);
  944. kref_put(&umad_dev->ref, ib_umad_release_dev);
  945. }
  946. static void ib_umad_remove_one(struct ib_device *device)
  947. {
  948. struct ib_umad_device *umad_dev = ib_get_client_data(device, &umad_client);
  949. int i;
  950. if (!umad_dev)
  951. return;
  952. for (i = 0; i <= umad_dev->end_port - umad_dev->start_port; ++i)
  953. ib_umad_kill_port(&umad_dev->port[i]);
  954. kref_put(&umad_dev->ref, ib_umad_release_dev);
  955. }
  956. static char *umad_devnode(struct device *dev, mode_t *mode)
  957. {
  958. return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
  959. }
  960. static int __init ib_umad_init(void)
  961. {
  962. int ret;
  963. ret = register_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2,
  964. "infiniband_mad");
  965. if (ret) {
  966. printk(KERN_ERR "user_mad: couldn't register device number\n");
  967. goto out;
  968. }
  969. umad_class = class_create(THIS_MODULE, "infiniband_mad");
  970. if (IS_ERR(umad_class)) {
  971. ret = PTR_ERR(umad_class);
  972. printk(KERN_ERR "user_mad: couldn't create class infiniband_mad\n");
  973. goto out_chrdev;
  974. }
  975. umad_class->devnode = umad_devnode;
  976. ret = class_create_file(umad_class, &class_attr_abi_version.attr);
  977. if (ret) {
  978. printk(KERN_ERR "user_mad: couldn't create abi_version attribute\n");
  979. goto out_class;
  980. }
  981. ret = ib_register_client(&umad_client);
  982. if (ret) {
  983. printk(KERN_ERR "user_mad: couldn't register ib_umad client\n");
  984. goto out_class;
  985. }
  986. return 0;
  987. out_class:
  988. class_destroy(umad_class);
  989. out_chrdev:
  990. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  991. out:
  992. return ret;
  993. }
  994. static void __exit ib_umad_cleanup(void)
  995. {
  996. ib_unregister_client(&umad_client);
  997. class_destroy(umad_class);
  998. unregister_chrdev_region(base_dev, IB_UMAD_MAX_PORTS * 2);
  999. if (overflow_maj)
  1000. unregister_chrdev_region(overflow_maj, IB_UMAD_MAX_PORTS * 2);
  1001. }
  1002. module_init(ib_umad_init);
  1003. module_exit(ib_umad_cleanup);