user_mad.c 30 KB

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