user_mad.c 30 KB

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