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