user_mad.c 30 KB

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