sa_query.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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_mcmember_query {
  88. void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
  89. void *context;
  90. struct ib_sa_query sa_query;
  91. };
  92. static void ib_sa_add_one(struct ib_device *device);
  93. static void ib_sa_remove_one(struct ib_device *device);
  94. static struct ib_client sa_client = {
  95. .name = "sa",
  96. .add = ib_sa_add_one,
  97. .remove = ib_sa_remove_one
  98. };
  99. static DEFINE_SPINLOCK(idr_lock);
  100. static DEFINE_IDR(query_idr);
  101. static DEFINE_SPINLOCK(tid_lock);
  102. static u32 tid;
  103. #define PATH_REC_FIELD(field) \
  104. .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
  105. .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
  106. .field_name = "sa_path_rec:" #field
  107. static const struct ib_field path_rec_table[] = {
  108. { PATH_REC_FIELD(service_id),
  109. .offset_words = 0,
  110. .offset_bits = 0,
  111. .size_bits = 64 },
  112. { PATH_REC_FIELD(dgid),
  113. .offset_words = 2,
  114. .offset_bits = 0,
  115. .size_bits = 128 },
  116. { PATH_REC_FIELD(sgid),
  117. .offset_words = 6,
  118. .offset_bits = 0,
  119. .size_bits = 128 },
  120. { PATH_REC_FIELD(dlid),
  121. .offset_words = 10,
  122. .offset_bits = 0,
  123. .size_bits = 16 },
  124. { PATH_REC_FIELD(slid),
  125. .offset_words = 10,
  126. .offset_bits = 16,
  127. .size_bits = 16 },
  128. { PATH_REC_FIELD(raw_traffic),
  129. .offset_words = 11,
  130. .offset_bits = 0,
  131. .size_bits = 1 },
  132. { RESERVED,
  133. .offset_words = 11,
  134. .offset_bits = 1,
  135. .size_bits = 3 },
  136. { PATH_REC_FIELD(flow_label),
  137. .offset_words = 11,
  138. .offset_bits = 4,
  139. .size_bits = 20 },
  140. { PATH_REC_FIELD(hop_limit),
  141. .offset_words = 11,
  142. .offset_bits = 24,
  143. .size_bits = 8 },
  144. { PATH_REC_FIELD(traffic_class),
  145. .offset_words = 12,
  146. .offset_bits = 0,
  147. .size_bits = 8 },
  148. { PATH_REC_FIELD(reversible),
  149. .offset_words = 12,
  150. .offset_bits = 8,
  151. .size_bits = 1 },
  152. { PATH_REC_FIELD(numb_path),
  153. .offset_words = 12,
  154. .offset_bits = 9,
  155. .size_bits = 7 },
  156. { PATH_REC_FIELD(pkey),
  157. .offset_words = 12,
  158. .offset_bits = 16,
  159. .size_bits = 16 },
  160. { PATH_REC_FIELD(qos_class),
  161. .offset_words = 13,
  162. .offset_bits = 0,
  163. .size_bits = 12 },
  164. { PATH_REC_FIELD(sl),
  165. .offset_words = 13,
  166. .offset_bits = 12,
  167. .size_bits = 4 },
  168. { PATH_REC_FIELD(mtu_selector),
  169. .offset_words = 13,
  170. .offset_bits = 16,
  171. .size_bits = 2 },
  172. { PATH_REC_FIELD(mtu),
  173. .offset_words = 13,
  174. .offset_bits = 18,
  175. .size_bits = 6 },
  176. { PATH_REC_FIELD(rate_selector),
  177. .offset_words = 13,
  178. .offset_bits = 24,
  179. .size_bits = 2 },
  180. { PATH_REC_FIELD(rate),
  181. .offset_words = 13,
  182. .offset_bits = 26,
  183. .size_bits = 6 },
  184. { PATH_REC_FIELD(packet_life_time_selector),
  185. .offset_words = 14,
  186. .offset_bits = 0,
  187. .size_bits = 2 },
  188. { PATH_REC_FIELD(packet_life_time),
  189. .offset_words = 14,
  190. .offset_bits = 2,
  191. .size_bits = 6 },
  192. { PATH_REC_FIELD(preference),
  193. .offset_words = 14,
  194. .offset_bits = 8,
  195. .size_bits = 8 },
  196. { RESERVED,
  197. .offset_words = 14,
  198. .offset_bits = 16,
  199. .size_bits = 48 },
  200. };
  201. #define MCMEMBER_REC_FIELD(field) \
  202. .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
  203. .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
  204. .field_name = "sa_mcmember_rec:" #field
  205. static const struct ib_field mcmember_rec_table[] = {
  206. { MCMEMBER_REC_FIELD(mgid),
  207. .offset_words = 0,
  208. .offset_bits = 0,
  209. .size_bits = 128 },
  210. { MCMEMBER_REC_FIELD(port_gid),
  211. .offset_words = 4,
  212. .offset_bits = 0,
  213. .size_bits = 128 },
  214. { MCMEMBER_REC_FIELD(qkey),
  215. .offset_words = 8,
  216. .offset_bits = 0,
  217. .size_bits = 32 },
  218. { MCMEMBER_REC_FIELD(mlid),
  219. .offset_words = 9,
  220. .offset_bits = 0,
  221. .size_bits = 16 },
  222. { MCMEMBER_REC_FIELD(mtu_selector),
  223. .offset_words = 9,
  224. .offset_bits = 16,
  225. .size_bits = 2 },
  226. { MCMEMBER_REC_FIELD(mtu),
  227. .offset_words = 9,
  228. .offset_bits = 18,
  229. .size_bits = 6 },
  230. { MCMEMBER_REC_FIELD(traffic_class),
  231. .offset_words = 9,
  232. .offset_bits = 24,
  233. .size_bits = 8 },
  234. { MCMEMBER_REC_FIELD(pkey),
  235. .offset_words = 10,
  236. .offset_bits = 0,
  237. .size_bits = 16 },
  238. { MCMEMBER_REC_FIELD(rate_selector),
  239. .offset_words = 10,
  240. .offset_bits = 16,
  241. .size_bits = 2 },
  242. { MCMEMBER_REC_FIELD(rate),
  243. .offset_words = 10,
  244. .offset_bits = 18,
  245. .size_bits = 6 },
  246. { MCMEMBER_REC_FIELD(packet_life_time_selector),
  247. .offset_words = 10,
  248. .offset_bits = 24,
  249. .size_bits = 2 },
  250. { MCMEMBER_REC_FIELD(packet_life_time),
  251. .offset_words = 10,
  252. .offset_bits = 26,
  253. .size_bits = 6 },
  254. { MCMEMBER_REC_FIELD(sl),
  255. .offset_words = 11,
  256. .offset_bits = 0,
  257. .size_bits = 4 },
  258. { MCMEMBER_REC_FIELD(flow_label),
  259. .offset_words = 11,
  260. .offset_bits = 4,
  261. .size_bits = 20 },
  262. { MCMEMBER_REC_FIELD(hop_limit),
  263. .offset_words = 11,
  264. .offset_bits = 24,
  265. .size_bits = 8 },
  266. { MCMEMBER_REC_FIELD(scope),
  267. .offset_words = 12,
  268. .offset_bits = 0,
  269. .size_bits = 4 },
  270. { MCMEMBER_REC_FIELD(join_state),
  271. .offset_words = 12,
  272. .offset_bits = 4,
  273. .size_bits = 4 },
  274. { MCMEMBER_REC_FIELD(proxy_join),
  275. .offset_words = 12,
  276. .offset_bits = 8,
  277. .size_bits = 1 },
  278. { RESERVED,
  279. .offset_words = 12,
  280. .offset_bits = 9,
  281. .size_bits = 23 },
  282. };
  283. #define SERVICE_REC_FIELD(field) \
  284. .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
  285. .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
  286. .field_name = "sa_service_rec:" #field
  287. static const struct ib_field service_rec_table[] = {
  288. { SERVICE_REC_FIELD(id),
  289. .offset_words = 0,
  290. .offset_bits = 0,
  291. .size_bits = 64 },
  292. { SERVICE_REC_FIELD(gid),
  293. .offset_words = 2,
  294. .offset_bits = 0,
  295. .size_bits = 128 },
  296. { SERVICE_REC_FIELD(pkey),
  297. .offset_words = 6,
  298. .offset_bits = 0,
  299. .size_bits = 16 },
  300. { SERVICE_REC_FIELD(lease),
  301. .offset_words = 7,
  302. .offset_bits = 0,
  303. .size_bits = 32 },
  304. { SERVICE_REC_FIELD(key),
  305. .offset_words = 8,
  306. .offset_bits = 0,
  307. .size_bits = 128 },
  308. { SERVICE_REC_FIELD(name),
  309. .offset_words = 12,
  310. .offset_bits = 0,
  311. .size_bits = 64*8 },
  312. { SERVICE_REC_FIELD(data8),
  313. .offset_words = 28,
  314. .offset_bits = 0,
  315. .size_bits = 16*8 },
  316. { SERVICE_REC_FIELD(data16),
  317. .offset_words = 32,
  318. .offset_bits = 0,
  319. .size_bits = 8*16 },
  320. { SERVICE_REC_FIELD(data32),
  321. .offset_words = 36,
  322. .offset_bits = 0,
  323. .size_bits = 4*32 },
  324. { SERVICE_REC_FIELD(data64),
  325. .offset_words = 40,
  326. .offset_bits = 0,
  327. .size_bits = 2*64 },
  328. };
  329. static void free_sm_ah(struct kref *kref)
  330. {
  331. struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
  332. ib_destroy_ah(sm_ah->ah);
  333. kfree(sm_ah);
  334. }
  335. static void update_sm_ah(struct work_struct *work)
  336. {
  337. struct ib_sa_port *port =
  338. container_of(work, struct ib_sa_port, update_task);
  339. struct ib_sa_sm_ah *new_ah;
  340. struct ib_port_attr port_attr;
  341. struct ib_ah_attr ah_attr;
  342. if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
  343. printk(KERN_WARNING "Couldn't query port\n");
  344. return;
  345. }
  346. new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
  347. if (!new_ah) {
  348. printk(KERN_WARNING "Couldn't allocate new SM AH\n");
  349. return;
  350. }
  351. kref_init(&new_ah->ref);
  352. new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
  353. new_ah->pkey_index = 0;
  354. if (ib_find_pkey(port->agent->device, port->port_num,
  355. IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
  356. printk(KERN_ERR "Couldn't find index for default PKey\n");
  357. memset(&ah_attr, 0, sizeof ah_attr);
  358. ah_attr.dlid = port_attr.sm_lid;
  359. ah_attr.sl = port_attr.sm_sl;
  360. ah_attr.port_num = port->port_num;
  361. new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
  362. if (IS_ERR(new_ah->ah)) {
  363. printk(KERN_WARNING "Couldn't create new SM AH\n");
  364. kfree(new_ah);
  365. return;
  366. }
  367. spin_lock_irq(&port->ah_lock);
  368. if (port->sm_ah)
  369. kref_put(&port->sm_ah->ref, free_sm_ah);
  370. port->sm_ah = new_ah;
  371. spin_unlock_irq(&port->ah_lock);
  372. }
  373. static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
  374. {
  375. if (event->event == IB_EVENT_PORT_ERR ||
  376. event->event == IB_EVENT_PORT_ACTIVE ||
  377. event->event == IB_EVENT_LID_CHANGE ||
  378. event->event == IB_EVENT_PKEY_CHANGE ||
  379. event->event == IB_EVENT_SM_CHANGE ||
  380. event->event == IB_EVENT_CLIENT_REREGISTER) {
  381. unsigned long flags;
  382. struct ib_sa_device *sa_dev =
  383. container_of(handler, typeof(*sa_dev), event_handler);
  384. struct ib_sa_port *port =
  385. &sa_dev->port[event->element.port_num - sa_dev->start_port];
  386. if (rdma_port_get_link_layer(handler->device, port->port_num) != IB_LINK_LAYER_INFINIBAND)
  387. return;
  388. spin_lock_irqsave(&port->ah_lock, flags);
  389. if (port->sm_ah)
  390. kref_put(&port->sm_ah->ref, free_sm_ah);
  391. port->sm_ah = NULL;
  392. spin_unlock_irqrestore(&port->ah_lock, flags);
  393. schedule_work(&sa_dev->port[event->element.port_num -
  394. sa_dev->start_port].update_task);
  395. }
  396. }
  397. void ib_sa_register_client(struct ib_sa_client *client)
  398. {
  399. atomic_set(&client->users, 1);
  400. init_completion(&client->comp);
  401. }
  402. EXPORT_SYMBOL(ib_sa_register_client);
  403. void ib_sa_unregister_client(struct ib_sa_client *client)
  404. {
  405. ib_sa_client_put(client);
  406. wait_for_completion(&client->comp);
  407. }
  408. EXPORT_SYMBOL(ib_sa_unregister_client);
  409. /**
  410. * ib_sa_cancel_query - try to cancel an SA query
  411. * @id:ID of query to cancel
  412. * @query:query pointer to cancel
  413. *
  414. * Try to cancel an SA query. If the id and query don't match up or
  415. * the query has already completed, nothing is done. Otherwise the
  416. * query is canceled and will complete with a status of -EINTR.
  417. */
  418. void ib_sa_cancel_query(int id, struct ib_sa_query *query)
  419. {
  420. unsigned long flags;
  421. struct ib_mad_agent *agent;
  422. struct ib_mad_send_buf *mad_buf;
  423. spin_lock_irqsave(&idr_lock, flags);
  424. if (idr_find(&query_idr, id) != query) {
  425. spin_unlock_irqrestore(&idr_lock, flags);
  426. return;
  427. }
  428. agent = query->port->agent;
  429. mad_buf = query->mad_buf;
  430. spin_unlock_irqrestore(&idr_lock, flags);
  431. ib_cancel_mad(agent, mad_buf);
  432. }
  433. EXPORT_SYMBOL(ib_sa_cancel_query);
  434. static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
  435. {
  436. struct ib_sa_device *sa_dev;
  437. struct ib_sa_port *port;
  438. unsigned long flags;
  439. u8 src_path_mask;
  440. sa_dev = ib_get_client_data(device, &sa_client);
  441. if (!sa_dev)
  442. return 0x7f;
  443. port = &sa_dev->port[port_num - sa_dev->start_port];
  444. spin_lock_irqsave(&port->ah_lock, flags);
  445. src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
  446. spin_unlock_irqrestore(&port->ah_lock, flags);
  447. return src_path_mask;
  448. }
  449. int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
  450. struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
  451. {
  452. int ret;
  453. u16 gid_index;
  454. memset(ah_attr, 0, sizeof *ah_attr);
  455. ah_attr->dlid = be16_to_cpu(rec->dlid);
  456. ah_attr->sl = rec->sl;
  457. ah_attr->src_path_bits = be16_to_cpu(rec->slid) &
  458. get_src_path_mask(device, port_num);
  459. ah_attr->port_num = port_num;
  460. ah_attr->static_rate = rec->rate;
  461. if (rec->hop_limit > 1) {
  462. ah_attr->ah_flags = IB_AH_GRH;
  463. ah_attr->grh.dgid = rec->dgid;
  464. ret = ib_find_cached_gid(device, &rec->sgid, &port_num,
  465. &gid_index);
  466. if (ret)
  467. return ret;
  468. ah_attr->grh.sgid_index = gid_index;
  469. ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
  470. ah_attr->grh.hop_limit = rec->hop_limit;
  471. ah_attr->grh.traffic_class = rec->traffic_class;
  472. }
  473. return 0;
  474. }
  475. EXPORT_SYMBOL(ib_init_ah_from_path);
  476. static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
  477. {
  478. unsigned long flags;
  479. spin_lock_irqsave(&query->port->ah_lock, flags);
  480. if (!query->port->sm_ah) {
  481. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  482. return -EAGAIN;
  483. }
  484. kref_get(&query->port->sm_ah->ref);
  485. query->sm_ah = query->port->sm_ah;
  486. spin_unlock_irqrestore(&query->port->ah_lock, flags);
  487. query->mad_buf = ib_create_send_mad(query->port->agent, 1,
  488. query->sm_ah->pkey_index,
  489. 0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
  490. gfp_mask);
  491. if (IS_ERR(query->mad_buf)) {
  492. kref_put(&query->sm_ah->ref, free_sm_ah);
  493. return -ENOMEM;
  494. }
  495. query->mad_buf->ah = query->sm_ah->ah;
  496. return 0;
  497. }
  498. static void free_mad(struct ib_sa_query *query)
  499. {
  500. ib_free_send_mad(query->mad_buf);
  501. kref_put(&query->sm_ah->ref, free_sm_ah);
  502. }
  503. static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
  504. {
  505. unsigned long flags;
  506. memset(mad, 0, sizeof *mad);
  507. mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
  508. mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  509. mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  510. spin_lock_irqsave(&tid_lock, flags);
  511. mad->mad_hdr.tid =
  512. cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
  513. spin_unlock_irqrestore(&tid_lock, flags);
  514. }
  515. static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
  516. {
  517. unsigned long flags;
  518. int ret, id;
  519. retry:
  520. if (!idr_pre_get(&query_idr, gfp_mask))
  521. return -ENOMEM;
  522. spin_lock_irqsave(&idr_lock, flags);
  523. ret = idr_get_new(&query_idr, query, &id);
  524. spin_unlock_irqrestore(&idr_lock, flags);
  525. if (ret == -EAGAIN)
  526. goto retry;
  527. if (ret)
  528. return ret;
  529. query->mad_buf->timeout_ms = timeout_ms;
  530. query->mad_buf->context[0] = query;
  531. query->id = id;
  532. ret = ib_post_send_mad(query->mad_buf, NULL);
  533. if (ret) {
  534. spin_lock_irqsave(&idr_lock, flags);
  535. idr_remove(&query_idr, id);
  536. spin_unlock_irqrestore(&idr_lock, flags);
  537. }
  538. /*
  539. * It's not safe to dereference query any more, because the
  540. * send may already have completed and freed the query in
  541. * another context.
  542. */
  543. return ret ? ret : id;
  544. }
  545. void ib_sa_unpack_path(void *attribute, struct ib_sa_path_rec *rec)
  546. {
  547. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
  548. }
  549. EXPORT_SYMBOL(ib_sa_unpack_path);
  550. static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
  551. int status,
  552. struct ib_sa_mad *mad)
  553. {
  554. struct ib_sa_path_query *query =
  555. container_of(sa_query, struct ib_sa_path_query, sa_query);
  556. if (mad) {
  557. struct ib_sa_path_rec rec;
  558. ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
  559. mad->data, &rec);
  560. query->callback(status, &rec, query->context);
  561. } else
  562. query->callback(status, NULL, query->context);
  563. }
  564. static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
  565. {
  566. kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
  567. }
  568. /**
  569. * ib_sa_path_rec_get - Start a Path get query
  570. * @client:SA client
  571. * @device:device to send query on
  572. * @port_num: port number to send query on
  573. * @rec:Path Record to send in query
  574. * @comp_mask:component mask to send in query
  575. * @timeout_ms:time to wait for response
  576. * @gfp_mask:GFP mask to use for internal allocations
  577. * @callback:function called when query completes, times out or is
  578. * canceled
  579. * @context:opaque user context passed to callback
  580. * @sa_query:query context, used to cancel query
  581. *
  582. * Send a Path Record Get query to the SA to look up a path. The
  583. * callback function will be called when the query completes (or
  584. * fails); status is 0 for a successful response, -EINTR if the query
  585. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  586. * occurred sending the query. The resp parameter of the callback is
  587. * only valid if status is 0.
  588. *
  589. * If the return value of ib_sa_path_rec_get() is negative, it is an
  590. * error code. Otherwise it is a query ID that can be used to cancel
  591. * the query.
  592. */
  593. int ib_sa_path_rec_get(struct ib_sa_client *client,
  594. struct ib_device *device, u8 port_num,
  595. struct ib_sa_path_rec *rec,
  596. ib_sa_comp_mask comp_mask,
  597. int timeout_ms, gfp_t gfp_mask,
  598. void (*callback)(int status,
  599. struct ib_sa_path_rec *resp,
  600. void *context),
  601. void *context,
  602. struct ib_sa_query **sa_query)
  603. {
  604. struct ib_sa_path_query *query;
  605. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  606. struct ib_sa_port *port;
  607. struct ib_mad_agent *agent;
  608. struct ib_sa_mad *mad;
  609. int ret;
  610. if (!sa_dev)
  611. return -ENODEV;
  612. port = &sa_dev->port[port_num - sa_dev->start_port];
  613. agent = port->agent;
  614. query = kmalloc(sizeof *query, gfp_mask);
  615. if (!query)
  616. return -ENOMEM;
  617. query->sa_query.port = port;
  618. ret = alloc_mad(&query->sa_query, gfp_mask);
  619. if (ret)
  620. goto err1;
  621. ib_sa_client_get(client);
  622. query->sa_query.client = client;
  623. query->callback = callback;
  624. query->context = context;
  625. mad = query->sa_query.mad_buf->mad;
  626. init_mad(mad, agent);
  627. query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
  628. query->sa_query.release = ib_sa_path_rec_release;
  629. mad->mad_hdr.method = IB_MGMT_METHOD_GET;
  630. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
  631. mad->sa_hdr.comp_mask = comp_mask;
  632. ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
  633. *sa_query = &query->sa_query;
  634. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  635. if (ret < 0)
  636. goto err2;
  637. return ret;
  638. err2:
  639. *sa_query = NULL;
  640. ib_sa_client_put(query->sa_query.client);
  641. free_mad(&query->sa_query);
  642. err1:
  643. kfree(query);
  644. return ret;
  645. }
  646. EXPORT_SYMBOL(ib_sa_path_rec_get);
  647. static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
  648. int status,
  649. struct ib_sa_mad *mad)
  650. {
  651. struct ib_sa_service_query *query =
  652. container_of(sa_query, struct ib_sa_service_query, sa_query);
  653. if (mad) {
  654. struct ib_sa_service_rec rec;
  655. ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
  656. mad->data, &rec);
  657. query->callback(status, &rec, query->context);
  658. } else
  659. query->callback(status, NULL, query->context);
  660. }
  661. static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
  662. {
  663. kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
  664. }
  665. /**
  666. * ib_sa_service_rec_query - Start Service Record operation
  667. * @client:SA client
  668. * @device:device to send request on
  669. * @port_num: port number to send request on
  670. * @method:SA method - should be get, set, or delete
  671. * @rec:Service Record to send in request
  672. * @comp_mask:component mask to send in request
  673. * @timeout_ms:time to wait for response
  674. * @gfp_mask:GFP mask to use for internal allocations
  675. * @callback:function called when request completes, times out or is
  676. * canceled
  677. * @context:opaque user context passed to callback
  678. * @sa_query:request context, used to cancel request
  679. *
  680. * Send a Service Record set/get/delete to the SA to register,
  681. * unregister or query a service record.
  682. * The callback function will be called when the request completes (or
  683. * fails); status is 0 for a successful response, -EINTR if the query
  684. * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
  685. * occurred sending the query. The resp parameter of the callback is
  686. * only valid if status is 0.
  687. *
  688. * If the return value of ib_sa_service_rec_query() is negative, it is an
  689. * error code. Otherwise it is a request ID that can be used to cancel
  690. * the query.
  691. */
  692. int ib_sa_service_rec_query(struct ib_sa_client *client,
  693. struct ib_device *device, u8 port_num, u8 method,
  694. struct ib_sa_service_rec *rec,
  695. ib_sa_comp_mask comp_mask,
  696. int timeout_ms, gfp_t gfp_mask,
  697. void (*callback)(int status,
  698. struct ib_sa_service_rec *resp,
  699. void *context),
  700. void *context,
  701. struct ib_sa_query **sa_query)
  702. {
  703. struct ib_sa_service_query *query;
  704. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  705. struct ib_sa_port *port;
  706. struct ib_mad_agent *agent;
  707. struct ib_sa_mad *mad;
  708. int ret;
  709. if (!sa_dev)
  710. return -ENODEV;
  711. port = &sa_dev->port[port_num - sa_dev->start_port];
  712. agent = port->agent;
  713. if (method != IB_MGMT_METHOD_GET &&
  714. method != IB_MGMT_METHOD_SET &&
  715. method != IB_SA_METHOD_DELETE)
  716. return -EINVAL;
  717. query = kmalloc(sizeof *query, gfp_mask);
  718. if (!query)
  719. return -ENOMEM;
  720. query->sa_query.port = port;
  721. ret = alloc_mad(&query->sa_query, gfp_mask);
  722. if (ret)
  723. goto err1;
  724. ib_sa_client_get(client);
  725. query->sa_query.client = client;
  726. query->callback = callback;
  727. query->context = context;
  728. mad = query->sa_query.mad_buf->mad;
  729. init_mad(mad, agent);
  730. query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
  731. query->sa_query.release = ib_sa_service_rec_release;
  732. mad->mad_hdr.method = method;
  733. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
  734. mad->sa_hdr.comp_mask = comp_mask;
  735. ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
  736. rec, mad->data);
  737. *sa_query = &query->sa_query;
  738. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  739. if (ret < 0)
  740. goto err2;
  741. return ret;
  742. err2:
  743. *sa_query = NULL;
  744. ib_sa_client_put(query->sa_query.client);
  745. free_mad(&query->sa_query);
  746. err1:
  747. kfree(query);
  748. return ret;
  749. }
  750. EXPORT_SYMBOL(ib_sa_service_rec_query);
  751. static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
  752. int status,
  753. struct ib_sa_mad *mad)
  754. {
  755. struct ib_sa_mcmember_query *query =
  756. container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
  757. if (mad) {
  758. struct ib_sa_mcmember_rec rec;
  759. ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  760. mad->data, &rec);
  761. query->callback(status, &rec, query->context);
  762. } else
  763. query->callback(status, NULL, query->context);
  764. }
  765. static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
  766. {
  767. kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
  768. }
  769. int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
  770. struct ib_device *device, u8 port_num,
  771. u8 method,
  772. struct ib_sa_mcmember_rec *rec,
  773. ib_sa_comp_mask comp_mask,
  774. int timeout_ms, gfp_t gfp_mask,
  775. void (*callback)(int status,
  776. struct ib_sa_mcmember_rec *resp,
  777. void *context),
  778. void *context,
  779. struct ib_sa_query **sa_query)
  780. {
  781. struct ib_sa_mcmember_query *query;
  782. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  783. struct ib_sa_port *port;
  784. struct ib_mad_agent *agent;
  785. struct ib_sa_mad *mad;
  786. int ret;
  787. if (!sa_dev)
  788. return -ENODEV;
  789. port = &sa_dev->port[port_num - sa_dev->start_port];
  790. agent = port->agent;
  791. query = kmalloc(sizeof *query, gfp_mask);
  792. if (!query)
  793. return -ENOMEM;
  794. query->sa_query.port = port;
  795. ret = alloc_mad(&query->sa_query, gfp_mask);
  796. if (ret)
  797. goto err1;
  798. ib_sa_client_get(client);
  799. query->sa_query.client = client;
  800. query->callback = callback;
  801. query->context = context;
  802. mad = query->sa_query.mad_buf->mad;
  803. init_mad(mad, agent);
  804. query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
  805. query->sa_query.release = ib_sa_mcmember_rec_release;
  806. mad->mad_hdr.method = method;
  807. mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
  808. mad->sa_hdr.comp_mask = comp_mask;
  809. ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
  810. rec, mad->data);
  811. *sa_query = &query->sa_query;
  812. ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
  813. if (ret < 0)
  814. goto err2;
  815. return ret;
  816. err2:
  817. *sa_query = NULL;
  818. ib_sa_client_put(query->sa_query.client);
  819. free_mad(&query->sa_query);
  820. err1:
  821. kfree(query);
  822. return ret;
  823. }
  824. static void send_handler(struct ib_mad_agent *agent,
  825. struct ib_mad_send_wc *mad_send_wc)
  826. {
  827. struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
  828. unsigned long flags;
  829. if (query->callback)
  830. switch (mad_send_wc->status) {
  831. case IB_WC_SUCCESS:
  832. /* No callback -- already got recv */
  833. break;
  834. case IB_WC_RESP_TIMEOUT_ERR:
  835. query->callback(query, -ETIMEDOUT, NULL);
  836. break;
  837. case IB_WC_WR_FLUSH_ERR:
  838. query->callback(query, -EINTR, NULL);
  839. break;
  840. default:
  841. query->callback(query, -EIO, NULL);
  842. break;
  843. }
  844. spin_lock_irqsave(&idr_lock, flags);
  845. idr_remove(&query_idr, query->id);
  846. spin_unlock_irqrestore(&idr_lock, flags);
  847. free_mad(query);
  848. ib_sa_client_put(query->client);
  849. query->release(query);
  850. }
  851. static void recv_handler(struct ib_mad_agent *mad_agent,
  852. struct ib_mad_recv_wc *mad_recv_wc)
  853. {
  854. struct ib_sa_query *query;
  855. struct ib_mad_send_buf *mad_buf;
  856. mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
  857. query = mad_buf->context[0];
  858. if (query->callback) {
  859. if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
  860. query->callback(query,
  861. mad_recv_wc->recv_buf.mad->mad_hdr.status ?
  862. -EINVAL : 0,
  863. (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
  864. else
  865. query->callback(query, -EIO, NULL);
  866. }
  867. ib_free_recv_mad(mad_recv_wc);
  868. }
  869. static void ib_sa_add_one(struct ib_device *device)
  870. {
  871. struct ib_sa_device *sa_dev;
  872. int s, e, i;
  873. if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
  874. return;
  875. if (device->node_type == RDMA_NODE_IB_SWITCH)
  876. s = e = 0;
  877. else {
  878. s = 1;
  879. e = device->phys_port_cnt;
  880. }
  881. sa_dev = kzalloc(sizeof *sa_dev +
  882. (e - s + 1) * sizeof (struct ib_sa_port),
  883. GFP_KERNEL);
  884. if (!sa_dev)
  885. return;
  886. sa_dev->start_port = s;
  887. sa_dev->end_port = e;
  888. for (i = 0; i <= e - s; ++i) {
  889. spin_lock_init(&sa_dev->port[i].ah_lock);
  890. if (rdma_port_get_link_layer(device, i + 1) != IB_LINK_LAYER_INFINIBAND)
  891. continue;
  892. sa_dev->port[i].sm_ah = NULL;
  893. sa_dev->port[i].port_num = i + s;
  894. sa_dev->port[i].agent =
  895. ib_register_mad_agent(device, i + s, IB_QPT_GSI,
  896. NULL, 0, send_handler,
  897. recv_handler, sa_dev);
  898. if (IS_ERR(sa_dev->port[i].agent))
  899. goto err;
  900. INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
  901. }
  902. ib_set_client_data(device, &sa_client, sa_dev);
  903. /*
  904. * We register our event handler after everything is set up,
  905. * and then update our cached info after the event handler is
  906. * registered to avoid any problems if a port changes state
  907. * during our initialization.
  908. */
  909. INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
  910. if (ib_register_event_handler(&sa_dev->event_handler))
  911. goto err;
  912. for (i = 0; i <= e - s; ++i)
  913. if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND)
  914. update_sm_ah(&sa_dev->port[i].update_task);
  915. return;
  916. err:
  917. while (--i >= 0)
  918. if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND)
  919. ib_unregister_mad_agent(sa_dev->port[i].agent);
  920. kfree(sa_dev);
  921. return;
  922. }
  923. static void ib_sa_remove_one(struct ib_device *device)
  924. {
  925. struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
  926. int i;
  927. if (!sa_dev)
  928. return;
  929. ib_unregister_event_handler(&sa_dev->event_handler);
  930. flush_scheduled_work();
  931. for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
  932. if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND) {
  933. ib_unregister_mad_agent(sa_dev->port[i].agent);
  934. if (sa_dev->port[i].sm_ah)
  935. kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
  936. }
  937. }
  938. kfree(sa_dev);
  939. }
  940. static int __init ib_sa_init(void)
  941. {
  942. int ret;
  943. get_random_bytes(&tid, sizeof tid);
  944. ret = ib_register_client(&sa_client);
  945. if (ret) {
  946. printk(KERN_ERR "Couldn't register ib_sa client\n");
  947. goto err1;
  948. }
  949. ret = mcast_init();
  950. if (ret) {
  951. printk(KERN_ERR "Couldn't initialize multicast handling\n");
  952. goto err2;
  953. }
  954. return 0;
  955. err2:
  956. ib_unregister_client(&sa_client);
  957. err1:
  958. return ret;
  959. }
  960. static void __exit ib_sa_cleanup(void)
  961. {
  962. mcast_cleanup();
  963. ib_unregister_client(&sa_client);
  964. idr_destroy(&query_idr);
  965. }
  966. module_init(ib_sa_init);
  967. module_exit(ib_sa_cleanup);