user_mad.c 29 KB

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