user_mad.c 34 KB

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