user_mad.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  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. #define pr_fmt(fmt) "user_mad: " fmt
  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/mutex.h>
  45. #include <linux/kref.h>
  46. #include <linux/compat.h>
  47. #include <linux/sched.h>
  48. #include <linux/semaphore.h>
  49. #include <linux/slab.h>
  50. #include <linux/nospec.h>
  51. #include <linux/uaccess.h>
  52. #include <rdma/ib_mad.h>
  53. #include <rdma/ib_user_mad.h>
  54. #include <rdma/rdma_netlink.h>
  55. #include "core_priv.h"
  56. MODULE_AUTHOR("Roland Dreier");
  57. MODULE_DESCRIPTION("InfiniBand userspace MAD packet access");
  58. MODULE_LICENSE("Dual BSD/GPL");
  59. #define MAX_UMAD_RECV_LIST_SIZE 200000
  60. enum {
  61. IB_UMAD_MAX_PORTS = RDMA_MAX_PORTS,
  62. IB_UMAD_MAX_AGENTS = 32,
  63. IB_UMAD_MAJOR = 231,
  64. IB_UMAD_MINOR_BASE = 0,
  65. IB_UMAD_NUM_FIXED_MINOR = 64,
  66. IB_UMAD_NUM_DYNAMIC_MINOR = IB_UMAD_MAX_PORTS - IB_UMAD_NUM_FIXED_MINOR,
  67. IB_ISSM_MINOR_BASE = IB_UMAD_NUM_FIXED_MINOR,
  68. };
  69. /*
  70. * Our lifetime rules for these structs are the following:
  71. * device special file is opened, we take a reference on the
  72. * ib_umad_port's struct ib_umad_device. We drop these
  73. * references in the corresponding close().
  74. *
  75. * In addition to references coming from open character devices, there
  76. * is one more reference to each ib_umad_device representing the
  77. * module's reference taken when allocating the ib_umad_device in
  78. * ib_umad_add_one().
  79. *
  80. * When destroying an ib_umad_device, we drop the module's reference.
  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. u32 port_num;
  94. };
  95. struct ib_umad_device {
  96. struct kref kref;
  97. struct ib_umad_port ports[];
  98. };
  99. struct ib_umad_file {
  100. struct mutex mutex;
  101. struct ib_umad_port *port;
  102. struct list_head recv_list;
  103. atomic_t recv_list_size;
  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. struct ib_rmpp_mad_hdr {
  121. struct ib_mad_hdr mad_hdr;
  122. struct ib_rmpp_hdr rmpp_hdr;
  123. } __packed;
  124. #define CREATE_TRACE_POINTS
  125. #include <trace/events/ib_umad.h>
  126. static const dev_t base_umad_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE);
  127. static const dev_t base_issm_dev = MKDEV(IB_UMAD_MAJOR, IB_UMAD_MINOR_BASE) +
  128. IB_UMAD_NUM_FIXED_MINOR;
  129. static dev_t dynamic_umad_dev;
  130. static dev_t dynamic_issm_dev;
  131. static DEFINE_IDA(umad_ida);
  132. static int ib_umad_add_one(struct ib_device *device);
  133. static void ib_umad_remove_one(struct ib_device *device, void *client_data);
  134. static void ib_umad_dev_free(struct kref *kref)
  135. {
  136. struct ib_umad_device *dev =
  137. container_of(kref, struct ib_umad_device, kref);
  138. kfree(dev);
  139. }
  140. static void ib_umad_dev_get(struct ib_umad_device *dev)
  141. {
  142. kref_get(&dev->kref);
  143. }
  144. static void ib_umad_dev_put(struct ib_umad_device *dev)
  145. {
  146. kref_put(&dev->kref, ib_umad_dev_free);
  147. }
  148. static int hdr_size(struct ib_umad_file *file)
  149. {
  150. return file->use_pkey_index ? sizeof(struct ib_user_mad_hdr) :
  151. sizeof(struct ib_user_mad_hdr_old);
  152. }
  153. /* caller must hold file->mutex */
  154. static struct ib_mad_agent *__get_agent(struct ib_umad_file *file, int id)
  155. {
  156. return file->agents_dead ? NULL : file->agent[id];
  157. }
  158. static int queue_packet(struct ib_umad_file *file, struct ib_mad_agent *agent,
  159. struct ib_umad_packet *packet, bool is_recv_mad)
  160. {
  161. int ret = 1;
  162. mutex_lock(&file->mutex);
  163. if (is_recv_mad &&
  164. atomic_read(&file->recv_list_size) > MAX_UMAD_RECV_LIST_SIZE)
  165. goto unlock;
  166. for (packet->mad.hdr.id = 0;
  167. packet->mad.hdr.id < IB_UMAD_MAX_AGENTS;
  168. packet->mad.hdr.id++)
  169. if (agent == __get_agent(file, packet->mad.hdr.id)) {
  170. list_add_tail(&packet->list, &file->recv_list);
  171. atomic_inc(&file->recv_list_size);
  172. wake_up_interruptible(&file->recv_wait);
  173. ret = 0;
  174. break;
  175. }
  176. unlock:
  177. mutex_unlock(&file->mutex);
  178. return ret;
  179. }
  180. static void dequeue_send(struct ib_umad_file *file,
  181. struct ib_umad_packet *packet)
  182. {
  183. spin_lock_irq(&file->send_lock);
  184. list_del(&packet->list);
  185. spin_unlock_irq(&file->send_lock);
  186. }
  187. static void send_handler(struct ib_mad_agent *agent,
  188. struct ib_mad_send_wc *send_wc)
  189. {
  190. struct ib_umad_file *file = agent->context;
  191. struct ib_umad_packet *packet = send_wc->send_buf->context[0];
  192. dequeue_send(file, packet);
  193. rdma_destroy_ah(packet->msg->ah, RDMA_DESTROY_AH_SLEEPABLE);
  194. ib_free_send_mad(packet->msg);
  195. if (send_wc->status == IB_WC_RESP_TIMEOUT_ERR) {
  196. packet->length = IB_MGMT_MAD_HDR;
  197. packet->mad.hdr.status = ETIMEDOUT;
  198. if (!queue_packet(file, agent, packet, false))
  199. return;
  200. }
  201. kfree(packet);
  202. }
  203. static void recv_handler(struct ib_mad_agent *agent,
  204. struct ib_mad_send_buf *send_buf,
  205. struct ib_mad_recv_wc *mad_recv_wc)
  206. {
  207. struct ib_umad_file *file = agent->context;
  208. struct ib_umad_packet *packet;
  209. if (mad_recv_wc->wc->status != IB_WC_SUCCESS)
  210. goto err1;
  211. packet = kzalloc_obj(*packet);
  212. if (!packet)
  213. goto err1;
  214. packet->length = mad_recv_wc->mad_len;
  215. packet->recv_wc = mad_recv_wc;
  216. packet->mad.hdr.status = 0;
  217. packet->mad.hdr.length = hdr_size(file) + mad_recv_wc->mad_len;
  218. packet->mad.hdr.qpn = cpu_to_be32(mad_recv_wc->wc->src_qp);
  219. /*
  220. * On OPA devices it is okay to lose the upper 16 bits of LID as this
  221. * information is obtained elsewhere. Mask off the upper 16 bits.
  222. */
  223. if (rdma_cap_opa_mad(agent->device, agent->port_num))
  224. packet->mad.hdr.lid = ib_lid_be16(0xFFFF &
  225. mad_recv_wc->wc->slid);
  226. else
  227. packet->mad.hdr.lid = ib_lid_be16(mad_recv_wc->wc->slid);
  228. packet->mad.hdr.sl = mad_recv_wc->wc->sl;
  229. packet->mad.hdr.path_bits = mad_recv_wc->wc->dlid_path_bits;
  230. packet->mad.hdr.pkey_index = mad_recv_wc->wc->pkey_index;
  231. packet->mad.hdr.grh_present = !!(mad_recv_wc->wc->wc_flags & IB_WC_GRH);
  232. if (packet->mad.hdr.grh_present) {
  233. struct rdma_ah_attr ah_attr;
  234. const struct ib_global_route *grh;
  235. int ret;
  236. ret = ib_init_ah_attr_from_wc(agent->device, agent->port_num,
  237. mad_recv_wc->wc,
  238. mad_recv_wc->recv_buf.grh,
  239. &ah_attr);
  240. if (ret)
  241. goto err2;
  242. grh = rdma_ah_read_grh(&ah_attr);
  243. packet->mad.hdr.gid_index = grh->sgid_index;
  244. packet->mad.hdr.hop_limit = grh->hop_limit;
  245. packet->mad.hdr.traffic_class = grh->traffic_class;
  246. memcpy(packet->mad.hdr.gid, &grh->dgid, 16);
  247. packet->mad.hdr.flow_label = cpu_to_be32(grh->flow_label);
  248. rdma_destroy_ah_attr(&ah_attr);
  249. }
  250. if (queue_packet(file, agent, packet, true))
  251. goto err2;
  252. return;
  253. err2:
  254. kfree(packet);
  255. err1:
  256. ib_free_recv_mad(mad_recv_wc);
  257. }
  258. static ssize_t copy_recv_mad(struct ib_umad_file *file, char __user *buf,
  259. struct ib_umad_packet *packet, size_t count)
  260. {
  261. struct ib_mad_recv_buf *recv_buf;
  262. int left, seg_payload, offset, max_seg_payload;
  263. size_t seg_size;
  264. recv_buf = &packet->recv_wc->recv_buf;
  265. seg_size = packet->recv_wc->mad_seg_size;
  266. /* We need enough room to copy the first (or only) MAD segment. */
  267. if ((packet->length <= seg_size &&
  268. count < hdr_size(file) + packet->length) ||
  269. (packet->length > seg_size &&
  270. count < hdr_size(file) + seg_size))
  271. return -EINVAL;
  272. if (copy_to_user(buf, &packet->mad, hdr_size(file)))
  273. return -EFAULT;
  274. buf += hdr_size(file);
  275. seg_payload = min_t(int, packet->length, seg_size);
  276. if (copy_to_user(buf, recv_buf->mad, seg_payload))
  277. return -EFAULT;
  278. if (seg_payload < packet->length) {
  279. /*
  280. * Multipacket RMPP MAD message. Copy remainder of message.
  281. * Note that last segment may have a shorter payload.
  282. */
  283. if (count < hdr_size(file) + packet->length) {
  284. /*
  285. * The buffer is too small, return the first RMPP segment,
  286. * which includes the RMPP message length.
  287. */
  288. return -ENOSPC;
  289. }
  290. offset = ib_get_mad_data_offset(recv_buf->mad->mad_hdr.mgmt_class);
  291. max_seg_payload = seg_size - offset;
  292. for (left = packet->length - seg_payload, buf += seg_payload;
  293. left; left -= seg_payload, buf += seg_payload) {
  294. recv_buf = container_of(recv_buf->list.next,
  295. struct ib_mad_recv_buf, list);
  296. seg_payload = min(left, max_seg_payload);
  297. if (copy_to_user(buf, ((void *) recv_buf->mad) + offset,
  298. seg_payload))
  299. return -EFAULT;
  300. }
  301. }
  302. trace_ib_umad_read_recv(file, &packet->mad.hdr, &recv_buf->mad->mad_hdr);
  303. return hdr_size(file) + packet->length;
  304. }
  305. static ssize_t copy_send_mad(struct ib_umad_file *file, char __user *buf,
  306. struct ib_umad_packet *packet, size_t count)
  307. {
  308. ssize_t size = hdr_size(file) + packet->length;
  309. if (count < size)
  310. return -EINVAL;
  311. if (copy_to_user(buf, &packet->mad, hdr_size(file)))
  312. return -EFAULT;
  313. buf += hdr_size(file);
  314. if (copy_to_user(buf, packet->mad.data, packet->length))
  315. return -EFAULT;
  316. trace_ib_umad_read_send(file, &packet->mad.hdr,
  317. (struct ib_mad_hdr *)&packet->mad.data);
  318. return size;
  319. }
  320. static ssize_t ib_umad_read(struct file *filp, char __user *buf,
  321. size_t count, loff_t *pos)
  322. {
  323. struct ib_umad_file *file = filp->private_data;
  324. struct ib_umad_packet *packet;
  325. ssize_t ret;
  326. if (count < hdr_size(file))
  327. return -EINVAL;
  328. mutex_lock(&file->mutex);
  329. if (file->agents_dead) {
  330. mutex_unlock(&file->mutex);
  331. return -EIO;
  332. }
  333. while (list_empty(&file->recv_list)) {
  334. mutex_unlock(&file->mutex);
  335. if (filp->f_flags & O_NONBLOCK)
  336. return -EAGAIN;
  337. if (wait_event_interruptible(file->recv_wait,
  338. !list_empty(&file->recv_list)))
  339. return -ERESTARTSYS;
  340. mutex_lock(&file->mutex);
  341. }
  342. if (file->agents_dead) {
  343. mutex_unlock(&file->mutex);
  344. return -EIO;
  345. }
  346. packet = list_entry(file->recv_list.next, struct ib_umad_packet, list);
  347. list_del(&packet->list);
  348. atomic_dec(&file->recv_list_size);
  349. mutex_unlock(&file->mutex);
  350. if (packet->recv_wc)
  351. ret = copy_recv_mad(file, buf, packet, count);
  352. else
  353. ret = copy_send_mad(file, buf, packet, count);
  354. if (ret < 0) {
  355. /* Requeue packet */
  356. mutex_lock(&file->mutex);
  357. list_add(&packet->list, &file->recv_list);
  358. atomic_inc(&file->recv_list_size);
  359. mutex_unlock(&file->mutex);
  360. } else {
  361. if (packet->recv_wc)
  362. ib_free_recv_mad(packet->recv_wc);
  363. kfree(packet);
  364. }
  365. return ret;
  366. }
  367. static int copy_rmpp_mad(struct ib_mad_send_buf *msg, const char __user *buf)
  368. {
  369. int left, seg;
  370. /* Copy class specific header */
  371. if ((msg->hdr_len > IB_MGMT_RMPP_HDR) &&
  372. copy_from_user(msg->mad + IB_MGMT_RMPP_HDR, buf + IB_MGMT_RMPP_HDR,
  373. msg->hdr_len - IB_MGMT_RMPP_HDR))
  374. return -EFAULT;
  375. /* All headers are in place. Copy data segments. */
  376. for (seg = 1, left = msg->data_len, buf += msg->hdr_len; left > 0;
  377. seg++, left -= msg->seg_size, buf += msg->seg_size) {
  378. if (copy_from_user(ib_get_rmpp_segment(msg, seg), buf,
  379. min(left, msg->seg_size)))
  380. return -EFAULT;
  381. }
  382. return 0;
  383. }
  384. static int same_destination(struct ib_user_mad_hdr *hdr1,
  385. struct ib_user_mad_hdr *hdr2)
  386. {
  387. if (!hdr1->grh_present && !hdr2->grh_present)
  388. return (hdr1->lid == hdr2->lid);
  389. if (hdr1->grh_present && hdr2->grh_present)
  390. return !memcmp(hdr1->gid, hdr2->gid, 16);
  391. return 0;
  392. }
  393. static int is_duplicate(struct ib_umad_file *file,
  394. struct ib_umad_packet *packet)
  395. {
  396. struct ib_umad_packet *sent_packet;
  397. struct ib_mad_hdr *sent_hdr, *hdr;
  398. hdr = (struct ib_mad_hdr *) packet->mad.data;
  399. list_for_each_entry(sent_packet, &file->send_list, list) {
  400. sent_hdr = (struct ib_mad_hdr *) sent_packet->mad.data;
  401. if ((hdr->tid != sent_hdr->tid) ||
  402. (hdr->mgmt_class != sent_hdr->mgmt_class))
  403. continue;
  404. /*
  405. * No need to be overly clever here. If two new operations have
  406. * the same TID, reject the second as a duplicate. This is more
  407. * restrictive than required by the spec.
  408. */
  409. if (!ib_response_mad(hdr)) {
  410. if (!ib_response_mad(sent_hdr))
  411. return 1;
  412. continue;
  413. } else if (!ib_response_mad(sent_hdr))
  414. continue;
  415. if (same_destination(&packet->mad.hdr, &sent_packet->mad.hdr))
  416. return 1;
  417. }
  418. return 0;
  419. }
  420. static ssize_t ib_umad_write(struct file *filp, const char __user *buf,
  421. size_t count, loff_t *pos)
  422. {
  423. struct ib_umad_file *file = filp->private_data;
  424. struct ib_rmpp_mad_hdr *rmpp_mad_hdr;
  425. struct ib_umad_packet *packet;
  426. struct ib_mad_agent *agent;
  427. struct rdma_ah_attr ah_attr;
  428. struct ib_ah *ah;
  429. __be64 *tid;
  430. int ret, hdr_len, copy_offset, rmpp_active;
  431. size_t data_len;
  432. u8 base_version;
  433. if (count < hdr_size(file) + IB_MGMT_RMPP_HDR)
  434. return -EINVAL;
  435. packet = kzalloc(sizeof(*packet) + IB_MGMT_RMPP_HDR, GFP_KERNEL);
  436. if (!packet)
  437. return -ENOMEM;
  438. if (copy_from_user(&packet->mad, buf, hdr_size(file))) {
  439. ret = -EFAULT;
  440. goto err;
  441. }
  442. if (packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
  443. ret = -EINVAL;
  444. goto err;
  445. }
  446. buf += hdr_size(file);
  447. if (copy_from_user(packet->mad.data, buf, IB_MGMT_RMPP_HDR)) {
  448. ret = -EFAULT;
  449. goto err;
  450. }
  451. mutex_lock(&file->mutex);
  452. trace_ib_umad_write(file, &packet->mad.hdr,
  453. (struct ib_mad_hdr *)&packet->mad.data);
  454. agent = __get_agent(file, packet->mad.hdr.id);
  455. if (!agent) {
  456. ret = -EIO;
  457. goto err_up;
  458. }
  459. memset(&ah_attr, 0, sizeof ah_attr);
  460. ah_attr.type = rdma_ah_find_type(agent->device,
  461. file->port->port_num);
  462. rdma_ah_set_dlid(&ah_attr, be16_to_cpu(packet->mad.hdr.lid));
  463. rdma_ah_set_sl(&ah_attr, packet->mad.hdr.sl);
  464. rdma_ah_set_path_bits(&ah_attr, packet->mad.hdr.path_bits);
  465. rdma_ah_set_port_num(&ah_attr, file->port->port_num);
  466. if (packet->mad.hdr.grh_present) {
  467. rdma_ah_set_grh(&ah_attr, NULL,
  468. be32_to_cpu(packet->mad.hdr.flow_label),
  469. packet->mad.hdr.gid_index,
  470. packet->mad.hdr.hop_limit,
  471. packet->mad.hdr.traffic_class);
  472. rdma_ah_set_dgid_raw(&ah_attr, packet->mad.hdr.gid);
  473. }
  474. ah = rdma_create_user_ah(agent->qp->pd, &ah_attr, NULL);
  475. if (IS_ERR(ah)) {
  476. ret = PTR_ERR(ah);
  477. goto err_up;
  478. }
  479. rmpp_mad_hdr = (struct ib_rmpp_mad_hdr *)packet->mad.data;
  480. hdr_len = ib_get_mad_data_offset(rmpp_mad_hdr->mad_hdr.mgmt_class);
  481. if (ib_is_mad_class_rmpp(rmpp_mad_hdr->mad_hdr.mgmt_class)
  482. && ib_mad_kernel_rmpp_agent(agent)) {
  483. copy_offset = IB_MGMT_RMPP_HDR;
  484. rmpp_active = ib_get_rmpp_flags(&rmpp_mad_hdr->rmpp_hdr) &
  485. IB_MGMT_RMPP_FLAG_ACTIVE;
  486. } else {
  487. copy_offset = IB_MGMT_MAD_HDR;
  488. rmpp_active = 0;
  489. }
  490. base_version = ((struct ib_mad_hdr *)&packet->mad.data)->base_version;
  491. if (check_sub_overflow(count, hdr_size(file) + hdr_len, &data_len)) {
  492. ret = -EINVAL;
  493. goto err_ah;
  494. }
  495. packet->msg = ib_create_send_mad(agent,
  496. be32_to_cpu(packet->mad.hdr.qpn),
  497. packet->mad.hdr.pkey_index, rmpp_active,
  498. hdr_len, data_len, GFP_KERNEL,
  499. base_version);
  500. if (IS_ERR(packet->msg)) {
  501. ret = PTR_ERR(packet->msg);
  502. goto err_ah;
  503. }
  504. packet->msg->ah = ah;
  505. packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
  506. packet->msg->retries = packet->mad.hdr.retries;
  507. packet->msg->context[0] = packet;
  508. /* Copy MAD header. Any RMPP header is already in place. */
  509. memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
  510. if (!rmpp_active) {
  511. if (copy_from_user(packet->msg->mad + copy_offset,
  512. buf + copy_offset,
  513. hdr_len + data_len - copy_offset)) {
  514. ret = -EFAULT;
  515. goto err_msg;
  516. }
  517. } else {
  518. ret = copy_rmpp_mad(packet->msg, buf);
  519. if (ret)
  520. goto err_msg;
  521. }
  522. /*
  523. * Set the high-order part of the transaction ID to make MADs from
  524. * different agents unique, and allow routing responses back to the
  525. * original requestor.
  526. */
  527. if (!ib_response_mad(packet->msg->mad)) {
  528. tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
  529. *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
  530. (be64_to_cpup(tid) & 0xffffffff));
  531. rmpp_mad_hdr->mad_hdr.tid = *tid;
  532. }
  533. if (!ib_mad_kernel_rmpp_agent(agent)
  534. && ib_is_mad_class_rmpp(rmpp_mad_hdr->mad_hdr.mgmt_class)
  535. && (ib_get_rmpp_flags(&rmpp_mad_hdr->rmpp_hdr) & IB_MGMT_RMPP_FLAG_ACTIVE)) {
  536. spin_lock_irq(&file->send_lock);
  537. list_add_tail(&packet->list, &file->send_list);
  538. spin_unlock_irq(&file->send_lock);
  539. } else {
  540. spin_lock_irq(&file->send_lock);
  541. ret = is_duplicate(file, packet);
  542. if (!ret)
  543. list_add_tail(&packet->list, &file->send_list);
  544. spin_unlock_irq(&file->send_lock);
  545. if (ret) {
  546. ret = -EINVAL;
  547. goto err_msg;
  548. }
  549. }
  550. ret = ib_post_send_mad(packet->msg, NULL);
  551. if (ret)
  552. goto err_send;
  553. mutex_unlock(&file->mutex);
  554. return count;
  555. err_send:
  556. dequeue_send(file, packet);
  557. err_msg:
  558. ib_free_send_mad(packet->msg);
  559. err_ah:
  560. rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE);
  561. err_up:
  562. mutex_unlock(&file->mutex);
  563. err:
  564. kfree(packet);
  565. return ret;
  566. }
  567. static __poll_t ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
  568. {
  569. struct ib_umad_file *file = filp->private_data;
  570. /* we will always be able to post a MAD send */
  571. __poll_t mask = EPOLLOUT | EPOLLWRNORM;
  572. mutex_lock(&file->mutex);
  573. poll_wait(filp, &file->recv_wait, wait);
  574. if (!list_empty(&file->recv_list))
  575. mask |= EPOLLIN | EPOLLRDNORM;
  576. if (file->agents_dead)
  577. mask = EPOLLERR;
  578. mutex_unlock(&file->mutex);
  579. return mask;
  580. }
  581. static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg,
  582. int compat_method_mask)
  583. {
  584. struct ib_user_mad_reg_req ureq;
  585. struct ib_mad_reg_req req;
  586. struct ib_mad_agent *agent = NULL;
  587. int agent_id;
  588. int ret;
  589. mutex_lock(&file->port->file_mutex);
  590. mutex_lock(&file->mutex);
  591. if (!file->port->ib_dev) {
  592. dev_notice(&file->port->dev, "%s: invalid device\n", __func__);
  593. ret = -EPIPE;
  594. goto out;
  595. }
  596. if (copy_from_user(&ureq, arg, sizeof ureq)) {
  597. ret = -EFAULT;
  598. goto out;
  599. }
  600. if (ureq.qpn != 0 && ureq.qpn != 1) {
  601. dev_notice(&file->port->dev,
  602. "%s: invalid QPN %u specified\n", __func__,
  603. ureq.qpn);
  604. ret = -EINVAL;
  605. goto out;
  606. }
  607. for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
  608. if (!__get_agent(file, agent_id))
  609. goto found;
  610. dev_notice(&file->port->dev, "%s: Max Agents (%u) reached\n", __func__,
  611. IB_UMAD_MAX_AGENTS);
  612. ret = -ENOMEM;
  613. goto out;
  614. found:
  615. if (ureq.mgmt_class) {
  616. memset(&req, 0, sizeof(req));
  617. req.mgmt_class = ureq.mgmt_class;
  618. req.mgmt_class_version = ureq.mgmt_class_version;
  619. memcpy(req.oui, ureq.oui, sizeof req.oui);
  620. if (compat_method_mask) {
  621. u32 *umm = (u32 *) ureq.method_mask;
  622. int i;
  623. for (i = 0; i < BITS_TO_LONGS(IB_MGMT_MAX_METHODS); ++i)
  624. req.method_mask[i] =
  625. umm[i * 2] | ((u64) umm[i * 2 + 1] << 32);
  626. } else
  627. memcpy(req.method_mask, ureq.method_mask,
  628. sizeof req.method_mask);
  629. }
  630. agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
  631. ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
  632. ureq.mgmt_class ? &req : NULL,
  633. ureq.rmpp_version,
  634. send_handler, recv_handler, file, 0);
  635. if (IS_ERR(agent)) {
  636. ret = PTR_ERR(agent);
  637. agent = NULL;
  638. goto out;
  639. }
  640. if (put_user(agent_id,
  641. (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
  642. ret = -EFAULT;
  643. goto out;
  644. }
  645. if (!file->already_used) {
  646. file->already_used = 1;
  647. if (!file->use_pkey_index) {
  648. dev_warn(&file->port->dev,
  649. "process %s did not enable P_Key index support.\n",
  650. current->comm);
  651. dev_warn(&file->port->dev,
  652. " Documentation/infiniband/user_mad.rst has info on the new ABI.\n");
  653. }
  654. }
  655. file->agent[agent_id] = agent;
  656. ret = 0;
  657. out:
  658. mutex_unlock(&file->mutex);
  659. if (ret && agent)
  660. ib_unregister_mad_agent(agent);
  661. mutex_unlock(&file->port->file_mutex);
  662. return ret;
  663. }
  664. static int ib_umad_reg_agent2(struct ib_umad_file *file, void __user *arg)
  665. {
  666. struct ib_user_mad_reg_req2 ureq;
  667. struct ib_mad_reg_req req;
  668. struct ib_mad_agent *agent = NULL;
  669. int agent_id;
  670. int ret;
  671. mutex_lock(&file->port->file_mutex);
  672. mutex_lock(&file->mutex);
  673. if (!file->port->ib_dev) {
  674. dev_notice(&file->port->dev, "%s: invalid device\n", __func__);
  675. ret = -EPIPE;
  676. goto out;
  677. }
  678. if (copy_from_user(&ureq, arg, sizeof(ureq))) {
  679. ret = -EFAULT;
  680. goto out;
  681. }
  682. if (ureq.qpn != 0 && ureq.qpn != 1) {
  683. dev_notice(&file->port->dev, "%s: invalid QPN %u specified\n",
  684. __func__, ureq.qpn);
  685. ret = -EINVAL;
  686. goto out;
  687. }
  688. if (ureq.flags & ~IB_USER_MAD_REG_FLAGS_CAP) {
  689. dev_notice(&file->port->dev,
  690. "%s failed: invalid registration flags specified 0x%x; supported 0x%x\n",
  691. __func__, ureq.flags, IB_USER_MAD_REG_FLAGS_CAP);
  692. ret = -EINVAL;
  693. if (put_user((u32)IB_USER_MAD_REG_FLAGS_CAP,
  694. (u32 __user *) (arg + offsetof(struct
  695. ib_user_mad_reg_req2, flags))))
  696. ret = -EFAULT;
  697. goto out;
  698. }
  699. for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
  700. if (!__get_agent(file, agent_id))
  701. goto found;
  702. dev_notice(&file->port->dev, "%s: Max Agents (%u) reached\n", __func__,
  703. IB_UMAD_MAX_AGENTS);
  704. ret = -ENOMEM;
  705. goto out;
  706. found:
  707. if (ureq.mgmt_class) {
  708. memset(&req, 0, sizeof(req));
  709. req.mgmt_class = ureq.mgmt_class;
  710. req.mgmt_class_version = ureq.mgmt_class_version;
  711. if (ureq.oui & 0xff000000) {
  712. dev_notice(&file->port->dev,
  713. "%s failed: oui invalid 0x%08x\n", __func__,
  714. ureq.oui);
  715. ret = -EINVAL;
  716. goto out;
  717. }
  718. req.oui[2] = ureq.oui & 0x0000ff;
  719. req.oui[1] = (ureq.oui & 0x00ff00) >> 8;
  720. req.oui[0] = (ureq.oui & 0xff0000) >> 16;
  721. memcpy(req.method_mask, ureq.method_mask,
  722. sizeof(req.method_mask));
  723. }
  724. agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
  725. ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
  726. ureq.mgmt_class ? &req : NULL,
  727. ureq.rmpp_version,
  728. send_handler, recv_handler, file,
  729. ureq.flags);
  730. if (IS_ERR(agent)) {
  731. ret = PTR_ERR(agent);
  732. agent = NULL;
  733. goto out;
  734. }
  735. if (put_user(agent_id,
  736. (u32 __user *)(arg +
  737. offsetof(struct ib_user_mad_reg_req2, id)))) {
  738. ret = -EFAULT;
  739. goto out;
  740. }
  741. if (!file->already_used) {
  742. file->already_used = 1;
  743. file->use_pkey_index = 1;
  744. }
  745. file->agent[agent_id] = agent;
  746. ret = 0;
  747. out:
  748. mutex_unlock(&file->mutex);
  749. if (ret && agent)
  750. ib_unregister_mad_agent(agent);
  751. mutex_unlock(&file->port->file_mutex);
  752. return ret;
  753. }
  754. static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)
  755. {
  756. struct ib_mad_agent *agent = NULL;
  757. u32 id;
  758. int ret = 0;
  759. if (get_user(id, arg))
  760. return -EFAULT;
  761. if (id >= IB_UMAD_MAX_AGENTS)
  762. return -EINVAL;
  763. mutex_lock(&file->port->file_mutex);
  764. mutex_lock(&file->mutex);
  765. id = array_index_nospec(id, IB_UMAD_MAX_AGENTS);
  766. if (!__get_agent(file, id)) {
  767. ret = -EINVAL;
  768. goto out;
  769. }
  770. agent = file->agent[id];
  771. file->agent[id] = NULL;
  772. out:
  773. mutex_unlock(&file->mutex);
  774. if (agent)
  775. ib_unregister_mad_agent(agent);
  776. mutex_unlock(&file->port->file_mutex);
  777. return ret;
  778. }
  779. static long ib_umad_enable_pkey(struct ib_umad_file *file)
  780. {
  781. int ret = 0;
  782. mutex_lock(&file->mutex);
  783. if (file->already_used)
  784. ret = -EINVAL;
  785. else
  786. file->use_pkey_index = 1;
  787. mutex_unlock(&file->mutex);
  788. return ret;
  789. }
  790. static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
  791. unsigned long arg)
  792. {
  793. switch (cmd) {
  794. case IB_USER_MAD_REGISTER_AGENT:
  795. return ib_umad_reg_agent(filp->private_data, (void __user *) arg, 0);
  796. case IB_USER_MAD_UNREGISTER_AGENT:
  797. return ib_umad_unreg_agent(filp->private_data, (__u32 __user *) arg);
  798. case IB_USER_MAD_ENABLE_PKEY:
  799. return ib_umad_enable_pkey(filp->private_data);
  800. case IB_USER_MAD_REGISTER_AGENT2:
  801. return ib_umad_reg_agent2(filp->private_data, (void __user *) arg);
  802. default:
  803. return -ENOIOCTLCMD;
  804. }
  805. }
  806. #ifdef CONFIG_COMPAT
  807. static long ib_umad_compat_ioctl(struct file *filp, unsigned int cmd,
  808. unsigned long arg)
  809. {
  810. switch (cmd) {
  811. case IB_USER_MAD_REGISTER_AGENT:
  812. return ib_umad_reg_agent(filp->private_data, compat_ptr(arg), 1);
  813. case IB_USER_MAD_UNREGISTER_AGENT:
  814. return ib_umad_unreg_agent(filp->private_data, compat_ptr(arg));
  815. case IB_USER_MAD_ENABLE_PKEY:
  816. return ib_umad_enable_pkey(filp->private_data);
  817. case IB_USER_MAD_REGISTER_AGENT2:
  818. return ib_umad_reg_agent2(filp->private_data, compat_ptr(arg));
  819. default:
  820. return -ENOIOCTLCMD;
  821. }
  822. }
  823. #endif
  824. /*
  825. * ib_umad_open() does not need the BKL:
  826. *
  827. * - the ib_umad_port structures are properly reference counted, and
  828. * everything else is purely local to the file being created, so
  829. * races against other open calls are not a problem;
  830. * - the ioctl method does not affect any global state outside of the
  831. * file structure being operated on;
  832. */
  833. static int ib_umad_open(struct inode *inode, struct file *filp)
  834. {
  835. struct ib_umad_port *port;
  836. struct ib_umad_file *file;
  837. int ret = 0;
  838. port = container_of(inode->i_cdev, struct ib_umad_port, cdev);
  839. mutex_lock(&port->file_mutex);
  840. if (!port->ib_dev) {
  841. ret = -ENXIO;
  842. goto out;
  843. }
  844. if (!rdma_dev_access_netns(port->ib_dev, current->nsproxy->net_ns)) {
  845. ret = -EPERM;
  846. goto out;
  847. }
  848. file = kzalloc_obj(*file);
  849. if (!file) {
  850. ret = -ENOMEM;
  851. goto out;
  852. }
  853. mutex_init(&file->mutex);
  854. spin_lock_init(&file->send_lock);
  855. INIT_LIST_HEAD(&file->recv_list);
  856. INIT_LIST_HEAD(&file->send_list);
  857. init_waitqueue_head(&file->recv_wait);
  858. file->port = port;
  859. filp->private_data = file;
  860. list_add_tail(&file->port_list, &port->file_list);
  861. stream_open(inode, filp);
  862. out:
  863. mutex_unlock(&port->file_mutex);
  864. return ret;
  865. }
  866. static int ib_umad_close(struct inode *inode, struct file *filp)
  867. {
  868. struct ib_umad_file *file = filp->private_data;
  869. struct ib_umad_packet *packet, *tmp;
  870. int already_dead;
  871. int i;
  872. mutex_lock(&file->port->file_mutex);
  873. mutex_lock(&file->mutex);
  874. already_dead = file->agents_dead;
  875. file->agents_dead = 1;
  876. list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {
  877. if (packet->recv_wc)
  878. ib_free_recv_mad(packet->recv_wc);
  879. kfree(packet);
  880. }
  881. list_del(&file->port_list);
  882. mutex_unlock(&file->mutex);
  883. if (!already_dead)
  884. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  885. if (file->agent[i])
  886. ib_unregister_mad_agent(file->agent[i]);
  887. mutex_unlock(&file->port->file_mutex);
  888. mutex_destroy(&file->mutex);
  889. kfree(file);
  890. return 0;
  891. }
  892. static const struct file_operations umad_fops = {
  893. .owner = THIS_MODULE,
  894. .read = ib_umad_read,
  895. .write = ib_umad_write,
  896. .poll = ib_umad_poll,
  897. .unlocked_ioctl = ib_umad_ioctl,
  898. #ifdef CONFIG_COMPAT
  899. .compat_ioctl = ib_umad_compat_ioctl,
  900. #endif
  901. .open = ib_umad_open,
  902. .release = ib_umad_close,
  903. };
  904. static int ib_umad_sm_open(struct inode *inode, struct file *filp)
  905. {
  906. struct ib_umad_port *port;
  907. struct ib_port_modify props = {
  908. .set_port_cap_mask = IB_PORT_SM
  909. };
  910. int ret;
  911. port = container_of(inode->i_cdev, struct ib_umad_port, sm_cdev);
  912. if (filp->f_flags & O_NONBLOCK) {
  913. if (down_trylock(&port->sm_sem)) {
  914. ret = -EAGAIN;
  915. goto fail;
  916. }
  917. } else {
  918. if (down_interruptible(&port->sm_sem)) {
  919. ret = -ERESTARTSYS;
  920. goto fail;
  921. }
  922. }
  923. if (!rdma_dev_access_netns(port->ib_dev, current->nsproxy->net_ns)) {
  924. ret = -EPERM;
  925. goto err_up_sem;
  926. }
  927. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  928. if (ret)
  929. goto err_up_sem;
  930. filp->private_data = port;
  931. nonseekable_open(inode, filp);
  932. return 0;
  933. err_up_sem:
  934. up(&port->sm_sem);
  935. fail:
  936. return ret;
  937. }
  938. static int ib_umad_sm_close(struct inode *inode, struct file *filp)
  939. {
  940. struct ib_umad_port *port = filp->private_data;
  941. struct ib_port_modify props = {
  942. .clr_port_cap_mask = IB_PORT_SM
  943. };
  944. int ret = 0;
  945. mutex_lock(&port->file_mutex);
  946. if (port->ib_dev)
  947. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  948. mutex_unlock(&port->file_mutex);
  949. up(&port->sm_sem);
  950. return ret;
  951. }
  952. static const struct file_operations umad_sm_fops = {
  953. .owner = THIS_MODULE,
  954. .open = ib_umad_sm_open,
  955. .release = ib_umad_sm_close,
  956. };
  957. static struct ib_umad_port *get_port(struct ib_device *ibdev,
  958. struct ib_umad_device *umad_dev,
  959. u32 port)
  960. {
  961. if (!umad_dev)
  962. return ERR_PTR(-EOPNOTSUPP);
  963. if (!rdma_is_port_valid(ibdev, port))
  964. return ERR_PTR(-EINVAL);
  965. if (!rdma_cap_ib_mad(ibdev, port))
  966. return ERR_PTR(-EOPNOTSUPP);
  967. return &umad_dev->ports[port - rdma_start_port(ibdev)];
  968. }
  969. static int ib_umad_get_nl_info(struct ib_device *ibdev, void *client_data,
  970. struct ib_client_nl_info *res)
  971. {
  972. struct ib_umad_port *port = get_port(ibdev, client_data, res->port);
  973. if (IS_ERR(port))
  974. return PTR_ERR(port);
  975. res->abi = IB_USER_MAD_ABI_VERSION;
  976. res->cdev = &port->dev;
  977. return 0;
  978. }
  979. static struct ib_client umad_client = {
  980. .name = "umad",
  981. .add = ib_umad_add_one,
  982. .remove = ib_umad_remove_one,
  983. .get_nl_info = ib_umad_get_nl_info,
  984. };
  985. MODULE_ALIAS_RDMA_CLIENT("umad");
  986. static int ib_issm_get_nl_info(struct ib_device *ibdev, void *client_data,
  987. struct ib_client_nl_info *res)
  988. {
  989. struct ib_umad_port *port = get_port(ibdev, client_data, res->port);
  990. if (IS_ERR(port))
  991. return PTR_ERR(port);
  992. res->abi = IB_USER_MAD_ABI_VERSION;
  993. res->cdev = &port->sm_dev;
  994. return 0;
  995. }
  996. static struct ib_client issm_client = {
  997. .name = "issm",
  998. .get_nl_info = ib_issm_get_nl_info,
  999. };
  1000. MODULE_ALIAS_RDMA_CLIENT("issm");
  1001. static ssize_t ibdev_show(struct device *dev, struct device_attribute *attr,
  1002. char *buf)
  1003. {
  1004. struct ib_umad_port *port = dev_get_drvdata(dev);
  1005. if (!port)
  1006. return -ENODEV;
  1007. return sysfs_emit(buf, "%s\n", dev_name(&port->ib_dev->dev));
  1008. }
  1009. static DEVICE_ATTR_RO(ibdev);
  1010. static ssize_t port_show(struct device *dev, struct device_attribute *attr,
  1011. char *buf)
  1012. {
  1013. struct ib_umad_port *port = dev_get_drvdata(dev);
  1014. if (!port)
  1015. return -ENODEV;
  1016. return sysfs_emit(buf, "%d\n", port->port_num);
  1017. }
  1018. static DEVICE_ATTR_RO(port);
  1019. static struct attribute *umad_class_dev_attrs[] = {
  1020. &dev_attr_ibdev.attr,
  1021. &dev_attr_port.attr,
  1022. NULL,
  1023. };
  1024. ATTRIBUTE_GROUPS(umad_class_dev);
  1025. static char *umad_devnode(const struct device *dev, umode_t *mode)
  1026. {
  1027. return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
  1028. }
  1029. static ssize_t abi_version_show(const struct class *class,
  1030. const struct class_attribute *attr, char *buf)
  1031. {
  1032. return sysfs_emit(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
  1033. }
  1034. static CLASS_ATTR_RO(abi_version);
  1035. static struct attribute *umad_class_attrs[] = {
  1036. &class_attr_abi_version.attr,
  1037. NULL,
  1038. };
  1039. ATTRIBUTE_GROUPS(umad_class);
  1040. static struct class umad_class = {
  1041. .name = "infiniband_mad",
  1042. .devnode = umad_devnode,
  1043. .class_groups = umad_class_groups,
  1044. .dev_groups = umad_class_dev_groups,
  1045. };
  1046. static void ib_umad_release_port(struct device *device)
  1047. {
  1048. struct ib_umad_port *port = dev_get_drvdata(device);
  1049. struct ib_umad_device *umad_dev = port->umad_dev;
  1050. ib_umad_dev_put(umad_dev);
  1051. }
  1052. static void ib_umad_init_port_dev(struct device *dev,
  1053. struct ib_umad_port *port,
  1054. const struct ib_device *device)
  1055. {
  1056. device_initialize(dev);
  1057. ib_umad_dev_get(port->umad_dev);
  1058. dev->class = &umad_class;
  1059. dev->parent = device->dev.parent;
  1060. dev_set_drvdata(dev, port);
  1061. dev->release = ib_umad_release_port;
  1062. }
  1063. static int ib_umad_init_port(struct ib_device *device, int port_num,
  1064. struct ib_umad_device *umad_dev,
  1065. struct ib_umad_port *port)
  1066. {
  1067. int devnum;
  1068. dev_t base_umad;
  1069. dev_t base_issm;
  1070. int ret;
  1071. devnum = ida_alloc_max(&umad_ida, IB_UMAD_MAX_PORTS - 1, GFP_KERNEL);
  1072. if (devnum < 0)
  1073. return -1;
  1074. port->dev_num = devnum;
  1075. if (devnum >= IB_UMAD_NUM_FIXED_MINOR) {
  1076. base_umad = dynamic_umad_dev + devnum - IB_UMAD_NUM_FIXED_MINOR;
  1077. base_issm = dynamic_issm_dev + devnum - IB_UMAD_NUM_FIXED_MINOR;
  1078. } else {
  1079. base_umad = devnum + base_umad_dev;
  1080. base_issm = devnum + base_issm_dev;
  1081. }
  1082. port->ib_dev = device;
  1083. port->umad_dev = umad_dev;
  1084. port->port_num = port_num;
  1085. sema_init(&port->sm_sem, 1);
  1086. mutex_init(&port->file_mutex);
  1087. INIT_LIST_HEAD(&port->file_list);
  1088. ib_umad_init_port_dev(&port->dev, port, device);
  1089. port->dev.devt = base_umad;
  1090. dev_set_name(&port->dev, "umad%d", port->dev_num);
  1091. cdev_init(&port->cdev, &umad_fops);
  1092. port->cdev.owner = THIS_MODULE;
  1093. ret = cdev_device_add(&port->cdev, &port->dev);
  1094. if (ret)
  1095. goto err_cdev;
  1096. if (rdma_cap_ib_smi(device, port_num)) {
  1097. ib_umad_init_port_dev(&port->sm_dev, port, device);
  1098. port->sm_dev.devt = base_issm;
  1099. dev_set_name(&port->sm_dev, "issm%d", port->dev_num);
  1100. cdev_init(&port->sm_cdev, &umad_sm_fops);
  1101. port->sm_cdev.owner = THIS_MODULE;
  1102. ret = cdev_device_add(&port->sm_cdev, &port->sm_dev);
  1103. if (ret)
  1104. goto err_dev;
  1105. }
  1106. return 0;
  1107. err_dev:
  1108. put_device(&port->sm_dev);
  1109. cdev_device_del(&port->cdev, &port->dev);
  1110. err_cdev:
  1111. put_device(&port->dev);
  1112. ida_free(&umad_ida, devnum);
  1113. return ret;
  1114. }
  1115. static void ib_umad_kill_port(struct ib_umad_port *port)
  1116. {
  1117. struct ib_umad_file *file;
  1118. bool has_smi = false;
  1119. int id;
  1120. if (rdma_cap_ib_smi(port->ib_dev, port->port_num)) {
  1121. cdev_device_del(&port->sm_cdev, &port->sm_dev);
  1122. has_smi = true;
  1123. }
  1124. cdev_device_del(&port->cdev, &port->dev);
  1125. mutex_lock(&port->file_mutex);
  1126. /* Mark ib_dev NULL and block ioctl or other file ops to progress
  1127. * further.
  1128. */
  1129. port->ib_dev = NULL;
  1130. list_for_each_entry(file, &port->file_list, port_list) {
  1131. mutex_lock(&file->mutex);
  1132. file->agents_dead = 1;
  1133. wake_up_interruptible(&file->recv_wait);
  1134. mutex_unlock(&file->mutex);
  1135. for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
  1136. if (file->agent[id])
  1137. ib_unregister_mad_agent(file->agent[id]);
  1138. }
  1139. mutex_unlock(&port->file_mutex);
  1140. ida_free(&umad_ida, port->dev_num);
  1141. /* balances device_initialize() */
  1142. if (has_smi)
  1143. put_device(&port->sm_dev);
  1144. put_device(&port->dev);
  1145. }
  1146. static int ib_umad_add_one(struct ib_device *device)
  1147. {
  1148. struct ib_umad_device *umad_dev;
  1149. int s, e, i;
  1150. int count = 0;
  1151. int ret;
  1152. s = rdma_start_port(device);
  1153. e = rdma_end_port(device);
  1154. umad_dev = kzalloc_flex(*umad_dev, ports, size_add(size_sub(e, s), 1));
  1155. if (!umad_dev)
  1156. return -ENOMEM;
  1157. kref_init(&umad_dev->kref);
  1158. for (i = s; i <= e; ++i) {
  1159. if (!rdma_cap_ib_mad(device, i))
  1160. continue;
  1161. ret = ib_umad_init_port(device, i, umad_dev,
  1162. &umad_dev->ports[i - s]);
  1163. if (ret)
  1164. goto err;
  1165. count++;
  1166. }
  1167. if (!count) {
  1168. ret = -EOPNOTSUPP;
  1169. goto free;
  1170. }
  1171. ib_set_client_data(device, &umad_client, umad_dev);
  1172. return 0;
  1173. err:
  1174. while (--i >= s) {
  1175. if (!rdma_cap_ib_mad(device, i))
  1176. continue;
  1177. ib_umad_kill_port(&umad_dev->ports[i - s]);
  1178. }
  1179. free:
  1180. /* balances kref_init */
  1181. ib_umad_dev_put(umad_dev);
  1182. return ret;
  1183. }
  1184. static void ib_umad_remove_one(struct ib_device *device, void *client_data)
  1185. {
  1186. struct ib_umad_device *umad_dev = client_data;
  1187. unsigned int i;
  1188. rdma_for_each_port (device, i) {
  1189. if (rdma_cap_ib_mad(device, i))
  1190. ib_umad_kill_port(
  1191. &umad_dev->ports[i - rdma_start_port(device)]);
  1192. }
  1193. /* balances kref_init() */
  1194. ib_umad_dev_put(umad_dev);
  1195. }
  1196. static int __init ib_umad_init(void)
  1197. {
  1198. int ret;
  1199. ret = register_chrdev_region(base_umad_dev,
  1200. IB_UMAD_NUM_FIXED_MINOR * 2,
  1201. umad_class.name);
  1202. if (ret) {
  1203. pr_err("couldn't register device number\n");
  1204. goto out;
  1205. }
  1206. ret = alloc_chrdev_region(&dynamic_umad_dev, 0,
  1207. IB_UMAD_NUM_DYNAMIC_MINOR * 2,
  1208. umad_class.name);
  1209. if (ret) {
  1210. pr_err("couldn't register dynamic device number\n");
  1211. goto out_alloc;
  1212. }
  1213. dynamic_issm_dev = dynamic_umad_dev + IB_UMAD_NUM_DYNAMIC_MINOR;
  1214. ret = class_register(&umad_class);
  1215. if (ret) {
  1216. pr_err("couldn't create class infiniband_mad\n");
  1217. goto out_chrdev;
  1218. }
  1219. ret = ib_register_client(&umad_client);
  1220. if (ret)
  1221. goto out_class;
  1222. ret = ib_register_client(&issm_client);
  1223. if (ret)
  1224. goto out_client;
  1225. return 0;
  1226. out_client:
  1227. ib_unregister_client(&umad_client);
  1228. out_class:
  1229. class_unregister(&umad_class);
  1230. out_chrdev:
  1231. unregister_chrdev_region(dynamic_umad_dev,
  1232. IB_UMAD_NUM_DYNAMIC_MINOR * 2);
  1233. out_alloc:
  1234. unregister_chrdev_region(base_umad_dev,
  1235. IB_UMAD_NUM_FIXED_MINOR * 2);
  1236. out:
  1237. return ret;
  1238. }
  1239. static void __exit ib_umad_cleanup(void)
  1240. {
  1241. ib_unregister_client(&issm_client);
  1242. ib_unregister_client(&umad_client);
  1243. class_unregister(&umad_class);
  1244. unregister_chrdev_region(base_umad_dev,
  1245. IB_UMAD_NUM_FIXED_MINOR * 2);
  1246. unregister_chrdev_region(dynamic_umad_dev,
  1247. IB_UMAD_NUM_DYNAMIC_MINOR * 2);
  1248. }
  1249. module_init(ib_umad_init);
  1250. module_exit(ib_umad_cleanup);