sa_query.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
  4. * Copyright (c) 2006 Intel Corporation. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/err.h>
  37. #include <linux/random.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/slab.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/kref.h>
  42. #include <linux/idr.h>
  43. #include <linux/workqueue.h>
  44. #include <rdma/ib_pack.h>
  45. #include <rdma/ib_cache.h>
  46. #include "sa.h"
  47. MODULE_AUTHOR("Roland Dreier");
  48. MODULE_DESCRIPTION("InfiniBand subnet administration query support");
  49. MODULE_LICENSE("Dual BSD/GPL");
  50. struct ib_sa_sm_ah {
  51. struct ib_ah *ah;
  52. struct kref ref;
  53. u16 pkey_index;
  54. u8 src_path_mask;
  55. };
  56. struct ib_sa_port {
  57. struct ib_mad_agent *agent;
  58. struct ib_sa_sm_ah *sm_ah;
  59. struct work_struct update_task;
  60. spinlock_t ah_lock;
  61. u8 port_num;
  62. };
  63. struct ib_sa_device {
  64. int start_port, end_port;
  65. struct ib_event_handler event_handler;
  66. struct ib_sa_port port[0];
  67. };
  68. struct ib_sa_query {
  69. void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
  70. void (*release)(struct ib_sa_query *);
  71. struct ib_sa_client *client;
  72. struct ib_sa_port *port;
  73. struct ib_mad_send_buf *mad_buf;
  74. struct ib_sa_sm_ah *sm_ah;
  75. int id;
  76. };
  77. struct ib_sa_service_query {
  78. void (*callback)(int, struct ib_sa_service_rec *, void *);
  79. void *context;
  80. struct ib_sa_query sa_query;
  81. };
  82. struct ib_sa_path_query {
  83. void (*callback)(int, struct ib_sa_path_rec *, void *);
  84. void *context;
  85. struct ib_sa_query sa_query;
  86. };
  87. struct ib_sa_guidinfo_query {
  88. void (*callback)(int, struct ib_sa_guidinfo_rec *, void *);
  89. void *context;
  90. struct ib_sa_query sa_query;
  91. };
  92. struct ib_sa_mcmember_query {
  93. void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
  94. void *context;
  95. struct ib_sa_query sa_query;
  96. };
  97. static void ib_sa_add_one(struct ib_device *device);
  98. static void ib_sa_remove_one(struct ib_device *device);
  99. static struct ib_client sa_client = {
  100. .name = "sa",
  101. .add = ib_sa_add_one,
  102. .remove = ib_sa_remove_one
  103. };
  104. static DEFINE_SPINLOCK(idr_lock);
  105. static DEFINE_IDR(query_idr);
  106. static DEFINE_SPINLOCK(tid_lock);
  107. static u32 tid;
  108. #define PATH_REC_FIELD(field) \
  109. .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
  110. .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
  111. .field_name = "sa_path_rec:" #field
  112. static const struct ib_field path_rec_table[] = {
  113. { PATH_REC_FIELD(service_id),
  114. .offset_words = 0,
  115. .offset_bits = 0,
  116. .size_bits = 64 },
  117. { PATH_REC_FIELD(dgid),
  118. .offset_words = 2,
  119. .offset_bits = 0,
  120. .size_bits = 128 },
  121. { PATH_REC_FIELD(sgid),
  122. .offset_words = 6,
  123. .offset_bits = 0,
  124. .size_bits = 128 },
  125. { PATH_REC_FIELD(dlid),
  126. .offset_words = 10,
  127. .offset_bits = 0,
  128. .size_bits = 16 },
  129. { PATH_REC_FIELD(slid),
  130. .offset_words = 10,
  131. .offset_bits = 16,
  132. .size_bits = 16 },
  133. { PATH_REC_FIELD(raw_traffic),
  134. .offset_words = 11,
  135. .offset_bits = 0,
  136. .size_bits = 1 },
  137. { RESERVED,
  138. .offset_words = 11,
  139. .offset_bits = 1,
  140. .size_bits = 3 },
  141. { PATH_REC_FIELD(flow_label),
  142. .offset_words = 11,
  143. .offset_bits = 4,
  144. .size_bits = 20 },
  145. { PATH_REC_FIELD(hop_limit),
  146. .offset_words = 11,
  147. .offset_bits = 24,
  148. .size_bits = 8 },
  149. { PATH_REC_FIELD(traffic_class),
  150. .offset_words = 12,
  151. .offset_bits = 0,
  152. .size_bits = 8 },
  153. { PATH_REC_FIELD(reversible),
  154. .offset_words = 12,
  155. .offset_bits = 8,
  156. .size_bits = 1 },
  157. { PATH_REC_FIELD(numb_path),
  158. .offset_words = 12,
  159. .offset_bits = 9,
  160. .size_bits = 7 },
  161. { PATH_REC_FIELD(pkey),
  162. .offset_words = 12,
  163. .offset_bits = 16,
  164. .size_bits = 16 },
  165. { PATH_REC_FIELD(qos_class),
  166. .offset_words = 13,
  167. .offset_bits = 0,
  168. .size_bits = 12 },
  169. { PATH_REC_FIELD(sl),
  170. .offset_words = 13,
  171. .offset_bits = 12,
  172. .size_bits = 4 },
  173. { PATH_REC_FIELD(mtu_selector),
  174. .offset_words = 13,
  175. .offset_bits = 16,
  176. .size_bits = 2 },
  177. { PATH_REC_FIELD(mtu),
  178. .offset_words = 13,
  179. .offset_bits = 18,
  180. .size_bits = 6 },
  181. { PATH_REC_FIELD(rate_selector),
  182. .offset_words = 13,
  183. .offset_bits = 24,
  184. .size_bits = 2 },
  185. { PATH_REC_FIELD(rate),
  186. .offset_words = 13,
  187. .offset_bits = 26,
  188. .size_bits = 6 },
  189. { PATH_REC_FIELD(packet_life_time_selector),
  190. .offset_words = 14,
  191. .offset_bits = 0,
  192. .size_bits = 2 },
  193. { PATH_REC_FIELD(packet_life_time),
  194. .offset_words = 14,
  195. .offset_bits = 2,
  196. .size_bits = 6 },
  197. { PATH_REC_FIELD(preference),
  198. .offset_words = 14,
  199. .offset_bits = 8,
  200. .size_bits = 8 },
  201. { RESERVED,
  202. .offset_words = 14,
  203. .offset_bits = 16,
  204. .size_bits = 48 },
  205. };
  206. #define MCMEMBER_REC_FIELD(field) \
  207. .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
  208. .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
  209. .field_name = "sa_mcmember_rec:" #field
  210. static const struct ib_field mcmember_rec_table[] = {
  211. { MCMEMBER_REC_FIELD(mgid),
  212. .offset_words = 0,
  213. .offset_bits = 0,
  214. .size_bits = 128 },
  215. { MCMEMBER_REC_FIELD(port_gid),
  216. .offset_words = 4,
  217. .offset_bits = 0,
  218. .size_bits = 128 },
  219. { MCMEMBER_REC_FIELD(qkey),
  220. .offset_words = 8,
  221. .offset_bits = 0,
  222. .size_bits = 32 },
  223. { MCMEMBER_REC_FIELD(mlid),
  224. .offset_words = 9,
  225. .offset_bits = 0,
  226. .size_bits = 16 },
  227. { MCMEMBER_REC_FIELD(mtu_selector),
  228. .offset_words = 9,
  229. .offset_bits = 16,
  230. .size_bits = 2 },
  231. { MCMEMBER_REC_FIELD(mtu),
  232. .offset_words = 9,
  233. .offset_bits = 18,
  234. .size_bits = 6 },
  235. { MCMEMBER_REC_FIELD(traffic_class),
  236. .offset_words = 9,
  237. .offset_bits = 24,
  238. .size_bits = 8 },
  239. { MCMEMBER_REC_FIELD(pkey),
  240. .offset_words = 10,
  241. .offset_bits = 0,
  242. .size_bits = 16 },
  243. { MCMEMBER_REC_FIELD(rate_selector),
  244. .offset_words = 10,
  245. .offset_bits = 16,
  246. .size_bits = 2 },
  247. { MCMEMBER_REC_FIELD(rate),
  248. .offset_words = 10,
  249. .offset_bits = 18,
  250. .size_bits = 6 },
  251. { MCMEMBER_REC_FIELD(packet_life_time_selector),
  252. .offset_words = 10,
  253. .offset_bits = 24,
  254. .size_bits = 2 },
  255. { MCMEMBER_REC_FIELD(packet_life_time),
  256. .offset_words = 10,
  257. .offset_bits = 26,
  258. .size_bits = 6 },
  259. { MCMEMBER_REC_FIELD(sl),
  260. .offset_words = 11,
  261. .offset_bits = 0,
  262. .size_bits = 4 },
  263. { MCMEMBER_REC_FIELD(flow_label),
  264. .offset_words = 11,
  265. .offset_bits = 4,
  266. .size_bits = 20 },
  267. { MCMEMBER_REC_FIELD(hop_limit),
  268. .offset_words = 11,
  269. .offset_bits = 24,
  270. .size_bits = 8 },
  271. { MCMEMBER_REC_FIELD(scope),
  272. .offset_words = 12,
  273. .offset_bits = 0,
  274. .size_bits = 4 },
  275. { MCMEMBER_REC_FIELD(join_state),
  276. .offset_words = 12,
  277. .offset_bits = 4,
  278. .size_bits = 4 },
  279. { MCMEMBER_REC_FIELD(proxy_join),
  280. .offset_words = 12,
  281. .offset_bits = 8,
  282. .size_bits = 1 },
  283. { RESERVED,
  284. .offset_words = 12,
  285. .offset_bits = 9,
  286. .size_bits = 23 },
  287. };
  288. #define SERVICE_REC_FIELD(field) \
  289. .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
  290. .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
  291. .field_name = "sa_service_rec:" #field
  292. static const struct ib_field service_rec_table[] = {
  293. { SERVICE_REC_FIELD(id),
  294. .offset_words = 0,
  295. .offset_bits = 0,
  296. .size_bits = 64 },
  297. { SERVICE_REC_FIELD(gid),
  298. .offset_words = 2,
  299. .offset_bits = 0,
  300. .size_bits = 128 },
  301. { SERVICE_REC_FIELD(pkey),
  302. .offset_words = 6,
  303. .offset_bits = 0,
  304. .size_bits = 16 },
  305. { SERVICE_REC_FIELD(lease),
  306. .offset_words = 7,
  307. .offset_bits = 0,
  308. .size_bits = 32 },
  309. { SERVICE_REC_FIELD(key),
  310. .offset_words = 8,
  311. .offset_bits = 0,
  312. .size_bits = 128 },
  313. { SERVICE_REC_FIELD(name),
  314. .offset_words = 12,
  315. .offset_bits = 0,
  316. .size_bits = 64*8 },
  317. { SERVICE_REC_FIELD(data8),
  318. .offset_words = 28,
  319. .offset_bits = 0,
  320. .size_bits = 16*8 },
  321. { SERVICE_REC_FIELD(data16),
  322. .offset_words = 32,
  323. .offset_bits = 0,
  324. .size_bits = 8*16 },
  325. { SERVICE_REC_FIELD(data32),
  326. .offset_words = 36,
  327. .offset_bits = 0,
  328. .size_bits = 4*32 },
  329. { SERVICE_REC_FIELD(data64),
  330. .offset_words = 40,
  331. .offset_bits = 0,
  332. .size_bits = 2*64 },
  333. };
  334. #define GUIDINFO_REC_FIELD(field) \
  335. .struct_offset_bytes = offsetof(struct ib_sa_guidinfo_rec, field), \
  336. .struct_size_bytes = sizeof((struct ib_sa_guidinfo_rec *) 0)->field, \
  337. .field_name = "sa_guidinfo_rec:" #field
  338. static const struct ib_field guidinfo_rec_table[] = {
  339. { GUIDINFO_REC_FIELD(lid),
  340. .offset_words = 0,
  341. .offset_bits = 0,
  342. .size_bits = 16 },
  343. { GUIDINFO_REC_FIELD(block_num),
  344. .offset_words = 0,
  345. .offset_bits = 16,
  346. .size_bits = 8 },
  347. { GUIDINFO_REC_FIELD(res1),
  348. .offset_words = 0,
  349. .offset_bits = 24,
  350. .size_bits = 8 },
  351. { GUIDINFO_REC_FIELD(res2),
  352. .offset_words = 1,
  353. .offset_bits = 0,
  354. .size_bits = 32 },
  355. { GUIDINFO_REC_FIELD(guid_info_list),
  356. .offset_words = 2,
  357. .offset_bits = 0,
  358. .size_bits = 512 },
  359. };
  360. static void free_sm_ah(struct kref *kref)
  361. {
  362. struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
  363. ib_destroy_ah(sm_ah->ah);
  364. kfree(sm_ah);
  365. }
  366. static void update_sm_ah(struct work_struct *work)
  367. {
  368. struct ib_sa_port *port =
  369. container_of(work, struct ib_sa_port, update_task);
  370. struct ib_sa_sm_ah *new_ah;
  371. struct ib_port_attr port_attr;
  372. struct ib_ah_attr ah_attr;
  373. if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
  374. printk(KERN_WARNING "Couldn't query port\n");
  375. return;
  376. }
  377. new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
  378. if (!new_ah) {
  379. printk(KERN_WARNING "Couldn't allocate new SM AH\n");
  380. return;
  381. }
  382. kref_init(&new_ah->ref);
  383. new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
  384. new_ah->pkey_index = 0;
  385. if (ib_find_pkey(port->agent->device, port->port_num,
  386. IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
  387. printk(KERN_ERR "Couldn't find index for default PKey\n");
  388. memset(&ah_attr, 0, sizeof ah_attr);
  389. ah_attr.dlid = port_attr.sm_lid;
  390. ah_attr.sl = port_attr.sm_sl;
  391. ah_attr.port_num = port->port_num;
  392. new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
  393. if (IS_ERR(new_ah->ah)) {
  394. printk(KERN_WARNING "Couldn't create new SM AH\n");
  395. kfree(new_ah);
  396. return;
  397. }
  398. spin_lock_irq(&port->ah_lock);
  399. if (port->sm_ah)
  400. kref_put(&port->sm_ah->ref, free_sm_ah);
  401. port->sm_ah = new_ah;
  402. spin_unlock_irq(&port->ah_lock);
  403. }
  404. static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
  405. {
  406. if (event->event == IB_EVENT_PORT_ERR ||
  407. event->event == IB_EVENT_PORT_ACTIVE ||
  408. event->event == IB_EVENT_LID_CHANGE ||
  409. event->event == IB_EVENT_PKEY_CHANGE ||
  410. event->event == IB_EVENT_SM_CHANGE ||
  411. event->event == IB_EVENT_CLIENT_REREGISTER) {
  412. unsigned long flags;
  413. struct ib_sa_device *sa_dev =
  414. container_of(handler, typeof(*sa_dev), event_handler);
  415. struct ib_sa_port *port =
  416. &sa_dev->port[event->element.port_num - sa_dev->start_port];
  417. if (rdma_port_get_link_layer(handler->device, port->port_num) != IB_LINK_LAYER_INFINIBAND)
  418. return;
  419. spin_lock_irqsave(&port->ah_lock, flags);
  420. if (port->sm_ah)
  421. kref_put(&port->sm_ah->ref, free_sm_ah);
  422. port->sm_ah = NULL;
  423. spin_unlock_irqrestore(&port->ah_lock, flags);
  424. queue_work(ib_wq, &sa_dev->port[event->element.port_num -
  425. sa_dev->start_port].update_task);
  426. }
  427. }
  428. void ib_sa_register_client(struct ib_sa_client *client)
  429. {
  430. atomic_set(&client->users, 1);
  431. init_completion(&client->comp);
  432. }
  433. EXPORT_SYMBOL(ib_sa_register_client);
  434. void ib_sa_unregister_client(struct ib_sa_client *client)
  435. {
  436. ib_sa_client_put(client);
  437. wait_for_completion(&client->comp);
  438. }
  439. EXPORT_SYMBOL(ib_sa_unregister_client);
  440. /**
  441. * ib_sa_cancel_query - try to cancel an SA query
  442. * @id:ID of query to cancel
  443. * @query:query pointer to cancel
  444. *
  445. * Try to cancel an SA query. If the id and query don't match up or
  446. * the query has already completed, nothing is done. Otherwise the
  447. * query is canceled and will complete with a status of -EINTR.
  448. */
  449. void ib_sa_cancel_query(int id, struct ib_sa_query *query)
  450. {
  451. unsigned long flags;
  452. struct ib_mad_agent *agent;
  453. struct ib_mad_send_buf *mad_buf;
  454. spin_lock_irqsave(&idr_lock, flags);
  455. if (idr_find(&query_idr, id) != query) {
  456. spin_unlock_irqrestore(&idr_lock, flags);
  457. return;
  458. }
  459. agent = query->port->agent;
  460. mad_buf = query->mad_buf;
  461. spin_unlock_irqrestore(&idr_lock, flags);
  462. ib_cancel_mad(agent, mad_buf);
  463. }
  464. EXPORT_SYMBOL(ib_sa_cancel_query);
  465. static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
  466. {
  467. struct ib_sa_device *sa_dev;
  468. struct ib_sa_port *port;
  469. unsigned long flags;
  470. u8 src_path_mask;
  471. sa_dev = ib_get_client_data(device, &sa_client);
  472. if (!sa_dev)
  473. return 0x7f;
  474. port = &sa_dev->port[port_num - sa_dev->start_port];
  475. spin_lock_irqsave(&port->ah_lock, flags);
  476. src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
  477. spin_unlock_irqrestore(&port->ah_lock, flags);
  478. return src_path_mask;
  479. }
  480. int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
  481. struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
  482. {
  483. int ret;
  484. u16 gid_index;
  485. int force_grh;
  486. memset(ah_attr, 0, sizeof *ah_attr);
  487. ah_attr->dlid = be16_to_cpu(rec->dlid);
  488. ah_attr->sl = rec->sl;
  489. ah_attr->src_path_bits = be16_to_cpu(rec->slid) &
  490. get_src_path_mask(device, port_num);
  491. ah_attr->port_num = port_num;
  492. ah_attr->static_rate = rec->rate;
  493. force_grh = rdma_port_get_link_layer(device, port_num) == IB_LINK_LAYER_ETHERNET;
  494. if (rec->hop_limit > 1 || force_grh) {
  495. ah_attr->ah_flags = IB_AH_GRH;
  496. ah_attr->grh.dgid = rec->dgid;
  497. ret = ib_find_cached_gid(device, &rec->sgid, &port_num,
  498. &gid_index);
  499. if (ret)
  500. return ret;
  501. ah_attr->grh.sgid_index = gid_index;
  502. ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
  503. ah_attr->grh.hop_limit = rec->hop_limit;
  504. ah_attr->grh.traffic_class = rec->traffic_class;
  505. }
  506. return 0;
  507. }
  508. EXPORT_SYMBOL(ib_init_ah_from_path);
  509. static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
  510. {
  511. unsigned long flags;
  512. spin_lock_irqsave(&query->port->ah_lock, flags);
  513. if (!query->port->sm_ah) {
  514. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  515. return -EAGAIN;
  516. }
  517. kref_get(&query->port->sm_ah->ref);
  518. query->sm_ah = query->port->sm_ah;
  519. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  520. query->mad_buf = ib_create_send_mad(query->port->agent, 1,
  521. query->sm_ah->pkey_index,
  522. 0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
  523. gfp_mask);
  524. if (IS_ERR(query->mad_buf)) {
  525. kref_put(&query->sm_ah->ref, free_sm_ah);
  526. return -ENOMEM;
  527. }
  528. query->mad_buf->ah = query->sm_ah->ah;
  529. return 0;
  530. }
  531. static void free_mad(struct ib_sa_query *query)
  532. {
  533. ib_free_send_mad(query->mad_buf);
  534. kref_put(&query->sm_ah->ref, free_sm_ah);
  535. }
  536. static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  537. {
  538. unsigned long flags;
  539. memset(mad, 0, sizeof *mad);
  540. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  541. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  542. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  543. spin_lock_irqsave(&tid_lock, flags);
  544. mad->mad_hdr.tid =
  545. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  546. spin_unlock_irqrestore(&tid_lock, flags);
  547. }
  548. static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
  549. {
  550. bool preload = gfp_mask & __GFP_WAIT;
  551. unsigned long flags;
  552. int ret, id;
  553. if (preload)
  554. idr_preload(gfp_mask);
  555. spin_lock_irqsave(&idr_lock, flags);
  556. id = idr_alloc(&query_idr, query, 0, 0, GFP_NOWAIT);
  557. spin_unlock_irqrestore(&idr_lock, flags);
  558. if (preload)
  559. idr_preload_end();
  560. if (id < 0)
  561. return id;
  562. query->mad_buf->timeout_ms = timeout_ms;
  563. query->mad_buf->context[0] = query;
  564. query->id = id;
  565. ret = ib_post_send_mad(query->mad_buf, NULL);
  566. if (ret) {
  567. spin_lock_irqsave(&idr_lock, flags);
  568. idr_remove(&query_idr, id);
  569. spin_unlock_irqrestore(&idr_lock, flags);
  570. }
  571. /*
  572. * It's not safe to dereference query any more, because the
  573. * send may already have completed and freed the query in
  574. * another context.
  575. */
  576. return ret ? ret : id;
  577. }
  578. void ib_sa_unpack_path(void *attribute, struct ib_sa_path_rec *rec)
  579. {
  580. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
  581. }
  582. EXPORT_SYMBOL(ib_sa_unpack_path);
  583. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  584. int status,
  585. struct ib_sa_mad *mad)
  586. {
  587. struct ib_sa_path_query *query =
  588. container_of(sa_query, struct ib_sa_path_query, sa_query);
  589. if (mad) {
  590. struct ib_sa_path_rec rec;
  591. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  592. mad->data, &rec);
  593. query->callback(status, &rec, query->context);
  594. } else
  595. query->callback(status, NULL, query->context);
  596. }
  597. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  598. {
  599. kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  600. }
  601. /**
  602. * ib_sa_path_rec_get - Start a Path get query
  603. * @client:SA client
  604. * @device:device to send query on
  605. * @port_num: port number to send query on
  606. * @rec:Path Record to send in query
  607. * @comp_mask:component mask to send in query
  608. * @timeout_ms:time to wait for response
  609. * @gfp_mask:GFP mask to use for internal allocations
  610. * @callback:function called when query completes, times out or is
  611. * canceled
  612. * @context:opaque user context passed to callback
  613. * @sa_query:query context, used to cancel query
  614. *
  615. * Send a Path Record Get query to the SA to look up a path. The
  616. * callback function will be called when the query completes (or
  617. * fails); status is 0 for a successful response, -EINTR if the query
  618. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  619. * occurred sending the query. The resp parameter of the callback is
  620. * only valid if status is 0.
  621. *
  622. * If the return value of ib_sa_path_rec_get() is negative, it is an
  623. * error code. Otherwise it is a query ID that can be used to cancel
  624. * the query.
  625. */
  626. int ib_sa_path_rec_get(struct ib_sa_client *client,
  627. struct ib_device *device, u8 port_num,
  628. struct ib_sa_path_rec *rec,
  629. ib_sa_comp_mask comp_mask,
  630. int timeout_ms, gfp_t gfp_mask,
  631. void (*callback)(int status,
  632. struct ib_sa_path_rec *resp,
  633. void *context),
  634. void *context,
  635. struct ib_sa_query **sa_query)
  636. {
  637. struct ib_sa_path_query *query;
  638. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  639. struct ib_sa_port *port;
  640. struct ib_mad_agent *agent;
  641. struct ib_sa_mad *mad;
  642. int ret;
  643. if (!sa_dev)
  644. return -ENODEV;
  645. port = &sa_dev->port[port_num - sa_dev->start_port];
  646. agent = port->agent;
  647. query = kmalloc(sizeof *query, gfp_mask);
  648. if (!query)
  649. return -ENOMEM;
  650. query->sa_query.port = port;
  651. ret = alloc_mad(&query->sa_query, gfp_mask);
  652. if (ret)
  653. goto err1;
  654. ib_sa_client_get(client);
  655. query->sa_query.client = client;
  656. query->callback = callback;
  657. query->context = context;
  658. mad = query->sa_query.mad_buf->mad;
  659. init_mad(mad, agent);
  660. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  661. query->sa_query.release = ib_sa_path_rec_release;
  662. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  663. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  664. mad->sa_hdr.comp_mask = comp_mask;
  665. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
  666. *sa_query = &query->sa_query;
  667. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  668. if (ret < 0)
  669. goto err2;
  670. return ret;
  671. err2:
  672. *sa_query = NULL;
  673. ib_sa_client_put(query->sa_query.client);
  674. free_mad(&query->sa_query);
  675. err1:
  676. kfree(query);
  677. return ret;
  678. }
  679. EXPORT_SYMBOL(ib_sa_path_rec_get);
  680. static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  681. int status,
  682. struct ib_sa_mad *mad)
  683. {
  684. struct ib_sa_service_query *query =
  685. container_of(sa_query, struct ib_sa_service_query, sa_query);
  686. if (mad) {
  687. struct ib_sa_service_rec rec;
  688. ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  689. mad->data, &rec);
  690. query->callback(status, &rec, query->context);
  691. } else
  692. query->callback(status, NULL, query->context);
  693. }
  694. static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  695. {
  696. kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  697. }
  698. /**
  699. * ib_sa_service_rec_query - Start Service Record operation
  700. * @client:SA client
  701. * @device:device to send request on
  702. * @port_num: port number to send request on
  703. * @method:SA method - should be get, set, or delete
  704. * @rec:Service Record to send in request
  705. * @comp_mask:component mask to send in request
  706. * @timeout_ms:time to wait for response
  707. * @gfp_mask:GFP mask to use for internal allocations
  708. * @callback:function called when request completes, times out or is
  709. * canceled
  710. * @context:opaque user context passed to callback
  711. * @sa_query:request context, used to cancel request
  712. *
  713. * Send a Service Record set/get/delete to the SA to register,
  714. * unregister or query a service record.
  715. * The callback function will be called when the request completes (or
  716. * fails); status is 0 for a successful response, -EINTR if the query
  717. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  718. * occurred sending the query. The resp parameter of the callback is
  719. * only valid if status is 0.
  720. *
  721. * If the return value of ib_sa_service_rec_query() is negative, it is an
  722. * error code. Otherwise it is a request ID that can be used to cancel
  723. * the query.
  724. */
  725. int ib_sa_service_rec_query(struct ib_sa_client *client,
  726. struct ib_device *device, u8 port_num, u8 method,
  727. struct ib_sa_service_rec *rec,
  728. ib_sa_comp_mask comp_mask,
  729. int timeout_ms, gfp_t gfp_mask,
  730. void (*callback)(int status,
  731. struct ib_sa_service_rec *resp,
  732. void *context),
  733. void *context,
  734. struct ib_sa_query **sa_query)
  735. {
  736. struct ib_sa_service_query *query;
  737. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  738. struct ib_sa_port *port;
  739. struct ib_mad_agent *agent;
  740. struct ib_sa_mad *mad;
  741. int ret;
  742. if (!sa_dev)
  743. return -ENODEV;
  744. port = &sa_dev->port[port_num - sa_dev->start_port];
  745. agent = port->agent;
  746. if (method != IB_MGMT_METHOD_GET &&
  747. method != IB_MGMT_METHOD_SET &&
  748. method != IB_SA_METHOD_DELETE)
  749. return -EINVAL;
  750. query = kmalloc(sizeof *query, gfp_mask);
  751. if (!query)
  752. return -ENOMEM;
  753. query->sa_query.port = port;
  754. ret = alloc_mad(&query->sa_query, gfp_mask);
  755. if (ret)
  756. goto err1;
  757. ib_sa_client_get(client);
  758. query->sa_query.client = client;
  759. query->callback = callback;
  760. query->context = context;
  761. mad = query->sa_query.mad_buf->mad;
  762. init_mad(mad, agent);
  763. query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
  764. query->sa_query.release = ib_sa_service_rec_release;
  765. mad->mad_hdr.method = method;
  766. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
  767. mad->sa_hdr.comp_mask = comp_mask;
  768. ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
  769. rec, mad->data);
  770. *sa_query = &query->sa_query;
  771. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  772. if (ret < 0)
  773. goto err2;
  774. return ret;
  775. err2:
  776. *sa_query = NULL;
  777. ib_sa_client_put(query->sa_query.client);
  778. free_mad(&query->sa_query);
  779. err1:
  780. kfree(query);
  781. return ret;
  782. }
  783. EXPORT_SYMBOL(ib_sa_service_rec_query);
  784. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  785. int status,
  786. struct ib_sa_mad *mad)
  787. {
  788. struct ib_sa_mcmember_query *query =
  789. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  790. if (mad) {
  791. struct ib_sa_mcmember_rec rec;
  792. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  793. mad->data, &rec);
  794. query->callback(status, &rec, query->context);
  795. } else
  796. query->callback(status, NULL, query->context);
  797. }
  798. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  799. {
  800. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  801. }
  802. int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
  803. struct ib_device *device, u8 port_num,
  804. u8 method,
  805. struct ib_sa_mcmember_rec *rec,
  806. ib_sa_comp_mask comp_mask,
  807. int timeout_ms, gfp_t gfp_mask,
  808. void (*callback)(int status,
  809. struct ib_sa_mcmember_rec *resp,
  810. void *context),
  811. void *context,
  812. struct ib_sa_query **sa_query)
  813. {
  814. struct ib_sa_mcmember_query *query;
  815. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  816. struct ib_sa_port *port;
  817. struct ib_mad_agent *agent;
  818. struct ib_sa_mad *mad;
  819. int ret;
  820. if (!sa_dev)
  821. return -ENODEV;
  822. port = &sa_dev->port[port_num - sa_dev->start_port];
  823. agent = port->agent;
  824. query = kmalloc(sizeof *query, gfp_mask);
  825. if (!query)
  826. return -ENOMEM;
  827. query->sa_query.port = port;
  828. ret = alloc_mad(&query->sa_query, gfp_mask);
  829. if (ret)
  830. goto err1;
  831. ib_sa_client_get(client);
  832. query->sa_query.client = client;
  833. query->callback = callback;
  834. query->context = context;
  835. mad = query->sa_query.mad_buf->mad;
  836. init_mad(mad, agent);
  837. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  838. query->sa_query.release = ib_sa_mcmember_rec_release;
  839. mad->mad_hdr.method = method;
  840. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  841. mad->sa_hdr.comp_mask = comp_mask;
  842. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  843. rec, mad->data);
  844. *sa_query = &query->sa_query;
  845. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  846. if (ret < 0)
  847. goto err2;
  848. return ret;
  849. err2:
  850. *sa_query = NULL;
  851. ib_sa_client_put(query->sa_query.client);
  852. free_mad(&query->sa_query);
  853. err1:
  854. kfree(query);
  855. return ret;
  856. }
  857. /* Support GuidInfoRecord */
  858. static void ib_sa_guidinfo_rec_callback(struct ib_sa_query *sa_query,
  859. int status,
  860. struct ib_sa_mad *mad)
  861. {
  862. struct ib_sa_guidinfo_query *query =
  863. container_of(sa_query, struct ib_sa_guidinfo_query, sa_query);
  864. if (mad) {
  865. struct ib_sa_guidinfo_rec rec;
  866. ib_unpack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table),
  867. mad->data, &rec);
  868. query->callback(status, &rec, query->context);
  869. } else
  870. query->callback(status, NULL, query->context);
  871. }
  872. static void ib_sa_guidinfo_rec_release(struct ib_sa_query *sa_query)
  873. {
  874. kfree(container_of(sa_query, struct ib_sa_guidinfo_query, sa_query));
  875. }
  876. int ib_sa_guid_info_rec_query(struct ib_sa_client *client,
  877. struct ib_device *device, u8 port_num,
  878. struct ib_sa_guidinfo_rec *rec,
  879. ib_sa_comp_mask comp_mask, u8 method,
  880. int timeout_ms, gfp_t gfp_mask,
  881. void (*callback)(int status,
  882. struct ib_sa_guidinfo_rec *resp,
  883. void *context),
  884. void *context,
  885. struct ib_sa_query **sa_query)
  886. {
  887. struct ib_sa_guidinfo_query *query;
  888. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  889. struct ib_sa_port *port;
  890. struct ib_mad_agent *agent;
  891. struct ib_sa_mad *mad;
  892. int ret;
  893. if (!sa_dev)
  894. return -ENODEV;
  895. if (method != IB_MGMT_METHOD_GET &&
  896. method != IB_MGMT_METHOD_SET &&
  897. method != IB_SA_METHOD_DELETE) {
  898. return -EINVAL;
  899. }
  900. port = &sa_dev->port[port_num - sa_dev->start_port];
  901. agent = port->agent;
  902. query = kmalloc(sizeof *query, gfp_mask);
  903. if (!query)
  904. return -ENOMEM;
  905. query->sa_query.port = port;
  906. ret = alloc_mad(&query->sa_query, gfp_mask);
  907. if (ret)
  908. goto err1;
  909. ib_sa_client_get(client);
  910. query->sa_query.client = client;
  911. query->callback = callback;
  912. query->context = context;
  913. mad = query->sa_query.mad_buf->mad;
  914. init_mad(mad, agent);
  915. query->sa_query.callback = callback ? ib_sa_guidinfo_rec_callback : NULL;
  916. query->sa_query.release = ib_sa_guidinfo_rec_release;
  917. mad->mad_hdr.method = method;
  918. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC);
  919. mad->sa_hdr.comp_mask = comp_mask;
  920. ib_pack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), rec,
  921. mad->data);
  922. *sa_query = &query->sa_query;
  923. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  924. if (ret < 0)
  925. goto err2;
  926. return ret;
  927. err2:
  928. *sa_query = NULL;
  929. ib_sa_client_put(query->sa_query.client);
  930. free_mad(&query->sa_query);
  931. err1:
  932. kfree(query);
  933. return ret;
  934. }
  935. EXPORT_SYMBOL(ib_sa_guid_info_rec_query);
  936. static void send_handler(struct ib_mad_agent *agent,
  937. struct ib_mad_send_wc *mad_send_wc)
  938. {
  939. struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
  940. unsigned long flags;
  941. if (query->callback)
  942. switch (mad_send_wc->status) {
  943. case IB_WC_SUCCESS:
  944. /* No callback -- already got recv */
  945. break;
  946. case IB_WC_RESP_TIMEOUT_ERR:
  947. query->callback(query, -ETIMEDOUT, NULL);
  948. break;
  949. case IB_WC_WR_FLUSH_ERR:
  950. query->callback(query, -EINTR, NULL);
  951. break;
  952. default:
  953. query->callback(query, -EIO, NULL);
  954. break;
  955. }
  956. spin_lock_irqsave(&idr_lock, flags);
  957. idr_remove(&query_idr, query->id);
  958. spin_unlock_irqrestore(&idr_lock, flags);
  959. free_mad(query);
  960. ib_sa_client_put(query->client);
  961. query->release(query);
  962. }
  963. static void recv_handler(struct ib_mad_agent *mad_agent,
  964. struct ib_mad_recv_wc *mad_recv_wc)
  965. {
  966. struct ib_sa_query *query;
  967. struct ib_mad_send_buf *mad_buf;
  968. mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
  969. query = mad_buf->context[0];
  970. if (query->callback) {
  971. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  972. query->callback(query,
  973. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  974. -EINVAL : 0,
  975. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  976. else
  977. query->callback(query, -EIO, NULL);
  978. }
  979. ib_free_recv_mad(mad_recv_wc);
  980. }
  981. static void ib_sa_add_one(struct ib_device *device)
  982. {
  983. struct ib_sa_device *sa_dev;
  984. int s, e, i;
  985. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  986. return;
  987. if (device->node_type == RDMA_NODE_IB_SWITCH)
  988. s = e = 0;
  989. else {
  990. s = 1;
  991. e = device->phys_port_cnt;
  992. }
  993. sa_dev = kzalloc(sizeof *sa_dev +
  994. (e - s + 1) * sizeof (struct ib_sa_port),
  995. GFP_KERNEL);
  996. if (!sa_dev)
  997. return;
  998. sa_dev->start_port = s;
  999. sa_dev->end_port = e;
  1000. for (i = 0; i <= e - s; ++i) {
  1001. spin_lock_init(&sa_dev->port[i].ah_lock);
  1002. if (rdma_port_get_link_layer(device, i + 1) != IB_LINK_LAYER_INFINIBAND)
  1003. continue;
  1004. sa_dev->port[i].sm_ah = NULL;
  1005. sa_dev->port[i].port_num = i + s;
  1006. sa_dev->port[i].agent =
  1007. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  1008. NULL, 0, send_handler,
  1009. recv_handler, sa_dev);
  1010. if (IS_ERR(sa_dev->port[i].agent))
  1011. goto err;
  1012. INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
  1013. }
  1014. ib_set_client_data(device, &sa_client, sa_dev);
  1015. /*
  1016. * We register our event handler after everything is set up,
  1017. * and then update our cached info after the event handler is
  1018. * registered to avoid any problems if a port changes state
  1019. * during our initialization.
  1020. */
  1021. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  1022. if (ib_register_event_handler(&sa_dev->event_handler))
  1023. goto err;
  1024. for (i = 0; i <= e - s; ++i)
  1025. if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND)
  1026. update_sm_ah(&sa_dev->port[i].update_task);
  1027. return;
  1028. err:
  1029. while (--i >= 0)
  1030. if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND)
  1031. ib_unregister_mad_agent(sa_dev->port[i].agent);
  1032. kfree(sa_dev);
  1033. return;
  1034. }
  1035. static void ib_sa_remove_one(struct ib_device *device)
  1036. {
  1037. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  1038. int i;
  1039. if (!sa_dev)
  1040. return;
  1041. ib_unregister_event_handler(&sa_dev->event_handler);
  1042. flush_workqueue(ib_wq);
  1043. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  1044. if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND) {
  1045. ib_unregister_mad_agent(sa_dev->port[i].agent);
  1046. if (sa_dev->port[i].sm_ah)
  1047. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  1048. }
  1049. }
  1050. kfree(sa_dev);
  1051. }
  1052. static int __init ib_sa_init(void)
  1053. {
  1054. int ret;
  1055. get_random_bytes(&tid, sizeof tid);
  1056. ret = ib_register_client(&sa_client);
  1057. if (ret) {
  1058. printk(KERN_ERR "Couldn't register ib_sa client\n");
  1059. goto err1;
  1060. }
  1061. ret = mcast_init();
  1062. if (ret) {
  1063. printk(KERN_ERR "Couldn't initialize multicast handling\n");
  1064. goto err2;
  1065. }
  1066. return 0;
  1067. err2:
  1068. ib_unregister_client(&sa_client);
  1069. err1:
  1070. return ret;
  1071. }
  1072. static void __exit ib_sa_cleanup(void)
  1073. {
  1074. mcast_cleanup();
  1075. ib_unregister_client(&sa_client);
  1076. idr_destroy(&query_idr);
  1077. }
  1078. module_init(ib_sa_init);
  1079. module_exit(ib_sa_cleanup);