user_mad.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  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(sizeof *packet, GFP_KERNEL);
  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, data_len, hdr_len, copy_offset, rmpp_active;
  431. u8 base_version;
  432. if (count < hdr_size(file) + IB_MGMT_RMPP_HDR)
  433. return -EINVAL;
  434. packet = kzalloc(sizeof(*packet) + IB_MGMT_RMPP_HDR, GFP_KERNEL);
  435. if (!packet)
  436. return -ENOMEM;
  437. if (copy_from_user(&packet->mad, buf, hdr_size(file))) {
  438. ret = -EFAULT;
  439. goto err;
  440. }
  441. if (packet->mad.hdr.id >= IB_UMAD_MAX_AGENTS) {
  442. ret = -EINVAL;
  443. goto err;
  444. }
  445. buf += hdr_size(file);
  446. if (copy_from_user(packet->mad.data, buf, IB_MGMT_RMPP_HDR)) {
  447. ret = -EFAULT;
  448. goto err;
  449. }
  450. mutex_lock(&file->mutex);
  451. trace_ib_umad_write(file, &packet->mad.hdr,
  452. (struct ib_mad_hdr *)&packet->mad.data);
  453. agent = __get_agent(file, packet->mad.hdr.id);
  454. if (!agent) {
  455. ret = -EIO;
  456. goto err_up;
  457. }
  458. memset(&ah_attr, 0, sizeof ah_attr);
  459. ah_attr.type = rdma_ah_find_type(agent->device,
  460. file->port->port_num);
  461. rdma_ah_set_dlid(&ah_attr, be16_to_cpu(packet->mad.hdr.lid));
  462. rdma_ah_set_sl(&ah_attr, packet->mad.hdr.sl);
  463. rdma_ah_set_path_bits(&ah_attr, packet->mad.hdr.path_bits);
  464. rdma_ah_set_port_num(&ah_attr, file->port->port_num);
  465. if (packet->mad.hdr.grh_present) {
  466. rdma_ah_set_grh(&ah_attr, NULL,
  467. be32_to_cpu(packet->mad.hdr.flow_label),
  468. packet->mad.hdr.gid_index,
  469. packet->mad.hdr.hop_limit,
  470. packet->mad.hdr.traffic_class);
  471. rdma_ah_set_dgid_raw(&ah_attr, packet->mad.hdr.gid);
  472. }
  473. ah = rdma_create_user_ah(agent->qp->pd, &ah_attr, NULL);
  474. if (IS_ERR(ah)) {
  475. ret = PTR_ERR(ah);
  476. goto err_up;
  477. }
  478. rmpp_mad_hdr = (struct ib_rmpp_mad_hdr *)packet->mad.data;
  479. hdr_len = ib_get_mad_data_offset(rmpp_mad_hdr->mad_hdr.mgmt_class);
  480. if (ib_is_mad_class_rmpp(rmpp_mad_hdr->mad_hdr.mgmt_class)
  481. && ib_mad_kernel_rmpp_agent(agent)) {
  482. copy_offset = IB_MGMT_RMPP_HDR;
  483. rmpp_active = ib_get_rmpp_flags(&rmpp_mad_hdr->rmpp_hdr) &
  484. IB_MGMT_RMPP_FLAG_ACTIVE;
  485. } else {
  486. copy_offset = IB_MGMT_MAD_HDR;
  487. rmpp_active = 0;
  488. }
  489. base_version = ((struct ib_mad_hdr *)&packet->mad.data)->base_version;
  490. data_len = count - hdr_size(file) - hdr_len;
  491. packet->msg = ib_create_send_mad(agent,
  492. be32_to_cpu(packet->mad.hdr.qpn),
  493. packet->mad.hdr.pkey_index, rmpp_active,
  494. hdr_len, data_len, GFP_KERNEL,
  495. base_version);
  496. if (IS_ERR(packet->msg)) {
  497. ret = PTR_ERR(packet->msg);
  498. goto err_ah;
  499. }
  500. packet->msg->ah = ah;
  501. packet->msg->timeout_ms = packet->mad.hdr.timeout_ms;
  502. packet->msg->retries = packet->mad.hdr.retries;
  503. packet->msg->context[0] = packet;
  504. /* Copy MAD header. Any RMPP header is already in place. */
  505. memcpy(packet->msg->mad, packet->mad.data, IB_MGMT_MAD_HDR);
  506. if (!rmpp_active) {
  507. if (copy_from_user(packet->msg->mad + copy_offset,
  508. buf + copy_offset,
  509. hdr_len + data_len - copy_offset)) {
  510. ret = -EFAULT;
  511. goto err_msg;
  512. }
  513. } else {
  514. ret = copy_rmpp_mad(packet->msg, buf);
  515. if (ret)
  516. goto err_msg;
  517. }
  518. /*
  519. * Set the high-order part of the transaction ID to make MADs from
  520. * different agents unique, and allow routing responses back to the
  521. * original requestor.
  522. */
  523. if (!ib_response_mad(packet->msg->mad)) {
  524. tid = &((struct ib_mad_hdr *) packet->msg->mad)->tid;
  525. *tid = cpu_to_be64(((u64) agent->hi_tid) << 32 |
  526. (be64_to_cpup(tid) & 0xffffffff));
  527. rmpp_mad_hdr->mad_hdr.tid = *tid;
  528. }
  529. if (!ib_mad_kernel_rmpp_agent(agent)
  530. && ib_is_mad_class_rmpp(rmpp_mad_hdr->mad_hdr.mgmt_class)
  531. && (ib_get_rmpp_flags(&rmpp_mad_hdr->rmpp_hdr) & IB_MGMT_RMPP_FLAG_ACTIVE)) {
  532. spin_lock_irq(&file->send_lock);
  533. list_add_tail(&packet->list, &file->send_list);
  534. spin_unlock_irq(&file->send_lock);
  535. } else {
  536. spin_lock_irq(&file->send_lock);
  537. ret = is_duplicate(file, packet);
  538. if (!ret)
  539. list_add_tail(&packet->list, &file->send_list);
  540. spin_unlock_irq(&file->send_lock);
  541. if (ret) {
  542. ret = -EINVAL;
  543. goto err_msg;
  544. }
  545. }
  546. ret = ib_post_send_mad(packet->msg, NULL);
  547. if (ret)
  548. goto err_send;
  549. mutex_unlock(&file->mutex);
  550. return count;
  551. err_send:
  552. dequeue_send(file, packet);
  553. err_msg:
  554. ib_free_send_mad(packet->msg);
  555. err_ah:
  556. rdma_destroy_ah(ah, RDMA_DESTROY_AH_SLEEPABLE);
  557. err_up:
  558. mutex_unlock(&file->mutex);
  559. err:
  560. kfree(packet);
  561. return ret;
  562. }
  563. static __poll_t ib_umad_poll(struct file *filp, struct poll_table_struct *wait)
  564. {
  565. struct ib_umad_file *file = filp->private_data;
  566. /* we will always be able to post a MAD send */
  567. __poll_t mask = EPOLLOUT | EPOLLWRNORM;
  568. mutex_lock(&file->mutex);
  569. poll_wait(filp, &file->recv_wait, wait);
  570. if (!list_empty(&file->recv_list))
  571. mask |= EPOLLIN | EPOLLRDNORM;
  572. if (file->agents_dead)
  573. mask = EPOLLERR;
  574. mutex_unlock(&file->mutex);
  575. return mask;
  576. }
  577. static int ib_umad_reg_agent(struct ib_umad_file *file, void __user *arg,
  578. int compat_method_mask)
  579. {
  580. struct ib_user_mad_reg_req ureq;
  581. struct ib_mad_reg_req req;
  582. struct ib_mad_agent *agent = NULL;
  583. int agent_id;
  584. int ret;
  585. mutex_lock(&file->port->file_mutex);
  586. mutex_lock(&file->mutex);
  587. if (!file->port->ib_dev) {
  588. dev_notice(&file->port->dev, "%s: invalid device\n", __func__);
  589. ret = -EPIPE;
  590. goto out;
  591. }
  592. if (copy_from_user(&ureq, arg, sizeof ureq)) {
  593. ret = -EFAULT;
  594. goto out;
  595. }
  596. if (ureq.qpn != 0 && ureq.qpn != 1) {
  597. dev_notice(&file->port->dev,
  598. "%s: invalid QPN %u specified\n", __func__,
  599. ureq.qpn);
  600. ret = -EINVAL;
  601. goto out;
  602. }
  603. for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
  604. if (!__get_agent(file, agent_id))
  605. goto found;
  606. dev_notice(&file->port->dev, "%s: Max Agents (%u) reached\n", __func__,
  607. IB_UMAD_MAX_AGENTS);
  608. ret = -ENOMEM;
  609. goto out;
  610. found:
  611. if (ureq.mgmt_class) {
  612. memset(&req, 0, sizeof(req));
  613. req.mgmt_class = ureq.mgmt_class;
  614. req.mgmt_class_version = ureq.mgmt_class_version;
  615. memcpy(req.oui, ureq.oui, sizeof req.oui);
  616. if (compat_method_mask) {
  617. u32 *umm = (u32 *) ureq.method_mask;
  618. int i;
  619. for (i = 0; i < BITS_TO_LONGS(IB_MGMT_MAX_METHODS); ++i)
  620. req.method_mask[i] =
  621. umm[i * 2] | ((u64) umm[i * 2 + 1] << 32);
  622. } else
  623. memcpy(req.method_mask, ureq.method_mask,
  624. sizeof req.method_mask);
  625. }
  626. agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
  627. ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
  628. ureq.mgmt_class ? &req : NULL,
  629. ureq.rmpp_version,
  630. send_handler, recv_handler, file, 0);
  631. if (IS_ERR(agent)) {
  632. ret = PTR_ERR(agent);
  633. agent = NULL;
  634. goto out;
  635. }
  636. if (put_user(agent_id,
  637. (u32 __user *) (arg + offsetof(struct ib_user_mad_reg_req, id)))) {
  638. ret = -EFAULT;
  639. goto out;
  640. }
  641. if (!file->already_used) {
  642. file->already_used = 1;
  643. if (!file->use_pkey_index) {
  644. dev_warn(&file->port->dev,
  645. "process %s did not enable P_Key index support.\n",
  646. current->comm);
  647. dev_warn(&file->port->dev,
  648. " Documentation/infiniband/user_mad.rst has info on the new ABI.\n");
  649. }
  650. }
  651. file->agent[agent_id] = agent;
  652. ret = 0;
  653. out:
  654. mutex_unlock(&file->mutex);
  655. if (ret && agent)
  656. ib_unregister_mad_agent(agent);
  657. mutex_unlock(&file->port->file_mutex);
  658. return ret;
  659. }
  660. static int ib_umad_reg_agent2(struct ib_umad_file *file, void __user *arg)
  661. {
  662. struct ib_user_mad_reg_req2 ureq;
  663. struct ib_mad_reg_req req;
  664. struct ib_mad_agent *agent = NULL;
  665. int agent_id;
  666. int ret;
  667. mutex_lock(&file->port->file_mutex);
  668. mutex_lock(&file->mutex);
  669. if (!file->port->ib_dev) {
  670. dev_notice(&file->port->dev, "%s: invalid device\n", __func__);
  671. ret = -EPIPE;
  672. goto out;
  673. }
  674. if (copy_from_user(&ureq, arg, sizeof(ureq))) {
  675. ret = -EFAULT;
  676. goto out;
  677. }
  678. if (ureq.qpn != 0 && ureq.qpn != 1) {
  679. dev_notice(&file->port->dev, "%s: invalid QPN %u specified\n",
  680. __func__, ureq.qpn);
  681. ret = -EINVAL;
  682. goto out;
  683. }
  684. if (ureq.flags & ~IB_USER_MAD_REG_FLAGS_CAP) {
  685. dev_notice(&file->port->dev,
  686. "%s failed: invalid registration flags specified 0x%x; supported 0x%x\n",
  687. __func__, ureq.flags, IB_USER_MAD_REG_FLAGS_CAP);
  688. ret = -EINVAL;
  689. if (put_user((u32)IB_USER_MAD_REG_FLAGS_CAP,
  690. (u32 __user *) (arg + offsetof(struct
  691. ib_user_mad_reg_req2, flags))))
  692. ret = -EFAULT;
  693. goto out;
  694. }
  695. for (agent_id = 0; agent_id < IB_UMAD_MAX_AGENTS; ++agent_id)
  696. if (!__get_agent(file, agent_id))
  697. goto found;
  698. dev_notice(&file->port->dev, "%s: Max Agents (%u) reached\n", __func__,
  699. IB_UMAD_MAX_AGENTS);
  700. ret = -ENOMEM;
  701. goto out;
  702. found:
  703. if (ureq.mgmt_class) {
  704. memset(&req, 0, sizeof(req));
  705. req.mgmt_class = ureq.mgmt_class;
  706. req.mgmt_class_version = ureq.mgmt_class_version;
  707. if (ureq.oui & 0xff000000) {
  708. dev_notice(&file->port->dev,
  709. "%s failed: oui invalid 0x%08x\n", __func__,
  710. ureq.oui);
  711. ret = -EINVAL;
  712. goto out;
  713. }
  714. req.oui[2] = ureq.oui & 0x0000ff;
  715. req.oui[1] = (ureq.oui & 0x00ff00) >> 8;
  716. req.oui[0] = (ureq.oui & 0xff0000) >> 16;
  717. memcpy(req.method_mask, ureq.method_mask,
  718. sizeof(req.method_mask));
  719. }
  720. agent = ib_register_mad_agent(file->port->ib_dev, file->port->port_num,
  721. ureq.qpn ? IB_QPT_GSI : IB_QPT_SMI,
  722. ureq.mgmt_class ? &req : NULL,
  723. ureq.rmpp_version,
  724. send_handler, recv_handler, file,
  725. ureq.flags);
  726. if (IS_ERR(agent)) {
  727. ret = PTR_ERR(agent);
  728. agent = NULL;
  729. goto out;
  730. }
  731. if (put_user(agent_id,
  732. (u32 __user *)(arg +
  733. offsetof(struct ib_user_mad_reg_req2, id)))) {
  734. ret = -EFAULT;
  735. goto out;
  736. }
  737. if (!file->already_used) {
  738. file->already_used = 1;
  739. file->use_pkey_index = 1;
  740. }
  741. file->agent[agent_id] = agent;
  742. ret = 0;
  743. out:
  744. mutex_unlock(&file->mutex);
  745. if (ret && agent)
  746. ib_unregister_mad_agent(agent);
  747. mutex_unlock(&file->port->file_mutex);
  748. return ret;
  749. }
  750. static int ib_umad_unreg_agent(struct ib_umad_file *file, u32 __user *arg)
  751. {
  752. struct ib_mad_agent *agent = NULL;
  753. u32 id;
  754. int ret = 0;
  755. if (get_user(id, arg))
  756. return -EFAULT;
  757. if (id >= IB_UMAD_MAX_AGENTS)
  758. return -EINVAL;
  759. mutex_lock(&file->port->file_mutex);
  760. mutex_lock(&file->mutex);
  761. id = array_index_nospec(id, IB_UMAD_MAX_AGENTS);
  762. if (!__get_agent(file, id)) {
  763. ret = -EINVAL;
  764. goto out;
  765. }
  766. agent = file->agent[id];
  767. file->agent[id] = NULL;
  768. out:
  769. mutex_unlock(&file->mutex);
  770. if (agent)
  771. ib_unregister_mad_agent(agent);
  772. mutex_unlock(&file->port->file_mutex);
  773. return ret;
  774. }
  775. static long ib_umad_enable_pkey(struct ib_umad_file *file)
  776. {
  777. int ret = 0;
  778. mutex_lock(&file->mutex);
  779. if (file->already_used)
  780. ret = -EINVAL;
  781. else
  782. file->use_pkey_index = 1;
  783. mutex_unlock(&file->mutex);
  784. return ret;
  785. }
  786. static long ib_umad_ioctl(struct file *filp, unsigned int cmd,
  787. unsigned long arg)
  788. {
  789. switch (cmd) {
  790. case IB_USER_MAD_REGISTER_AGENT:
  791. return ib_umad_reg_agent(filp->private_data, (void __user *) arg, 0);
  792. case IB_USER_MAD_UNREGISTER_AGENT:
  793. return ib_umad_unreg_agent(filp->private_data, (__u32 __user *) arg);
  794. case IB_USER_MAD_ENABLE_PKEY:
  795. return ib_umad_enable_pkey(filp->private_data);
  796. case IB_USER_MAD_REGISTER_AGENT2:
  797. return ib_umad_reg_agent2(filp->private_data, (void __user *) arg);
  798. default:
  799. return -ENOIOCTLCMD;
  800. }
  801. }
  802. #ifdef CONFIG_COMPAT
  803. static long ib_umad_compat_ioctl(struct file *filp, unsigned int cmd,
  804. unsigned long arg)
  805. {
  806. switch (cmd) {
  807. case IB_USER_MAD_REGISTER_AGENT:
  808. return ib_umad_reg_agent(filp->private_data, compat_ptr(arg), 1);
  809. case IB_USER_MAD_UNREGISTER_AGENT:
  810. return ib_umad_unreg_agent(filp->private_data, compat_ptr(arg));
  811. case IB_USER_MAD_ENABLE_PKEY:
  812. return ib_umad_enable_pkey(filp->private_data);
  813. case IB_USER_MAD_REGISTER_AGENT2:
  814. return ib_umad_reg_agent2(filp->private_data, compat_ptr(arg));
  815. default:
  816. return -ENOIOCTLCMD;
  817. }
  818. }
  819. #endif
  820. /*
  821. * ib_umad_open() does not need the BKL:
  822. *
  823. * - the ib_umad_port structures are properly reference counted, and
  824. * everything else is purely local to the file being created, so
  825. * races against other open calls are not a problem;
  826. * - the ioctl method does not affect any global state outside of the
  827. * file structure being operated on;
  828. */
  829. static int ib_umad_open(struct inode *inode, struct file *filp)
  830. {
  831. struct ib_umad_port *port;
  832. struct ib_umad_file *file;
  833. int ret = 0;
  834. port = container_of(inode->i_cdev, struct ib_umad_port, cdev);
  835. mutex_lock(&port->file_mutex);
  836. if (!port->ib_dev) {
  837. ret = -ENXIO;
  838. goto out;
  839. }
  840. if (!rdma_dev_access_netns(port->ib_dev, current->nsproxy->net_ns)) {
  841. ret = -EPERM;
  842. goto out;
  843. }
  844. file = kzalloc(sizeof(*file), GFP_KERNEL);
  845. if (!file) {
  846. ret = -ENOMEM;
  847. goto out;
  848. }
  849. mutex_init(&file->mutex);
  850. spin_lock_init(&file->send_lock);
  851. INIT_LIST_HEAD(&file->recv_list);
  852. INIT_LIST_HEAD(&file->send_list);
  853. init_waitqueue_head(&file->recv_wait);
  854. file->port = port;
  855. filp->private_data = file;
  856. list_add_tail(&file->port_list, &port->file_list);
  857. stream_open(inode, filp);
  858. out:
  859. mutex_unlock(&port->file_mutex);
  860. return ret;
  861. }
  862. static int ib_umad_close(struct inode *inode, struct file *filp)
  863. {
  864. struct ib_umad_file *file = filp->private_data;
  865. struct ib_umad_packet *packet, *tmp;
  866. int already_dead;
  867. int i;
  868. mutex_lock(&file->port->file_mutex);
  869. mutex_lock(&file->mutex);
  870. already_dead = file->agents_dead;
  871. file->agents_dead = 1;
  872. list_for_each_entry_safe(packet, tmp, &file->recv_list, list) {
  873. if (packet->recv_wc)
  874. ib_free_recv_mad(packet->recv_wc);
  875. kfree(packet);
  876. }
  877. list_del(&file->port_list);
  878. mutex_unlock(&file->mutex);
  879. if (!already_dead)
  880. for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
  881. if (file->agent[i])
  882. ib_unregister_mad_agent(file->agent[i]);
  883. mutex_unlock(&file->port->file_mutex);
  884. mutex_destroy(&file->mutex);
  885. kfree(file);
  886. return 0;
  887. }
  888. static const struct file_operations umad_fops = {
  889. .owner = THIS_MODULE,
  890. .read = ib_umad_read,
  891. .write = ib_umad_write,
  892. .poll = ib_umad_poll,
  893. .unlocked_ioctl = ib_umad_ioctl,
  894. #ifdef CONFIG_COMPAT
  895. .compat_ioctl = ib_umad_compat_ioctl,
  896. #endif
  897. .open = ib_umad_open,
  898. .release = ib_umad_close,
  899. };
  900. static int ib_umad_sm_open(struct inode *inode, struct file *filp)
  901. {
  902. struct ib_umad_port *port;
  903. struct ib_port_modify props = {
  904. .set_port_cap_mask = IB_PORT_SM
  905. };
  906. int ret;
  907. port = container_of(inode->i_cdev, struct ib_umad_port, sm_cdev);
  908. if (filp->f_flags & O_NONBLOCK) {
  909. if (down_trylock(&port->sm_sem)) {
  910. ret = -EAGAIN;
  911. goto fail;
  912. }
  913. } else {
  914. if (down_interruptible(&port->sm_sem)) {
  915. ret = -ERESTARTSYS;
  916. goto fail;
  917. }
  918. }
  919. if (!rdma_dev_access_netns(port->ib_dev, current->nsproxy->net_ns)) {
  920. ret = -EPERM;
  921. goto err_up_sem;
  922. }
  923. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  924. if (ret)
  925. goto err_up_sem;
  926. filp->private_data = port;
  927. nonseekable_open(inode, filp);
  928. return 0;
  929. err_up_sem:
  930. up(&port->sm_sem);
  931. fail:
  932. return ret;
  933. }
  934. static int ib_umad_sm_close(struct inode *inode, struct file *filp)
  935. {
  936. struct ib_umad_port *port = filp->private_data;
  937. struct ib_port_modify props = {
  938. .clr_port_cap_mask = IB_PORT_SM
  939. };
  940. int ret = 0;
  941. mutex_lock(&port->file_mutex);
  942. if (port->ib_dev)
  943. ret = ib_modify_port(port->ib_dev, port->port_num, 0, &props);
  944. mutex_unlock(&port->file_mutex);
  945. up(&port->sm_sem);
  946. return ret;
  947. }
  948. static const struct file_operations umad_sm_fops = {
  949. .owner = THIS_MODULE,
  950. .open = ib_umad_sm_open,
  951. .release = ib_umad_sm_close,
  952. };
  953. static struct ib_umad_port *get_port(struct ib_device *ibdev,
  954. struct ib_umad_device *umad_dev,
  955. u32 port)
  956. {
  957. if (!umad_dev)
  958. return ERR_PTR(-EOPNOTSUPP);
  959. if (!rdma_is_port_valid(ibdev, port))
  960. return ERR_PTR(-EINVAL);
  961. if (!rdma_cap_ib_mad(ibdev, port))
  962. return ERR_PTR(-EOPNOTSUPP);
  963. return &umad_dev->ports[port - rdma_start_port(ibdev)];
  964. }
  965. static int ib_umad_get_nl_info(struct ib_device *ibdev, void *client_data,
  966. struct ib_client_nl_info *res)
  967. {
  968. struct ib_umad_port *port = get_port(ibdev, client_data, res->port);
  969. if (IS_ERR(port))
  970. return PTR_ERR(port);
  971. res->abi = IB_USER_MAD_ABI_VERSION;
  972. res->cdev = &port->dev;
  973. return 0;
  974. }
  975. static struct ib_client umad_client = {
  976. .name = "umad",
  977. .add = ib_umad_add_one,
  978. .remove = ib_umad_remove_one,
  979. .get_nl_info = ib_umad_get_nl_info,
  980. };
  981. MODULE_ALIAS_RDMA_CLIENT("umad");
  982. static int ib_issm_get_nl_info(struct ib_device *ibdev, void *client_data,
  983. struct ib_client_nl_info *res)
  984. {
  985. struct ib_umad_port *port = get_port(ibdev, client_data, res->port);
  986. if (IS_ERR(port))
  987. return PTR_ERR(port);
  988. res->abi = IB_USER_MAD_ABI_VERSION;
  989. res->cdev = &port->sm_dev;
  990. return 0;
  991. }
  992. static struct ib_client issm_client = {
  993. .name = "issm",
  994. .get_nl_info = ib_issm_get_nl_info,
  995. };
  996. MODULE_ALIAS_RDMA_CLIENT("issm");
  997. static ssize_t ibdev_show(struct device *dev, struct device_attribute *attr,
  998. char *buf)
  999. {
  1000. struct ib_umad_port *port = dev_get_drvdata(dev);
  1001. if (!port)
  1002. return -ENODEV;
  1003. return sysfs_emit(buf, "%s\n", dev_name(&port->ib_dev->dev));
  1004. }
  1005. static DEVICE_ATTR_RO(ibdev);
  1006. static ssize_t port_show(struct device *dev, struct device_attribute *attr,
  1007. char *buf)
  1008. {
  1009. struct ib_umad_port *port = dev_get_drvdata(dev);
  1010. if (!port)
  1011. return -ENODEV;
  1012. return sysfs_emit(buf, "%d\n", port->port_num);
  1013. }
  1014. static DEVICE_ATTR_RO(port);
  1015. static struct attribute *umad_class_dev_attrs[] = {
  1016. &dev_attr_ibdev.attr,
  1017. &dev_attr_port.attr,
  1018. NULL,
  1019. };
  1020. ATTRIBUTE_GROUPS(umad_class_dev);
  1021. static char *umad_devnode(const struct device *dev, umode_t *mode)
  1022. {
  1023. return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
  1024. }
  1025. static ssize_t abi_version_show(const struct class *class,
  1026. const struct class_attribute *attr, char *buf)
  1027. {
  1028. return sysfs_emit(buf, "%d\n", IB_USER_MAD_ABI_VERSION);
  1029. }
  1030. static CLASS_ATTR_RO(abi_version);
  1031. static struct attribute *umad_class_attrs[] = {
  1032. &class_attr_abi_version.attr,
  1033. NULL,
  1034. };
  1035. ATTRIBUTE_GROUPS(umad_class);
  1036. static struct class umad_class = {
  1037. .name = "infiniband_mad",
  1038. .devnode = umad_devnode,
  1039. .class_groups = umad_class_groups,
  1040. .dev_groups = umad_class_dev_groups,
  1041. };
  1042. static void ib_umad_release_port(struct device *device)
  1043. {
  1044. struct ib_umad_port *port = dev_get_drvdata(device);
  1045. struct ib_umad_device *umad_dev = port->umad_dev;
  1046. ib_umad_dev_put(umad_dev);
  1047. }
  1048. static void ib_umad_init_port_dev(struct device *dev,
  1049. struct ib_umad_port *port,
  1050. const struct ib_device *device)
  1051. {
  1052. device_initialize(dev);
  1053. ib_umad_dev_get(port->umad_dev);
  1054. dev->class = &umad_class;
  1055. dev->parent = device->dev.parent;
  1056. dev_set_drvdata(dev, port);
  1057. dev->release = ib_umad_release_port;
  1058. }
  1059. static int ib_umad_init_port(struct ib_device *device, int port_num,
  1060. struct ib_umad_device *umad_dev,
  1061. struct ib_umad_port *port)
  1062. {
  1063. int devnum;
  1064. dev_t base_umad;
  1065. dev_t base_issm;
  1066. int ret;
  1067. devnum = ida_alloc_max(&umad_ida, IB_UMAD_MAX_PORTS - 1, GFP_KERNEL);
  1068. if (devnum < 0)
  1069. return -1;
  1070. port->dev_num = devnum;
  1071. if (devnum >= IB_UMAD_NUM_FIXED_MINOR) {
  1072. base_umad = dynamic_umad_dev + devnum - IB_UMAD_NUM_FIXED_MINOR;
  1073. base_issm = dynamic_issm_dev + devnum - IB_UMAD_NUM_FIXED_MINOR;
  1074. } else {
  1075. base_umad = devnum + base_umad_dev;
  1076. base_issm = devnum + base_issm_dev;
  1077. }
  1078. port->ib_dev = device;
  1079. port->umad_dev = umad_dev;
  1080. port->port_num = port_num;
  1081. sema_init(&port->sm_sem, 1);
  1082. mutex_init(&port->file_mutex);
  1083. INIT_LIST_HEAD(&port->file_list);
  1084. ib_umad_init_port_dev(&port->dev, port, device);
  1085. port->dev.devt = base_umad;
  1086. dev_set_name(&port->dev, "umad%d", port->dev_num);
  1087. cdev_init(&port->cdev, &umad_fops);
  1088. port->cdev.owner = THIS_MODULE;
  1089. ret = cdev_device_add(&port->cdev, &port->dev);
  1090. if (ret)
  1091. goto err_cdev;
  1092. if (rdma_cap_ib_smi(device, port_num)) {
  1093. ib_umad_init_port_dev(&port->sm_dev, port, device);
  1094. port->sm_dev.devt = base_issm;
  1095. dev_set_name(&port->sm_dev, "issm%d", port->dev_num);
  1096. cdev_init(&port->sm_cdev, &umad_sm_fops);
  1097. port->sm_cdev.owner = THIS_MODULE;
  1098. ret = cdev_device_add(&port->sm_cdev, &port->sm_dev);
  1099. if (ret)
  1100. goto err_dev;
  1101. }
  1102. return 0;
  1103. err_dev:
  1104. put_device(&port->sm_dev);
  1105. cdev_device_del(&port->cdev, &port->dev);
  1106. err_cdev:
  1107. put_device(&port->dev);
  1108. ida_free(&umad_ida, devnum);
  1109. return ret;
  1110. }
  1111. static void ib_umad_kill_port(struct ib_umad_port *port)
  1112. {
  1113. struct ib_umad_file *file;
  1114. bool has_smi = false;
  1115. int id;
  1116. if (rdma_cap_ib_smi(port->ib_dev, port->port_num)) {
  1117. cdev_device_del(&port->sm_cdev, &port->sm_dev);
  1118. has_smi = true;
  1119. }
  1120. cdev_device_del(&port->cdev, &port->dev);
  1121. mutex_lock(&port->file_mutex);
  1122. /* Mark ib_dev NULL and block ioctl or other file ops to progress
  1123. * further.
  1124. */
  1125. port->ib_dev = NULL;
  1126. list_for_each_entry(file, &port->file_list, port_list) {
  1127. mutex_lock(&file->mutex);
  1128. file->agents_dead = 1;
  1129. wake_up_interruptible(&file->recv_wait);
  1130. mutex_unlock(&file->mutex);
  1131. for (id = 0; id < IB_UMAD_MAX_AGENTS; ++id)
  1132. if (file->agent[id])
  1133. ib_unregister_mad_agent(file->agent[id]);
  1134. }
  1135. mutex_unlock(&port->file_mutex);
  1136. ida_free(&umad_ida, port->dev_num);
  1137. /* balances device_initialize() */
  1138. if (has_smi)
  1139. put_device(&port->sm_dev);
  1140. put_device(&port->dev);
  1141. }
  1142. static int ib_umad_add_one(struct ib_device *device)
  1143. {
  1144. struct ib_umad_device *umad_dev;
  1145. int s, e, i;
  1146. int count = 0;
  1147. int ret;
  1148. s = rdma_start_port(device);
  1149. e = rdma_end_port(device);
  1150. umad_dev = kzalloc(struct_size(umad_dev, ports,
  1151. size_add(size_sub(e, s), 1)),
  1152. GFP_KERNEL);
  1153. if (!umad_dev)
  1154. return -ENOMEM;
  1155. kref_init(&umad_dev->kref);
  1156. for (i = s; i <= e; ++i) {
  1157. if (!rdma_cap_ib_mad(device, i))
  1158. continue;
  1159. ret = ib_umad_init_port(device, i, umad_dev,
  1160. &umad_dev->ports[i - s]);
  1161. if (ret)
  1162. goto err;
  1163. count++;
  1164. }
  1165. if (!count) {
  1166. ret = -EOPNOTSUPP;
  1167. goto free;
  1168. }
  1169. ib_set_client_data(device, &umad_client, umad_dev);
  1170. return 0;
  1171. err:
  1172. while (--i >= s) {
  1173. if (!rdma_cap_ib_mad(device, i))
  1174. continue;
  1175. ib_umad_kill_port(&umad_dev->ports[i - s]);
  1176. }
  1177. free:
  1178. /* balances kref_init */
  1179. ib_umad_dev_put(umad_dev);
  1180. return ret;
  1181. }
  1182. static void ib_umad_remove_one(struct ib_device *device, void *client_data)
  1183. {
  1184. struct ib_umad_device *umad_dev = client_data;
  1185. unsigned int i;
  1186. rdma_for_each_port (device, i) {
  1187. if (rdma_cap_ib_mad(device, i))
  1188. ib_umad_kill_port(
  1189. &umad_dev->ports[i - rdma_start_port(device)]);
  1190. }
  1191. /* balances kref_init() */
  1192. ib_umad_dev_put(umad_dev);
  1193. }
  1194. static int __init ib_umad_init(void)
  1195. {
  1196. int ret;
  1197. ret = register_chrdev_region(base_umad_dev,
  1198. IB_UMAD_NUM_FIXED_MINOR * 2,
  1199. umad_class.name);
  1200. if (ret) {
  1201. pr_err("couldn't register device number\n");
  1202. goto out;
  1203. }
  1204. ret = alloc_chrdev_region(&dynamic_umad_dev, 0,
  1205. IB_UMAD_NUM_DYNAMIC_MINOR * 2,
  1206. umad_class.name);
  1207. if (ret) {
  1208. pr_err("couldn't register dynamic device number\n");
  1209. goto out_alloc;
  1210. }
  1211. dynamic_issm_dev = dynamic_umad_dev + IB_UMAD_NUM_DYNAMIC_MINOR;
  1212. ret = class_register(&umad_class);
  1213. if (ret) {
  1214. pr_err("couldn't create class infiniband_mad\n");
  1215. goto out_chrdev;
  1216. }
  1217. ret = ib_register_client(&umad_client);
  1218. if (ret)
  1219. goto out_class;
  1220. ret = ib_register_client(&issm_client);
  1221. if (ret)
  1222. goto out_client;
  1223. return 0;
  1224. out_client:
  1225. ib_unregister_client(&umad_client);
  1226. out_class:
  1227. class_unregister(&umad_class);
  1228. out_chrdev:
  1229. unregister_chrdev_region(dynamic_umad_dev,
  1230. IB_UMAD_NUM_DYNAMIC_MINOR * 2);
  1231. out_alloc:
  1232. unregister_chrdev_region(base_umad_dev,
  1233. IB_UMAD_NUM_FIXED_MINOR * 2);
  1234. out:
  1235. return ret;
  1236. }
  1237. static void __exit ib_umad_cleanup(void)
  1238. {
  1239. ib_unregister_client(&issm_client);
  1240. ib_unregister_client(&umad_client);
  1241. class_unregister(&umad_class);
  1242. unregister_chrdev_region(base_umad_dev,
  1243. IB_UMAD_NUM_FIXED_MINOR * 2);
  1244. unregister_chrdev_region(dynamic_umad_dev,
  1245. IB_UMAD_NUM_DYNAMIC_MINOR * 2);
  1246. }
  1247. module_init(ib_umad_init);
  1248. module_exit(ib_umad_cleanup);