user_mad.c 30 KB

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