user_mad.c 34 KB

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