user_mad.c 30 KB

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