user_mad.c 30 KB

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