user_mad.c 29 KB

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