user_mad.c 34 KB

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