user_mad.c 34 KB

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