user_mad.c 34 KB

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